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

feat: Docker support #233

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
23 changes: 23 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Ubuntu",
"image": "mcr.microsoft.com/devcontainers/base:noble",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/go:1": {},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers-extra/features/mkcert:1": {}
},
"customizations": {
"vscode": {
"settings": {
"files.watcherExclude": {
"docker/volumes/**": true
}
},
"extensions": [
"golang.go"
]
}
},
"postCreateCommand": "chmod +x ./.devcontainer/post-create && ./.devcontainer/post-create"
}
8 changes: 8 additions & 0 deletions .devcontainer/post-create
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

npm run setup

sed -i "s/^SECRET_KEY=.*/SECRET_KEY=$(openssl rand -hex 12)/" backend/.env

go install github.com/air-verse/air@latest
go install github.com/swaggo/swag/cmd/swag@latest
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.devcontainer/
docker/
frontend/
rankings/
.dockerignore
.env.example
.gitignore
CONTRIBUTING.md
docker-compose.yaml
Dockerfile
FUNDING.yml
LICENSE
package-lock.json
package.json
README.md
RULES.md

# from .gitignore

.env
.vscode
.vim
*.sh
*.txt
*.dem
backend/lphub
backend/tmp
rankings/rankings
rankings/tmp
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DB_PORT=5432
DB_USER=lphub
DB_PASS=lphub
DB_NAME=lphub
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.env
.vscode
.vim
*.sh
*.txt
*.dem
backend/lphub
backend/tmp
rankings/rankings
rankings/tmp
125 changes: 125 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# How to contribute

## Requirements

* [Go]
* [Node/npm]
* [Docker Engine]
* [mkcert]
* [Steam API key]
* [Backblaze Account]

[Go]: https://go.dev/doc/install
[Node/npm]: https://nodejs.org/en/download/package-manager
[Docker Engine]: https://docs.docker.com/engine/install
[mkcert]: https://github.com/FiloSottile/mkcert
[Steam API key]: https://steamcommunity.com/dev/apikey
[Backblaze Account]: https://www.backblaze.com

## Setup

### Local

* `npm run setup`
* Edit `backend/.env`
* `npm run up`
* `npm run frontend` (2nd terminal)
* Add a host entry `127.0.0.1 lp.hub.local` to `C:\Windows\System32\drivers\etc\hosts` or `/etc/hosts`
* Navigate to `https://lp.hub.local/api/v1/` to test the backend
* Navigate to `https://lp.hub.local:3000` to test the frontend

### Using GitHub

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/pektezol/lphub)

* Edit `backend/.env`
* `npm run up`
* Backend development:
* Navigate to `https://<CODESPACES_ID>-443.app.github.dev/api/v1/`
* Frontend development:
* Edit "proxy" field in package.json to point to the backend `<CODESPACES_ID>-443.app.github.dev`
* Add `127.0.0.1 <CODESPACES_ID>-3000.app.github.dev` to `/etc/hosts`
* Run `HOST_DOMAIN=<CODESPACES_ID>-3000.app.github.dev npm run frontend` (2nd terminal)
* Open frontend in the browser (VSCode will incorrectly suggest app.github.dev:3000 so remove the port at the end)
* NOTE: Hot reloading won't work. Use Ctrl+R or F5 instead
* NOTE: The `token` cookie must be copied manually from the backend domain when trying to login

## Config

Configure `backend/.env` file.

|Config|Description|
|---|---|
|SECRET_KEY|Securely generated random secret for login tokens. Example: `openssl rand -hex 16`|
|API_KEY|Steam API key for fetching profile data.|
|B2_BUCKET_NAME|Bucket name from Backblaze.|
|B2_KEY_ID|Key ID from Backblaze.|
|B2_API_KEY|Application key from Backblaze.|
|B2_DOWNLOAD_URL|Bucket download friendly URL from Backblaze.|

Configure `rankings/.env` file.

|Config|Description|
|---|---|
|API_KEY|Steam API key for fetching profile data.|

## Login

* Navigate to frontend
* Log in with Steam
* Navigate to frontend again (port 3000)

## Demo storage

* Create new public Backblaze bucket
* Create new application key giving access to the bucket
* Modify `backend/.env` and set:
* `B2_BUCKET_NAME` name of the created bucket
* `B2_KEY_ID` key ID
* `B2_API_KEY` application key
* `B2_DOWNLOAD_URL` download friendly URL of the bucket
* Get ID from **endpoint** of the bucket e.g. `005` from `s3.us-east-005.backblazeb2.com`
* Get bucket name e.g. `lphub-demos`
* Construct the download URL e.g. `https://f005.backblazeb2.com/file/lphub-demos/`

## Build

Use `npm run build:frontend` to create an optimized build which the proxy will serve.

## Generate rankings locally

* Install [air CLI](https://github.com/air-verse/air)
* Run `npm run rankings` to update files in `./rankings/output`

## Update docs locally

* Install [swag CLI](https://github.com/swaggo/swag)
* Run `npm run docs` to update files in `./backend/docs`

## Scripts

Execute with `npm run <script>`

|Script|Description|
|---|---|
|setup|Developer setup.|
|frontend|Start frontend.|
|rankings|Update rankings files locally.|
|docs|Update swagger files locally.|
|up|Start backend.|
|down|Stop backend.|
|build:frontend|Create build that gets served from the backend.|
|build:image|Build backend image.|
|reload|Reload backend.|
|sv:debug|Get server shell.|
|sv:restart|Restart server.|
|sv:stop|Stop server.|
|proxy:debug|Get proxy shell.|
|proxy:restart|Restart proxy.|
|proxy:stop|Stop proxy.|
|db|Connect to database.|
|db:debug|Get database server shell|
|db:restart|Restart database.|
|db:stop|Stop database.|
|db:dump|Dump database backup.|
|db:dump:raw|Dump database without compression.|
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:1.23.0-alpine3.20 AS lphub

WORKDIR /app

COPY backend .

RUN go install github.com/air-verse/air@latest
RUN go mod download

CMD ["air"]
14 changes: 7 additions & 7 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PORT=
PORT=8001
SECRET_KEY=
API_KEY=
ENV=
DB_HOST=
DB_PORT=
DB_USER=
DB_PASS=
DB_NAME=
ENV=DEV
DB_HOST=lphub.database
DB_PORT=5432
DB_USER=lphub
DB_PASS=lphub
DB_NAME=lphub
B2_BUCKET_NAME=
B2_KEY_ID=
B2_API_KEY=
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
server:
image: lphub:latest
build:
context: .
target: lphub
container_name: lphub-server
restart: unless-stopped
links:
- database:lphub.database
volumes:
- ./backend:/app
- ./rankings:/rankings:ro
database:
image: postgres:15.4
container_name: lphub-db
restart: unless-stopped
ports:
- "127.0.0.1:${DB_PORT}:${DB_PORT}"
environment:
POSTGRES_USER: "${DB_USER}"
POSTGRES_PASSWORD: "${DB_PASS}"
POSTGRES_DB: "${DB_NAME}"
volumes:
- ./docker/volumes/initdb:/docker-entrypoint-initdb.d
- ./docker/volumes/postgresql-data:/var/lib/postgresql/data
proxy:
image: nginx:stable-alpine3.20
container_name: lphub-proxy
links:
- server:lphub.server
ports:
- 80:80
- 443:443
environment:
- NGINX_HOST=lp.hub.local
- NGINX_PORT=443
volumes:
- ./docker/volumes/ssl:/ssl:ro
- ./docker/volumes/nginx:/etc/nginx/templates
- ./docker/volumes/logs/nginx:/var/log/nginx:rw
- ./docker/volumes/build:/build:ro
- ./backend/docs/swagger.json:/docs/swagger.json:ro
1 change: 1 addition & 0 deletions docker/volumes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
postgresql/
2 changes: 2 additions & 0 deletions docker/volumes/build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading