Skip to content

Commit 7765675

Browse files
committed
First draft
1 parent b664f4d commit 7765675

File tree

10 files changed

+307
-0
lines changed

10 files changed

+307
-0
lines changed

examples/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Running examples with Couchbase Sync Gateway & Server
2+
3+
Couchbase Lite is often used with replication to a central server, so it can be useful to test the full stack.
4+
The examples in this directory aim at covering these use cases.
5+
6+
## Setup the Couchbase Sync Gateway & Server
7+
8+
This process is handled through docker images, with as an entry point the file `docker-conf/docker-compose.yml`.
9+
10+
The configuration files that might interest are:
11+
- `docker-conf/couchbase-server-dev/configure-server.sh` -> sets up the cluster, bucket and SG user
12+
- `docker-conf/db-config.json` -> contains the database configuration
13+
- `docker-conf/sync-function.js` -> contains the sync function used by the Sync Gateway
14+
15+
To start both the Sync Gatewawy and Couchbase Server, move to `docker-conf` through a terminal and use:
16+
17+
```shell
18+
$ docker-compose up
19+
```
20+
21+
##
22+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# hadolint ignore=DL3007
2+
FROM couchbase/server:enterprise
3+
COPY configure-server.sh /configure-server.sh
4+
5+
ENTRYPOINT ["/configure-server.sh"]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env bash
2+
3+
export COUCHBASE_ADMINISTRATOR_USERNAME="cb_admin"
4+
export COUCHBASE_ADMINISTRATOR_PASSWORD="cb_admin_pwd"
5+
6+
export COUCHBASE_BUCKET="my-bucket"
7+
8+
export COUCHBASE_SG_USERNAME="syncgw"
9+
export COUCHBASE_SG_PASSWORD="syncgw-pwd"
10+
export COUCHBASE_SG_NAME="sg-service-user"
11+
12+
function retry() {
13+
for i in $(seq 1 10); do
14+
$1
15+
if [[ $? == 0 ]]; then
16+
return 0
17+
fi
18+
sleep 1
19+
done
20+
return 1
21+
}
22+
23+
function clusterInit() {
24+
couchbase-cli cluster-init \
25+
-c 127.0.0.1:8091 \
26+
--cluster-username $COUCHBASE_ADMINISTRATOR_USERNAME \
27+
--cluster-password $COUCHBASE_ADMINISTRATOR_PASSWORD \
28+
--services data,index,query \
29+
--cluster-ramsize 256 \
30+
--cluster-index-ramsize 256 \
31+
--index-storage-setting default
32+
if [[ $? != 0 ]]; then
33+
return 1
34+
fi
35+
}
36+
37+
function bucketCreate() {
38+
couchbase-cli bucket-create \
39+
-c 127.0.0.1:8091 \
40+
--username $COUCHBASE_ADMINISTRATOR_USERNAME \
41+
--password $COUCHBASE_ADMINISTRATOR_PASSWORD \
42+
--bucket-type=couchbase \
43+
--bucket-ramsize=100 \
44+
--bucket-replica=0 \
45+
--bucket $COUCHBASE_BUCKET \
46+
--wait
47+
if [[ $? != 0 ]]; then
48+
return 1
49+
fi
50+
}
51+
52+
function userSgCreate() {
53+
couchbase-cli user-manage \
54+
-c 127.0.0.1:8091 \
55+
--username $COUCHBASE_ADMINISTRATOR_USERNAME \
56+
--password $COUCHBASE_ADMINISTRATOR_PASSWORD \
57+
--set \
58+
--rbac-username $COUCHBASE_SG_USERNAME \
59+
--rbac-password $COUCHBASE_SG_PASSWORD \
60+
--rbac-name $COUCHBASE_SG_NAME \
61+
--roles bucket_full_access[*],bucket_admin[*] \
62+
--auth-domain local
63+
if [[ $? != 0 ]]; then
64+
return 1
65+
fi
66+
}
67+
68+
function main() {
69+
/entrypoint.sh couchbase-server &
70+
if [[ $? != 0 ]]; then
71+
echo "Couchbase startup failed. Exiting." >&2
72+
exit 1
73+
fi
74+
75+
# wait for service to come up
76+
until $(curl --output /dev/null --silent --head --fail http://localhost:8091); do
77+
sleep 5
78+
done
79+
80+
if couchbase-cli server-list -c 127.0.0.1:8091 --username $COUCHBASE_ADMINISTRATOR_USERNAME --password $COUCHBASE_ADMINISTRATOR_PASSWORD ; then
81+
echo "Couchbase already initialized, skipping initialization"
82+
else
83+
echo "Couchbase is not configured."
84+
echo
85+
86+
echo "Initializing the cluster...."
87+
retry clusterInit
88+
if [[ $? != 0 ]]; then
89+
echo "Cluster init failed. Exiting." >&2
90+
exit 1
91+
fi
92+
echo "Initializing the cluster [OK]"
93+
echo
94+
95+
echo "Creating the bucket...."
96+
retry bucketCreate
97+
if [[ $? != 0 ]]; then
98+
echo "Bucket create failed. Exiting." >&2
99+
exit 1
100+
fi
101+
echo "Creating the bucket [OK]"
102+
echo
103+
104+
echo "Creating Sync Gateway user...."
105+
retry userSgCreate
106+
if [[ $? != 0 ]]; then
107+
echo "User create failed. Exiting." >&2
108+
exit 1
109+
fi
110+
echo "Creating Sync Gateway user [OK]"
111+
echo
112+
113+
sleep 10
114+
115+
fi
116+
117+
wait
118+
}
119+
120+
main
121+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"import_docs": true,
3+
"enable_shared_bucket_access": true,
4+
"bucket": "my-bucket",
5+
"num_index_replicas": 0,
6+
"revs_limit": 20,
7+
"allow_conflicts": false
8+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
services:
2+
couchbase-server:
3+
image: couchbase-server:dev
4+
ports:
5+
- "8091:8091" # REST(admin), Web console
6+
- "8093:8093" # Query service REST/HTTP traffic
7+
- "11207:11207" # memcached port (TLS)
8+
- "11210:11210" # memcached port
9+
build:
10+
context: ${PWD}/couchbase-server-dev
11+
deploy:
12+
resources:
13+
limits:
14+
memory: 2048M
15+
restart: on-failure
16+
sync-gateway:
17+
image: couchbase/sync-gateway:enterprise
18+
ports:
19+
- "4984:4984"
20+
- "4985:4985"
21+
deploy:
22+
resources:
23+
limits:
24+
memory: 512M
25+
healthcheck:
26+
test: ["CMD", "curl", "-f", "http://localhost:4985"]
27+
interval: 30s
28+
timeout: 10s
29+
retries: 5
30+
volumes:
31+
- ${PWD}/syncgw-config.json:/etc/sync_gateway/config.json:ro
32+
- ${PWD}/wait-for-couchbase-server.sh:/wait-for-couchbase-server.sh
33+
depends_on:
34+
- couchbase-server
35+
entrypoint: ["/wait-for-couchbase-server.sh"]
36+
restart: on-failure
37+
sync-gateway-setup:
38+
image: alpine:3.21.3@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c
39+
depends_on:
40+
sync-gateway:
41+
condition: service_healthy
42+
volumes:
43+
- ${PWD}/sync-function.js:/sync-function.js
44+
- ${PWD}/db-config.json:/db-config.json
45+
- ${PWD}/update.sh:/update.sh
46+
links:
47+
- "sync-gateway:sg"
48+
entrypoint: /update.sh
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
function sync(doc, oldDoc, meta) {
3+
console.log("=== New document revision ===");
4+
console.log("New doc: ", doc);
5+
console.log("Old doc: ", oldDoc);
6+
console.log("Metadata: ", meta);
7+
8+
if(doc.channels) {
9+
channel(doc.channels);
10+
}
11+
if(doc.expiry) {
12+
// Format: "2022-06-23T05:00:00+01:00"
13+
expiry(doc.expiry);
14+
}
15+
16+
console.log("=== Document processed ===");
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"bootstrap": {
3+
"server": "couchbase://couchbase-server",
4+
"username": "syncgw",
5+
"password": "syncgw-pwd"
6+
},
7+
"api": {
8+
"public_interface": ":4984",
9+
"admin_interface": ":4985",
10+
"admin_interface_authentication": false,
11+
"https": {}
12+
},
13+
"logging": {
14+
"console": {
15+
"rotation": {},
16+
"log_level": "debug",
17+
"log_keys": [
18+
"*"
19+
]
20+
},
21+
"error": {
22+
"rotation": {}
23+
},
24+
"warn": {
25+
"rotation": {}
26+
},
27+
"info": {
28+
"rotation": {}
29+
},
30+
"debug": {
31+
"rotation": {}
32+
},
33+
"trace": {
34+
"rotation": {}
35+
},
36+
"stats": {
37+
"rotation": {}
38+
}
39+
},
40+
"auth": {},
41+
"replicator": {},
42+
"unsupported": {}
43+
}

examples/docker-conf/update.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh -x
2+
3+
apk add curl
4+
5+
export DB_NAME="my-db"
6+
7+
echo 'START SG Update'
8+
echo
9+
10+
# Setting up database API: https://docs.couchbase.com/sync-gateway/current/rest_api_admin.html#tag/Database-Management/operation/put_db-
11+
echo 'Setting up the database...'
12+
curl -XPUT -v "http://sg:4985/${DB_NAME}/" -H 'Content-Type: application/json' --data-binary @db-config.json
13+
echo
14+
15+
# Updating sync function API: https://docs.couchbase.com/sync-gateway/current/rest_api_admin.html#tag/Database-Configuration/operation/put_keyspace-_config-sync
16+
# Sync function doc: https://docs.couchbase.com/sync-gateway/current/sync-function.html
17+
echo 'Updating sync function...'
18+
curl -XPUT -v "http://sg:4985/${DB_NAME}/_config/sync" -H 'Content-Type: application/javascript' --data-binary @sync-function.js
19+
20+
echo 'END SG Update'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Launch SG"
4+
SG_CONFIG_PATH=/etc/sync_gateway/config.json
5+
6+
COUCHBASE_SERVER_URL="http://couchbase-server:8091"
7+
SG_AUTH_ARG="syncgw:syncgw-pwd"
8+
9+
while ! { curl -X GET -u $SG_AUTH_ARG $COUCHBASE_SERVER_URL/pools/default/buckets -H "accept: application/json" -s | grep -q '"status":"healthy"'; }; do
10+
echo "Wait 🕑"
11+
sleep 1
12+
done
13+
echo "CB ready, starting SG"
14+
15+
sleep 5
16+
17+
/entrypoint.sh -bootstrap.use_tls_server=false $SG_CONFIG_PATH
18+
19+

examples/sgw_1_cblite.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use couchbase_lite::*;
2+
3+
///
4+
fn main() {}

0 commit comments

Comments
 (0)