Skip to content

Commit

Permalink
Database setup (#25)
Browse files Browse the repository at this point in the history
Some setup is required.

1. Add a file `src/secrets.ts` with the contents:
```ts
export const user = '<your username>'
export const password = '<your password>'
```
2. Before running `yarn start` setup tunneling with:
```
ssh -L 5432:lkdb:5432 ab123456@students.mimuw.edu.pl
```
3. Run `yarn start`

If everything worked correctly, you should see two tables created under
your account: `Games` and `Players`.

Closes #4
  • Loading branch information
Kwasow authored Mar 23, 2023
1 parent 069ac9e commit 4d6dbbd
Show file tree
Hide file tree
Showing 14 changed files with 715 additions and 132 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Setup secrets
run: |
echo "export const user = '';export const password = '';" > src/secrets.ts
- run: yarn install
- uses: sibiraj-s/action-eslint@v2
with:
Expand Down
17 changes: 5 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
# This is a basic workflow to help you get started with Actions

name: CI (build)

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [main]
pull_request:
branches: [main]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

steps:
# Checks out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 18

- name: add firebase service account json file
- name: Setup secrets
run: |
echo "export const user = '';export const password = '';" > src/secrets.ts
- name: Add firebase service account json file
env:
JSON: ${{ secrets.FIREBASE_SERVICEACCOUNTJSON }}
run: echo $JSON > src/serviceAccount.json
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Setup secrets
run: |
echo "export const user = '';export const password = '';" > src/secrets.ts
- run: yarn install
- run: yarn pretty-check
29 changes: 17 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
# This is a basic workflow to help you get started with Actions

name: CI (test)

# Controls when the workflow will run
on:
push:
branches: [main]
pull_request:
branches: [main]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
test:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 18

- name: add firebase service account json file
- name: Setup secrets
env:
USER: ${{ secrets.KWASOW_USERNAME }}
PASSWORD: ${{ secrets.KWASOW_PASSWORD }}
run: |
echo "export const user = '$USER';export const password = '$PASSWORD';" > src/secrets.ts
- name: Setup SSH tunnel
env:
USER: ${{ secrets.KWASOW_USERNAME }}
PASSWORD: ${{ secrets.KWASOW_PASSWORD }}
run: |
mkdir -p ~/.ssh
echo "StrictHostKeyChecking no" >> ~/.ssh/config
sshpass -p $PASSWORD ssh -fN -L 5432:lkdb:5432 $USER@students.mimuw.edu.pl
- name: Add firebase service account json file
env:
JSON: ${{ secrets.FIREBASE_SERVICEACCOUNTJSON }}
run: echo $JSON > src/serviceAccount.json
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dist/
node_modules/
coverage/
src/secrets.ts
src/serviceAccount.json
yarn-error.log
yarn-error.log
3 changes: 1 addition & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn pretty
yarn lint
npx lint-staged
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ package.json
tsconfig.json
yarn.lock
yarn-error.log
coverage/
coverage/
src/secrets.ts
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
"main": "src/index.ts",
"repository": "git@github.com:poker-io/pokerio-server.git",
"author": "Karol Wąsowski <wasowski02@protonmail.com>",
"type": "module",
"license": "MIT",
"scripts": {
"start": "tsc && node dist/server.js",
"start": "tsc && node dist/index.js",
"build": "tsc",
"test": "jest src/tests --coverage --config package.json",
"lint": "eslint --ext '.js,.ts,.tsx' src/",
"pretty": "yarn prettier --write .",
"pretty-check": "yarn prettier --check .",
"pretty-check": "yarn prettier --check src/",
"prepare": "husky install"
},
"devDependencies": {
Expand All @@ -25,22 +24,32 @@
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"husky": "^8.0.0",
"lint-staged": "^13.2.0",
"prettier": "2.8.4",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"typescript": "4.9.5"
},
"dependencies": {
"@types/pg": "^8.6.6",
"express": "^4.18.2",
"firebase-admin": "^11.5.0",
"jest": "^29.5.0"
"jest": "^29.5.0",
"pg": "^8.10.0"
},
"jest": {
"collectCoverage": true,
"testEnvironment": "node",
"collectCoverageFrom": [
"src/*.ts"
],
"coveragePathIgnorePatterns": [
"src/secrets.ts"
],
"preset": "ts-jest"
},
"lint-staged": {
"*.(js|ts|tsx)": "yarn lint",
"src/*": "prettier --write"
}
}
78 changes: 78 additions & 0 deletions src/databaseConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pg from 'pg'
import { user, password } from './secrets'
// pq is a CommonJS module, so we have to do it this way for the import to work
const { Client } = pg

const client = new Client({
user,
password,
database: 'bd',
port: 5432,
host: 'localhost',
})

export async function databaseInit(): Promise<void> {
let success = true

try {
// Connect
await client.connect()

// Create the tables
// This will fail if tables already exist, but we don't care
try {
await client.query(
`CREATE TABLE IF NOT EXISTS Players (
id SERIAL UNIQUE NOT NULL,
token VARCHAR(250) NOT NULL PRIMARY KEY,
nickname VARCHAR(20) NOT NULL,
turn BIGINT NOT NULL,
game_id VARCHAR(6),
card1 VARCHAR(3),
card2 VARCHAR(3),
funds BIGINT,
bet BIGINT
)`
)
} catch (err) {
console.error(err)
}

try {
await client.query(
`CREATE TABLE IF NOT EXISTS Games (
game_id VARCHAR(6) PRIMARY KEY,
game_master SERIAL REFERENCES Players(id) NOT NULL,
card1 VARCHAR(3),
card2 VARCHAR(3),
card3 VARCHAR(3),
card4 VARCHAR(3),
card5 VARCHAR(3),
game_round BIGINT DEFAULT 0 NOT NULL,
starting_funds BIGINT NOT NULL,
small_blind BIGINT NOT NULL,
small_blind_who SERIAL REFERENCES Players(id) NOT NULL,
current_table_value BIGINT,
current_player SERIAL REFERENCES Players(id) NOT NULL
)`
)
} catch (err) {
console.error(err)
}
} catch (err) {
console.error(err)
success = false
} finally {
try {
await client.end()
} catch (err) {
console.error(err)
}
}

if (success) {
await Promise.resolve()
} else {
await Promise.reject(new Error('Failed to connect to database'))
}
}
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express'
import { app, port } from './app.js'
import { databaseInit } from './databaseConnection'
import admin from 'firebase-admin'
import { readFileSync } from 'fs'

Expand All @@ -14,13 +15,15 @@ admin.initializeApp({
}),
})

const app = express()
export const port = 42069

app.get('/test', (req, res) => {
res.send('Hello from typescript express!')
})

app.listen(port, () => {
console.log(`[server]: Server is running at localhost:${port}`)
})
databaseInit()
.then(() => {
// Only start listening if database connection was successful
app.listen(port, () => {
console.log(`[server]: Server is running at localhost:${port}`)
})
})
.catch(() => {
// Don't start the server in case of an error
console.log('Failed to init database')
process.exit(1)
})
5 changes: 0 additions & 5 deletions src/server.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/tests/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app } from '../app'

import request from 'supertest'
import { databaseInit } from '../databaseConnection'

test('Simple test', (done) => {
request(app)
Expand All @@ -10,3 +10,7 @@ test('Simple test', (done) => {
.expect('Hello from typescript express!')
.end(done)
})

test('Database connection', async () => {
await expect(databaseInit()).resolves.not.toThrow()
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"strictNullChecks": true
"strictNullChecks": true,
"module": "CommonJS"
},
"lib": ["es2015"],
"include": [
Expand Down
Loading

0 comments on commit 4d6dbbd

Please sign in to comment.