Skip to content

Commit

Permalink
Merge pull request #15 from willroberts/firebase-backends
Browse files Browse the repository at this point in the history
Updates Firebase client for servers and workers
  • Loading branch information
marwanhilmi committed Sep 6, 2022
2 parents c4d3352 + 60001b6 commit 4adb4ef
Show file tree
Hide file tree
Showing 23 changed files with 1,111 additions and 289 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Secrets
.env
.env_for_alpha
serviceAccountKey.json

# Build artifacts
/app/data/packages.js
/app/localization/locales/en/index.json
Expand All @@ -8,8 +13,6 @@ node_modules
*.log
*.log.txt
dump.rdb
.env
.env_for_alpha
.foreman
.idea/
.node-version
Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Worker:

#### Other Dependencies

Firebase:

- Client code can be found in `app` (see `new Firebase()` calls) and `server/lib/duelyst_firebase_module.coffee`, and configuration can be found in `config/`

Postgres:

- Client code can be found in `server/lib/data_access/knex.coffee` and `server/knexfile.js`, and configuration can be found in `config/`
Expand All @@ -64,9 +68,9 @@ Consul:

#### Building the code

Install NPM tools:
Install some NPM tools (can use `sudo` on Mac/Linux):
```
npm install -g typescript yarn gulpjs/gulp-cli
npm install -g typescript yarn
```

Compile TypeScript dependencies:
Expand Down Expand Up @@ -97,11 +101,13 @@ When working in the `server` or `worker` directories, no rebuilds are needed. Se

#### Starting a test environment in Docker

- Modify `development.json` with:
- Firebase Realtime endpoint and secret
- Redis connection string
- Postgres connection string
- Optionally enable debug logging for sockets by prepending `yarn $1` with `DEBUG=*` in `docker/start`
- Create a Firebase Realtime Database
- This is a Google Cloud product, and they offer a no-cost "Spark" plan
- Set the `FIREBASE_URL` environment variable in an `.env` file in the repo root
- For example, `FIREBASE_URL=https://my-example-project.firebaseio.com/`
- Create a new service account with r/w access to your realtime DB
- Create a new JSON key for the service account, and store it in a `serviceAccountKey.json` file in the repo root
- Optionally enable debug logging for WebSockets by prepending `yarn $1` with `DEBUG=*` in `docker/start`
- Use `docker compose up` to start required services locally, or start individual services:
- `redis`
- `db` (Postgres)
Expand All @@ -117,11 +123,11 @@ When working in the `server` or `worker` directories, no rebuilds are needed. Se
- In another terminal window, use `NODE_ENV=development yarn migrate:latest` to run database migrations
- On Windows: `$env:NODE_ENV = 'development'; yarn migrate:latest`
- Only need to run this once (unless you change Postgres schema)
- Open http://localhost:3000 in a browser to load the game client
- Once you see `Duelyst 'development' started on port 3000` in the logs, open http://localhost:3000 in a browser to load the game client

#### Starting individual components

- `yarn api` to start api server
- `yarn game` to start game server
- `yarn sp` to start single player game server
- `yarn worker` to start worker
- `yarn worker` to start worker
61 changes: 30 additions & 31 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,36 @@ var config = convict({
env: "API_URL"
},
firebase: {
doc: "Firebase URL.",
// format: "url",
default: "https://duelyst-local.firebaseio.com/",
env: "FIREBASE_URL"
},
firebaseToken: {
doc: "Firebase security token.",
default: "",
env: "FIREBASE_TOKEN"
},
firebaseLoggingEnabled: {
doc: "Firebase logging enabled flag.",
default: false,
env: "FIREBASE_LOGGING"
},
auth: {
doc: "Firebase URL for auth service",
// format: "url",
default: "", // "https://duelyst-dev-auth.firebaseio.com/"
env: "AUTH_URL"
},
authToken: {
doc: "Firebase auth token.",
default: "", // ""
env: "AUTH_TOKEN"
url: {
doc: "Firebase URL, e.g. https://my-duelyst-project-12345.firebaseio.com/",
// format: "url",
default: "",
env: "FIREBASE_URL"
},
authServiceUrl: {
doc: "Firebase URL for auth service",
// format: "url",
default: "",
env: "FIREBASE_AUTH_URL"
},
loggingEnabled: {
doc: "Enable logging in the Firebase Admin SDK.",
default: false,
env: "FIREBASE_LOGGING_ENABLED"
}
},
tokenExpiration: {
doc: "Time (in minutes) before tokens expire.",
format: "int",
default: 60 * 24 * 14, // 14 days in minutes
env: "TOKEN_EXPIRES"
jwt: {
signingSecret: {
doc: "The secret used when signing JSON Web Tokens",
default: "duelyst", // Set in .env
env: "JWT_SECRET"
},
tokenExpiration: {
doc: "Time (in minutes) before tokens expire.",
format: "int",
default: 60 * 24 * 14, // 14 days in minutes
env: "TOKEN_EXPIRES"
},
},
cdn: {
doc: "CDN / S3 url. Default is blank.",
Expand Down Expand Up @@ -431,7 +430,7 @@ config.version = require('../version').version
var pgUrl = url.parse(config.get('postgres_connection_string'));
console.log("CONFIG: version:"+config.version)
console.log("CONFIG: env:"+config.get('env'))
console.log("CONFIG: firebase:"+url.parse(config.get('firebase')).host)
console.log("CONFIG: firebase:"+url.parse(config.get('firebase.url')).host)
console.log("CONFIG: postgres:"+pgUrl.host+pgUrl.pathname)
console.log("CONFIG: redis:"+config.get('redis.ip'))
// console.log("CONFIG: paypal_buttons:",config.get('paypalButtons'))
Expand Down
4 changes: 1 addition & 3 deletions config/development.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"env": "development",
"api": "http://localhost:3000",
"postgres_connection_string": "postgres://duelyst:duelyst@0.0.0.0:5431/duelyst",
"firebase": "https://duelyst-development.firebaseio.com/",
"firebaseToken": "K4EkQWIT9UrTATVW9h0dHhf8t1nvGVjWMi8rPHb2",
"postgres_connection_string": "postgres://duelyst:duelyst@0.0.0.0:5432/duelyst",
"redis": {
"ip": "localhost",
"port": 6379,
Expand Down
2 changes: 0 additions & 2 deletions config/production.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"env": "production",
"api": "",
"firebase": "",
"firebaseToken": "",
"cdn": "",
"s3_url":"",
"consul": {
Expand Down
72 changes: 62 additions & 10 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ version: "3.9"

x-base-service: &base
image: node:lts
environment:
REDIS_IP: redis
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
depends_on:
- db
- redis
entrypoint: bash
working_dir: /app
volumes:
Expand All @@ -19,50 +13,80 @@ services:
image: "redis"
ports:
- "6379:6379"

db:
image: postgres
image: postgres:14
restart: always
environment:
POSTGRES_USER: duelyst
POSTGRES_PASSWORD: duelyst
POSTGRES_DB: duelyst
ports:
- 5431:5432
- 5432:5432
volumes:
- ./.pgdata:/var/lib/postgresql/data

adminer:
image: adminer
profiles:
- donotstart
restart: always
ports:
- 8080:8080
environment:
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
depends_on:
- db

api:
<<: *base
ports:
- 3000:3000
environment:
REDIS_IP: redis
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
FIREBASE_URL: ${FIREBASE_URL}
command: docker/start api
depends_on:
- db
- redis

game:
<<: *base
profiles:
- donotstart
ports:
- 8000:8000
environment:
REDIS_IP: redis
FIREBASE_URL: ${FIREBASE_URL}
command: docker/start game
depends_on:
- redis

sp:
<<: *base
ports:
- 8000:8000
environment:
REDIS_IP: redis
FIREBASE_URL: ${FIREBASE_URL}
command: docker/start sp
depends_on:
- redis

worker:
<<: *base
ports:
- 3002:8080
environment:
REDIS_IP: redis
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
FIREBASE_URL: ${FIREBASE_URL}
command: docker/start worker
depends_on:
- db
- redis

worker-ui:
<<: *base
Expand All @@ -71,6 +95,8 @@ services:
ports:
- 3001:3000
command: docker/start worker-ui
depends_on:
- worker

migrate:
image: node:lts
Expand All @@ -80,10 +106,36 @@ services:
DEBUG: "*"
NODE_ENV: development
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
FIREBASE_URL: ${FIREBASE_URL}
depends_on:
- db
working_dir: /app
volumes:
- .:/app
entrypoint: "bash -c"
command: "yarn install --dev && yarn migrate:latest"
command: docker/start migrate:latest

test-unit:
<<: *base
profiles:
- donotstart
environment:
REDIS_IP: redis
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
FIREBASE_URL: ${FIREBASE_URL}
command: docker/start test:unit
depends_on:
- db
- redis

test-integration:
<<: *base
profiles:
- donotstart
environment:
REDIS_IP: redis
POSTGRES_CONNECTION: "pg://duelyst:duelyst@db/duelyst"
FIREBASE_URL: ${FIREBASE_URL}
command: docker/start test:integration
depends_on:
- db
- redis
8 changes: 7 additions & 1 deletion docker/start
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/usr/bin/env bash

# Work around git vs https issue for old packages.
git config --global url."https://github.com/".insteadOf git@github.com:
git config --global url."https://".insteadOf git://

# Install dependencies
yarn install --dev

# To enable socket debug logs, prepend this command with DEBUG=*
yarn $1
yarn $1
2 changes: 1 addition & 1 deletion gulp/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bundler.transform(envify({
NODE_ENV: env,
VERSION: version,
API_URL: config.get('api'),
FIREBASE_URL: config.get('firebase'),
FIREBASE_URL: config.get('firebase.url'),
AMI_ID: config.get('amaId'),
MARKETING_AMA_ID: config.get('marketingAmaId'),
STRIPE_CLIENT_KEY: config.get('stripeClientKey'),
Expand Down
2 changes: 1 addition & 1 deletion gulp/bundler.register.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bundler.transform(envify({
NODE_ENV: env,
VERSION: version,
API_URL: config.get('api'),
FIREBASE_URL: config.get('firebase'),
FIREBASE_URL: config.get('firebase.url'),
AMI_ID: config.get('amaId'),
MARKETING_AMA_ID: config.get('marketingAmaId'),
STRIPE_CLIENT_KEY: config.get('stripeClientKey'),
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@counterplay/chromajs": "./packages/chroma-js",
"@counterplay/exception-reporter": "./packages/exception-reporter",
"@counterplay/warlock": "./packages/warlock",
"@firebase/app-types": "^0.7.0",
"@thream/socketio-jwt": "^3.0.0",
"analytics-node": "*",
"app-module-path": "^1.0.6",
Expand All @@ -28,6 +29,7 @@
"express-real-ip": "^1.0.0",
"fb": "^2.0.0-alpha1",
"firebase": "2.0.3",
"firebase-admin": "^11.0.1",
"form-urlencoded": "^1.2.1",
"gamecenter-identity-verifier": "^0.1.1",
"glicko2": "^0.8.4",
Expand Down Expand Up @@ -159,7 +161,6 @@
"mailchimp": "1.1.0",
"minimist": "^1.2.0",
"mocha": "^2.4.5",
"mockfirebase": "^0.3.0",
"ncp": "^2.0.0",
"power-assert": "^1.4.1",
"progress": "1.1.8",
Expand All @@ -183,7 +184,6 @@
"sp": "node ./bin/single_player",
"worker": "node ./bin/worker",
"worker-ui": "node ./bin/workerui",

"build:app": "node --max_old_space_size=2048 --stack-size=100000 node_modules/gulp/bin/gulp.js build:app --watch=false",
"build:desktop": "node --max_old_space_size=2048 --stack-size=100000 node_modules/gulp/bin/gulp.js desktop:build",
"build:release": "node --max_old_space_size=2048 --stack-size=100000 node_modules/gulp/bin/gulp.js build:release",
Expand All @@ -199,11 +199,14 @@
"push:master": "gulp git:master:patch",
"push:staging": "gulp git:staging",
"rsx:copy": "gulp rsx:copy",
"test:all": "mocha -t 10000",
"test:data": "mocha test/unit/data_access --recursive -t 15000",
"test:misc": "mocha test/unit/misc --recursive -t 5000",
"test:sdk": "mocha test/unit/sdk --recursive -t 5000",
"test": "mocha",
"test:achievements": "mocha -t 100000 --recursive test/unit/achievements",
"test:data": "mocha -t 15000 --recursive test/unit/data_access",
"test:firebase": "mocha -t 1000 test/unit/firebase test/integration/firebase",
"test:misc": "mocha -t 5000 --recursive test/unit/misc",
"test:sdk": "mocha -t 5000 --recursive test/unit/sdk",
"test:unit": "mocha -t 10000 --recursive test/unit",
"test:integration": "mocha -t 10000 --recursive test/integration",
"test": "mocha -t 10000",
"watch": "node --max_old_space_size=2048 --stack-size=100000 node_modules/gulp/bin/gulp.js build"
},
"xo": {
Expand Down
Loading

0 comments on commit 4adb4ef

Please sign in to comment.