|
| 1 | +use std::{collections::HashMap, path::Path}; |
| 2 | + |
1 | 3 | use couchbase_lite::*;
|
2 | 4 |
|
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