Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Technical feature/NR-50 setup mongodb cloud instance (#155)
Browse files Browse the repository at this point in the history
* extracted config to package

* updated gitignore

* added dotenv

* added fatal log

* updated db init

* updated logger

# switched to class entirely

* switched to config

# switched to debug logs

* updated gitignore

* updated gitignore

* added db directory

* updated env var

* fixed password bug

* fixed logger bug

* added local db init

* updated gitignore

* added url debug log

* added start and stop scripts

# start using local db
# start using cloud db

* extracted config to env vars

* fixed failing unit tests

* fixed int test bugs

* simplified setup

* update params

* fixed container issues

# added env vars to docker compose file
# added webserver listen ip
# fixed typo in config

* updated docu

* updated dockerignore

* added rootless mode

* added debug version

# compose file
# npm script
# launch configuration

* renamed launch configurations

* added build option

* added debug configuration

# added launch config
# added tasks: start in debug mode, stop

* updated debug config

* removed build script

* added tasks

* updated docu

# added debugging
# updated run
  • Loading branch information
stas-at-ibm authored Aug 31, 2023
1 parent 2ec8afb commit 356b185
Show file tree
Hide file tree
Showing 26 changed files with 510 additions and 210 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
_node_modules/
node_modules/
db/
db/data
/.pnp
.pnp.js

Expand All @@ -13,12 +13,15 @@ build

# misc
.DS_Store
*.env
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
todo.md
.mongodb
.secrets

npm-debug.log*
yarn-debug.log*
Expand Down
23 changes: 0 additions & 23 deletions .vscode/project.code-workspace

This file was deleted.

107 changes: 107 additions & 0 deletions .vscode/steam-game-stats.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"folders": [
{
"name": "steam-game-stats",
"path": "../"
},
{
"name": "db",
"path": "../db"
},
{
"name": "backend",
"path": "../backend"
},
{
"name": "frontend",
"path": "../frontend"
},
],
"settings": {
"files.exclude": {
"node_modules/": false,
"db/": true,
"backend/": true,
"frontend/": true
}
},
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "Docker: attach and debug backend",
"type": "node",
"request": "attach",
"remoteRoot": "/app/steam-game-stats/backend",
"localRoot": "${workspaceFolder}/backend",
"port": 9229,
"address": "localhost",
"preLaunchTask": "Docker: start backend in debug with local-db",
"postDebugTask": "Docker: stop"
},
],
},
"tasks": {
"version": "2.0.0",
"tasks": [
{
"label": "Docker: start backend in debug with local-db",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
},
"files": [
"${workspaceFolder}/docker-compose.yaml",
"${workspaceFolder}/docker-compose.debug.yaml"
],
"envFile": "${workspaceFolder}/config/local-db.env",
},
"problemMatcher": {
"base": "$tsc-watch",
"background": {
"activeOnStart": true,
"beginsPattern": "Building",
"endsPattern": "Debugger listening on"
}
}
},
{
"label": "Docker: start backend with local-db",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
},
"files": [
"${workspaceFolder}/docker-compose.yaml",
],
"envFile": "${workspaceFolder}/config/local-db.env",
}
},
{
"label": "Docker: start backend with cloud-db",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
},
"files": [
"${workspaceFolder}/docker-compose.yaml",
],
"envFile": "${workspaceFolder}/config/cloud-db.env",
}
},
{
"label": "Docker: stop",
"type": "docker-compose",
"dockerCompose": {
"down": {},
}
}
]
}
}
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,126 @@

- [frontend/README.md](frontend/README.md)
- [backend/README.md](backend/README.md)

## Development Setup

For development and testing the application locally use the Docker Compose setup. Either connect to a MongoDB running in a container or to a remote MongoDB instance. See details below on how to do that.

**Step 1.**, install:

- [Node.js v18.17.x](https://nodejs.org/download/release/v18.17.1/).
- [Docker Desktop](https://www.docker.com/products/docker-desktop/).
- [VSCode](https://code.visualstudio.com/) or any other code editor.
- [MongoDB extension](https://www.mongodb.com/products/vs-code) to test your queries if you use VSCode.

**Step 2.**:

- check out code from this repo

**Step 3.1**, local setup:

- For the local db setup create a `local-db.env` in the root folder in the `config` directory:

```bash
# ./config/local-db.env
DB_ENDPOINT="mongodb://mongodb:27017"
DB_USERNAME="<local-db-username>" # pick a username
DB_PASSWORD="<local-db-password>" # generate using: openssl rand -base64 16
DB_NAME="<local-db-name>" # pick a db name
DB_AUTH_ON="true"

MONGO_INITDB_ROOT_USERNAME="root"
MONGO_INITDB_ROOT_PASSWORD="<local-db-password>" # generate using: openssl rand -base64 16
MONGO_INITDB_DATABASE="<local-db-name>" # same as DB_NAME

LOG_LEVEL="debug"

FEATURES_BATCH_SIZE=5
FEATURES_BATCH_DELAY=5000 # ms
FEATURES_UNIT_DELAY=800 # ms
FEATURES_CURRENT_PLAYERS_UPDATE_INTERVAL_DELAY=2 # h
FEATURES_UPDATE_INTERVAL_DELAY=12 # h
FEATURES_ITERATION_DELAY=5000 # ms

RUNNER_ITERATION_DELAY=5000 # ms
```

**Step 3.2**, cloud setup:

- create a free tier [MongoDB Atlas account](https://www.mongodb.com/cloud/atlas/register)
- add your IP to the access list in the [MongoDB Atlas dashboard](https://cloud.mongodb.com)
- get the connection uri from the same dashboad under `deployment > database`, then clicking the `connect` button and then `shell`, the uri should look something like this: `mongodb+srv://a-database-name.wml8hrs.mongodb.net/`
- there is a small bug in MongoDB Atlas with the first user which is created when the account is created, so create a second user and use the second user to connect
- store the password in a password manager upon creation of the user otherwise you have to overwrite it
- get the username under `security > database access`
- for the cloud db setup create a `cloud-db.env` in the root folder in the `config` directory:

```bash
# ./config/cloud-db.env
DB_ENDPOINT="<cloud-db-endpoint>" # set accordingly
DB_USERNAME="<cloud-db-username>" # set accordingly
DB_PASSWORD="<cloud-db-password>" # set accordingly
DB_NAME="<cloud-db-name>" # set accordingly
DB_AUTH_ON="true"

MONGO_INITDB_ROOT_USERNAME="<unset>" # not used but must have a value in cloud setup
MONGO_INITDB_ROOT_PASSWORD="<unset>" # not used but must have a value in cloud setup
MONGO_INITDB_DATABASE="<unset>" # not used but must have a value in cloud setup

LOG_LEVEL="debug"

FEATURES_BATCH_SIZE=3
FEATURES_BATCH_DELAY=30000 # ms
FEATURES_UNIT_DELAY=2000 # ms
FEATURES_CURRENT_PLAYERS_UPDATE_INTERVAL_DELAY=2 # h
FEATURES_UPDATE_INTERVAL_DELAY=12 # h
FEATURES_ITERATION_DELAY=30000 # ms

RUNNER_ITERATION_DELAY=30000 # ms
```

## Running the Application

The application is packaged in containers using Docker Compose. The current setup contains the backend and the database. Adding the frontend to the setup is pending.

There are two simple ways to start the containers:

1. NPM Scripts
1. VSCode Tasks

### NPM Scripts

Start application using the local db:

```bash
npm run start:backend:local-db
```

Start application using the cloud db:

```bash
npm run start:backend:cloud-db
```

Stop the application (cloud or local):

```bash
npm stop
```

The rest api is exposed under [http://localhost:3000](http://localhost:3000).

### VSCode Tasks

- open the "Show All Commands" search bar
- type "`run task`" and hit enter
- type "`Docker:`" and select start with local-db or cloud-db or stop
- the task with `debug` in its name is used for the debugging configuration

## Debugging

The debugging configuration is only available for VSCode. You can debug the backend, unit tests and integration tests locally from the VSCode debugger menu by selecting one of the three configurations starting with "`Local: ...`".

For debugging the application in the Docker container select the configuration starting with "`Docker: ...`". This configuration will rebuild the backend image, start the Docker Compose setup with Node.js in debug mode and attach itself to it so that you can set breakpoints in VSCode. When you are finished debugging detach the debugger and the configuration will stop and clean up the containers.

The Docker debugging configuration uses the `local-db.env` file for the db setup. For more info on the debugging configuration check the `launch` and `tasks` sections in `.vscode/steam-game-stats.code-workspace`.
30 changes: 24 additions & 6 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
node_modules
.git
.gitignore
.github
.vscode
.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
26 changes: 15 additions & 11 deletions backend/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Debug App",
"name": "Local: debug backend",
"program": "${workspaceFolder}/src/main.js",
"request": "launch",
"skipFiles": [
Expand All @@ -11,39 +11,43 @@
"type": "node",
"runtimeArgs": [
"--inspect"
],
]
},
{
"name": "Debug Jasmine Unit Tests",
"name": "Local: debug backend unit tests",
"type": "node",
"runtimeArgs": [
"--inspect",
"--no-lazy",
"--no-lazy"
],
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"program": "../node_modules/jasmine/bin/jasmine.js",
"args": ["--config=jasmine-unit.json"],
"args": [
"--config=jasmine-unit.json"
],
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "Debug Jasmine Integration Tests",
"name": "Local: debug backend integration tests",
"type": "node",
"runtimeArgs": [
"--inspect",
"--no-lazy",
"--no-lazy"
],
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"program": "../node_modules/jasmine/bin/jasmine.js",
"args": ["--config=jasmine-integration.json"],
"args": [
"--config=jasmine-integration.json"
],
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
}
"internalConsoleOptions": "openOnSessionStart"
},
]
}
Loading

0 comments on commit 356b185

Please sign in to comment.