Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database setup #25

Merged
merged 34 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
76f05de
Added oracledb package
Kwasow Mar 21, 2023
5f79090
Added init to index.ts
Kwasow Mar 21, 2023
f1d65d3
Moved credentials out of databaseConnection.ts
Kwasow Mar 22, 2023
ea019d6
Added secrets to gitignore
Kwasow Mar 22, 2023
234c88d
Removed secrets from repo
Kwasow Mar 22, 2023
efa2ef9
Minor fixes
Kwasow Mar 22, 2023
76b74b3
Update CI
Kwasow Mar 22, 2023
03a1f92
Update CI
Kwasow Mar 22, 2023
59a188a
Update CI
Kwasow Mar 22, 2023
b4b2d01
Added print for debugging workflow
Kwasow Mar 22, 2023
b9121e3
Removed debug print
Kwasow Mar 22, 2023
b1fe98c
Updated test CI
Kwasow Mar 22, 2023
7a9fedc
Made server fail to start if database connection cannot be established
Kwasow Mar 22, 2023
3af45c4
Made tests run in docker container
Kwasow Mar 23, 2023
eb98562
try installing oracle libraries
Kwasow Mar 23, 2023
9cdac37
More testing
Kwasow Mar 23, 2023
776169b
more debugging info
Kwasow Mar 23, 2023
6943bc4
tried fix
Kwasow Mar 23, 2023
afb3ac1
tried fix
Kwasow Mar 23, 2023
cbfaf98
Replaced oracle with postgres
Kwasow Mar 23, 2023
f88f45c
Updated queries for postgres
Kwasow Mar 23, 2023
b63535b
Fixed connection to postgres
Kwasow Mar 23, 2023
839a35b
Updated CI
Kwasow Mar 23, 2023
a25a419
fixed ci?
Kwasow Mar 23, 2023
1a2ddd8
Fixed test CI
Kwasow Mar 23, 2023
038c65f
More fixes
Kwasow Mar 23, 2023
8560208
CI Fix
Kwasow Mar 23, 2023
99c27d3
Merged main
Kwasow Mar 23, 2023
2d90a5d
Added missing files from merge
Kwasow Mar 23, 2023
422552b
Fixed weird stuff after merge
Kwasow Mar 23, 2023
f14e112
Added secrets to jest ignore pattern
Kwasow Mar 23, 2023
711a517
Added lint staged
Kwasow Mar 23, 2023
f9e4f01
Added tests
Kwasow Mar 23, 2023
c0e4ee3
Removed duplicate file from gitignore
Kwasow Mar 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Kwasow marked this conversation as resolved.
Show resolved Hide resolved
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