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

Cleanup and Github warnings fix #33

Merged
merged 6 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ jobs:
needs: test
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy using ssh
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_KEY }}
port: 22
script: |
cd ~/Git/pokerio-server
git pull origin main
yarn
yarn production
- name: Deploy using ssh
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_KEY }}
port: 22
script: |
cd ~/Git/pokerio-server
git pull origin main
yarn
yarn production
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@
[![Tests](https://github.com/poker-io/pokerio-server/actions/workflows/test.yml/badge.svg)](https://github.com/poker-io/pokerio-server/blob/main/.github/workflows/test.yml)
[![codecov](https://codecov.io/gh/poker-io/pokerio-server/branch/main/graph/badge.svg?token=4QCZNOWFZJ)](https://codecov.io/gh/poker-io/pokerio-server)

## Building
## Requirements
Kwasow marked this conversation as resolved.
Show resolved Hide resolved

Run `yarn install`.
Make sure you're using node version 18 or newer. To install the appropriate
version follow the instructions on nodejs.org.

## Running
To install dependencies run:

```
yarn install
```

Remember to add the `serviceAccount.json` file from Firebase under `src/` as
well as a `secrets.ts` file that contains login credentials for the database.
It should be similar to this:

```ts
export const user = 'pokerio-user'
export const password = 'pokerio-password'

First you have to add firebase service account json file in src/ as serviceAccount.json.
```

## Running

Run `yarn start`.

The app will attempt to connect to a Postgres database using the details
provided in `databaseConnection.ts`.

## Testing

We use Jest for testing. To run it, simply type `yarn test`.
Make sure your Node's version is > v18 (`nvm install v18.15.0` to install appropriate version).
11 changes: 2 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express from 'express'
import { isCelebrateError } from 'celebrate'

import { rateLimit } from 'express-rate-limit'
import joinGame from './routes/joinGame'
import createGame from './routes/createGame'
import { rateLimiter } from './utils/rateLimiter'

export const app = express()
export const port = 42069
Expand Down Expand Up @@ -38,17 +38,10 @@ const errorHandling = (error, req, res, next) => {
return next(error)
}

const rateLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100, // limit each IP to 100 requests per windowMs
})

app.get('/status', (req, res) => {
app.get('/status', rateLimiter, (req, res) => {
res.send('OK')
})

app.use(rateLimiter)

app.use(joinGame)

app.use(createGame)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app, port } from './app.js'
import { databaseInit } from './databaseConnection'
import { databaseInit } from './utils/databaseConnection'

databaseInit()
.then(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/routes/createGame.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { getClient } from '../databaseConnection'
import { getClient } from '../utils/databaseConnection'
import { celebrate, Joi, Segments } from 'celebrate'
import {
startingFundsDefault,
smallBlindDefault,
type newGameInfo,
} from '../app'
import { verifyFCMToken } from '../firebase'
import { verifyFCMToken } from '../utils/firebase'

import express, { type Router } from 'express'
import { rateLimiter } from '../utils/rateLimiter'
const router: Router = express.Router()

router.get(
'/createGame',
rateLimiter,
celebrate({
[Segments.QUERY]: Joi.object().keys({
creatorToken: Joi.string()
Expand Down
6 changes: 4 additions & 2 deletions src/routes/joinGame.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { getClient } from '../databaseConnection'
import { getClient } from '../utils/databaseConnection'
import { celebrate, Joi, Segments } from 'celebrate'
import sha256 from 'crypto-js/sha256'
import { getMessaging } from 'firebase-admin/messaging'
import { type gameSettings } from '../app'
import { verifyFCMToken } from '../firebase'
import { verifyFCMToken } from '../utils/firebase'
import express, { type Router } from 'express'
import { rateLimiter } from '../utils/rateLimiter'

const router: Router = express.Router()

router.get(
'/joinGame',
rateLimiter,
celebrate({
[Segments.QUERY]: Joi.object().keys({
playerToken: Joi.string().required().min(1).max(250).label('playerToken'),
Expand Down
2 changes: 1 addition & 1 deletion src/tests/createGame.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 { getClient } from '../databaseConnection'
import { getClient } from '../utils/databaseConnection'

test('Create game, wrong args', (done) => {
request(app).get('/createGame/?creatorToken=-1').expect(400).end(done)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/databaseConnection.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { databaseInit } from '../databaseConnection'
import { databaseInit } from '../utils/databaseConnection'

test('Database connection', async () => {
await expect(databaseInit()).resolves.not.toThrow()
Expand Down
2 changes: 1 addition & 1 deletion src/tests/joinGame.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 { getClient } from '../databaseConnection'
import { getClient } from '../utils/databaseConnection'
import type { gameSettings } from '../app'
import sha256 from 'crypto-js/sha256'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pg from 'pg'
import { user, password } from './secrets'
import { user, password } from '../secrets'
// pg is a CommonJS module, so we have to do it this way for the import to work
export const { Client } = pg

Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/utils/rateLimiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import rateLimit from 'express-rate-limit'

export const rateLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100, // limit each IP to 100 requests per windowMs
})