Skip to content

Commit 8cfd489

Browse files
committed
examples/chat-server: use environment variables for configuration
1 parent 870e17e commit 8cfd489

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

examples/chat-server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ COPY package.json package.json
55
COPY contract.canvas.js contract.canvas.js
66
RUN npm install --omit-dev
77

8-
CMD ["npm", "run", "start"]
8+
CMD ["npx", "canvas", "run", "/data/app", "--init", "contract.canvas.js", "--metrics"]

examples/chat-server/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# @canvas-js/example-chat-server
2+
3+
This is an example of a replication server for a the `chat-example.canvas.xyz` chat application.
4+
5+
## Local development
6+
7+
Run `npm run dev` to start a temporary in-memory server, or `npm run start` to persist data to a `.cache` directory.
8+
9+
## Deploying to Fly
10+
11+
If you're forking this example and/or running it with a different contract, be sure to edit `fly.toml` and change
12+
13+
- the Fly app name
14+
- the `ANNOUNCE` environment variable to match your Fly app name
15+
- the data source name (from `canvas_chat_data`) to your own Fly volume
16+
17+
Then deploy with
18+
19+
```
20+
fly deploy
21+
```
22+
23+
## Running the Docker container locally
24+
25+
Mount a volume to `/data`. Set the `PORT`, `LISTEN`, `ANNOUNCE`, and `BOOTSTRAP_LIST` environment variables if appropriate.
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
export const topic = "chat-example.canvas.xyz";
1+
export const topic = "chat-example.canvas.xyz"
22

33
export const models = {
4-
message: {
5-
id: "primary",
6-
user: "string",
7-
content: "string",
8-
timestamp: "integer",
9-
$indexes: ["user", "timestamp"],
10-
},
11-
};
4+
message: {
5+
id: "primary",
6+
address: "string",
7+
content: "string",
8+
timestamp: "integer",
9+
$indexes: ["address", "timestamp"],
10+
},
11+
}
1212

1313
export const actions = {
14-
async createMessage(db, { content }, { id, chain, address, timestamp }) {
15-
const user = `${chain}:${address}`;
16-
await db.set("message", { id, user, content, timestamp });
17-
},
18-
};
14+
async createMessage(db, { content }, { id, address, timestamp }) {
15+
console.log("received message:", content)
16+
await db.set("message", { id, address, content, timestamp })
17+
},
18+
}

examples/chat-server/fly.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ processes = []
77

88
[env]
99
DEBUG = "canvas:*:error"
10+
LISTEN=/ip6/::/tcp/8080/ws
11+
ANNOUNCE=/dns4/canvas-chat.fly.dev/tcp/443/wss
1012

1113
[mounts]
1214
destination = "/data"

examples/chat-server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"version": "0.8.14",
55
"type": "module",
66
"scripts": {
7-
"start": "canvas run /data/app --init contract.canvas.js --verbose --metrics --listen /ip6/::/tcp/8080/ws --announce /dns4/canvas-chat.fly.dev/tcp/443/wss"
7+
"dev": "canvas run contract.canvas.js",
8+
"start": "canvas run .cache --init contract.canvas.js"
89
},
910
"dependencies": {
1011
"@canvas-js/cli": "0.8.14"

0 commit comments

Comments
 (0)