Skip to content

Commit 19945d9

Browse files
committed
The simplest of replications is working
1 parent 53b8656 commit 19945d9

File tree

7 files changed

+149
-15
lines changed

7 files changed

+149
-15
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ tempdir = "*"
1010
lazy_static = "1.4.0"
1111
regex = "1.10.4"
1212

13+
[dev-dependencies]
14+
serde_json = "1"
15+
16+
[dev-dependencies.reqwest]
17+
version = "0.12.15"
18+
default-features = false
19+
features = ["blocking", "json"]
20+
1321
[dev-dependencies.cargo-husky]
1422
version = "1"
1523
default-features = false # Disable features which are enabled by default

examples/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,23 @@ To start both the Sync Gatewawy and Couchbase Server, move to `docker-conf` thro
1818
$ docker-compose up
1919
```
2020

21-
##
21+
The first time, it's very long.
2222

23+
## Update the config after startup
24+
25+
You can change a few things through the `curl` command.
26+
27+
#### Sync function
28+
29+
Update the file `docker-conf/sync-function.js` and run
30+
```shell
31+
$ curl -XPUT -v "http://localhost:4985/my-db/_config/sync" -H 'Content-Type: application/javascript' --data-binary @docker-conf/sync-function.js
32+
```
33+
34+
#### Database config
35+
36+
Update the file `docker-conf/db-config.json` and run
37+
38+
```shell
39+
$ curl -XPUT -v "http://localhost:4985/my-db/" -H 'Content-Type: application/json' --data-binary @docker-conf/db-config.json
40+
```

examples/docker-conf/docker-compose.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
services:
2-
couchbase-server:
2+
cblr-couchbase-server:
33
ports:
44
- "8091:8091" # REST(admin), Web console
55
- "8093:8093" # Query service REST/HTTP traffic
@@ -12,7 +12,7 @@ services:
1212
limits:
1313
memory: 2048M
1414
restart: on-failure
15-
sync-gateway:
15+
cblr-sync-gateway:
1616
image: couchbase/sync-gateway:enterprise
1717
ports:
1818
- "4984:4984"
@@ -30,18 +30,18 @@ services:
3030
- ${PWD}/syncgw-config.json:/etc/sync_gateway/config.json:ro
3131
- ${PWD}/wait-for-couchbase-server.sh:/wait-for-couchbase-server.sh
3232
depends_on:
33-
- couchbase-server
33+
- cblr-couchbase-server
3434
entrypoint: ["/wait-for-couchbase-server.sh"]
3535
restart: on-failure
36-
sync-gateway-setup:
36+
cblr-sync-gateway-setup:
3737
image: alpine:3.21.3@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c
3838
depends_on:
39-
sync-gateway:
39+
cblr-sync-gateway:
4040
condition: service_healthy
4141
volumes:
4242
- ${PWD}/sync-function.js:/sync-function.js
4343
- ${PWD}/db-config.json:/db-config.json
4444
- ${PWD}/update.sh:/update.sh
4545
links:
46-
- "sync-gateway:sg"
46+
- "cblr-sync-gateway:sg"
4747
entrypoint: /update.sh

examples/docker-conf/sync-function.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
21
function sync(doc, oldDoc, meta) {
32
console.log("=== New document revision ===");
4-
console.log("New doc: ", doc);
5-
console.log("Old doc: ", oldDoc);
6-
console.log("Metadata: ", meta);
3+
console.log("New doc:");
4+
console.log(doc);
5+
console.log("Old doc:");
6+
console.log(oldDoc);
7+
console.log("Metadata:");
8+
console.log(meta);
79

810
if(doc.channels) {
911
channel(doc.channels);

examples/docker-conf/syncgw-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"bootstrap": {
3-
"server": "couchbase://couchbase-server",
3+
"server": "couchbase://cblr-couchbase-server",
44
"username": "syncgw",
55
"password": "syncgw-pwd"
66
},

examples/docker-conf/wait-for-couchbase-server.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
echo "Launch SG"
44
SG_CONFIG_PATH=/etc/sync_gateway/config.json
55

6-
COUCHBASE_SERVER_URL="http://couchbase-server:8091"
6+
COUCHBASE_SERVER_URL="http://cblr-couchbase-server:8091"
77
SG_AUTH_ARG="syncgw:syncgw-pwd"
88

99
while ! { curl -X GET -u $SG_AUTH_ARG $COUCHBASE_SERVER_URL/pools/default/buckets -H "accept: application/json" -s | grep -q '"status":"healthy"'; }; do

examples/sgw_1_cblite.rs

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,110 @@
1+
use std::{collections::HashMap, path::Path};
2+
13
use couchbase_lite::*;
24

3-
///
4-
fn main() {}
5+
pub const SYNC_GW_URL_ADMIN: &str = "http://localhost:4985/my-db";
6+
pub const SYNC_GW_URL: &str = "ws://localhost:4984/my-db";
7+
8+
fn main() {
9+
let mut db = Database::open(
10+
"test1",
11+
Some(DatabaseConfiguration {
12+
directory: Path::new(
13+
"/Users/antoinemenciere/Projects/couchbase-lite-rust-docto/examples",
14+
),
15+
encryption_key: None,
16+
}),
17+
)
18+
.unwrap();
19+
20+
add_or_update_user("great_name", vec!["channel1".into()]);
21+
let session_token = Some(get_session("great_name")).unwrap();
22+
print!("Sync gateway session token: {session_token}");
23+
24+
let repl_conf = ReplicatorConfiguration {
25+
database: Some(db.clone()),
26+
endpoint: Endpoint::new_with_url(SYNC_GW_URL).unwrap(),
27+
replicator_type: ReplicatorType::PushAndPull,
28+
continuous: true,
29+
disable_auto_purge: true, // false if we want auto purge when the user loses access to a document
30+
max_attempts: 3,
31+
max_attempt_wait_time: 1,
32+
heartbeat: 60,
33+
authenticator: None,
34+
proxy: None,
35+
headers: vec![(
36+
"Cookie".to_string(),
37+
format!("SyncGatewaySession={session_token}"),
38+
)]
39+
.into_iter()
40+
.collect(),
41+
pinned_server_certificate: None,
42+
trusted_root_certificates: None,
43+
channels: MutableArray::default(),
44+
document_ids: MutableArray::default(),
45+
collections: None,
46+
accept_parent_domain_cookies: false,
47+
};
48+
let repl_context = ReplicationConfigurationContext::default();
49+
let mut repl = Replicator::new(repl_conf, Box::new(repl_context)).unwrap();
50+
51+
repl.start(false);
52+
53+
println!("Replicator state: {:?}", repl.status());
54+
std::thread::sleep(std::time::Duration::from_secs(1));
55+
println!("Replicator state: {:?}", repl.status());
56+
std::thread::sleep(std::time::Duration::from_secs(1));
57+
println!("Replicator state: {:?}", repl.status());
58+
std::thread::sleep(std::time::Duration::from_secs(1));
59+
println!("Replicator state: {:?}", repl.status());
60+
std::thread::sleep(std::time::Duration::from_secs(1));
61+
println!("Replicator state: {:?}", repl.status());
62+
63+
let mut doc = Document::new_with_id("id1");
64+
doc.set_properties_as_json(r#"{"name": "allo"}"#).unwrap();
65+
db.save_document(&mut doc).unwrap();
66+
67+
assert!(db.get_document("id1").is_ok());
68+
print!("Doc content: {}", doc.properties_as_json());
69+
70+
println!("Replicator state: {:?}", repl.status());
71+
std::thread::sleep(std::time::Duration::from_secs(1));
72+
println!("Replicator state: {:?}", repl.status());
73+
std::thread::sleep(std::time::Duration::from_secs(1));
74+
println!("Replicator state: {:?}", repl.status());
75+
std::thread::sleep(std::time::Duration::from_secs(1));
76+
println!("Replicator state: {:?}", repl.status());
77+
78+
repl.stop(None);
79+
80+
// Create new user session: https://docs.couchbase.com/sync-gateway/current/rest_api_admin.html#tag/Session/operation/post_db-_session
81+
}
82+
83+
fn add_or_update_user(name: &str, channels: Vec<String>) {
84+
let url_admin_sg = format!("{SYNC_GW_URL_ADMIN}/_user/");
85+
let user_to_post = serde_json::json!({
86+
"name": name,
87+
"password": "very_secure",
88+
"admin_channels": channels
89+
});
90+
let result = reqwest::blocking::Client::new()
91+
.post(url_admin_sg)
92+
.json(&user_to_post)
93+
.send();
94+
println!("{result:?}");
95+
}
96+
97+
fn get_session(name: &str) -> String {
98+
let url_admin_sg = format!("{SYNC_GW_URL_ADMIN}/_session");
99+
let to_post = serde_json::json!({
100+
"name": name,
101+
});
102+
let result: serde_json::Value = reqwest::blocking::Client::new()
103+
.post(url_admin_sg)
104+
.json(&to_post)
105+
.send()
106+
.unwrap()
107+
.json()
108+
.unwrap();
109+
result["session_id"].as_str().unwrap().to_string()
110+
}

0 commit comments

Comments
 (0)