From e96874ac5a36a2a564bd1639a8963f113b7cf547 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Mon, 10 Jun 2024 23:51:39 +0200 Subject: [PATCH] feat: Docker development --- .gitignore | 96 +----------------------------------------- README.md | 29 ++++++++++++- dev.sh | 39 +++++++++++++++++ docker-compose.yml | 36 ++++++++++++++++ vingo/.air.toml | 51 ++++++++++++++++++++++ vingo/.gitignore | 2 + vingo/Dockerfile.dev | 10 +++++ vingo/README.md | 2 + vinscant/.gitignore | 6 +++ vinscant/README.md | 12 ++++-- vinvoor/Dockerfile | 22 ++++++++++ vinvoor/Dockerfile.dev | 7 +++ vinvoor/README.md | 5 +++ vinvoor/nginx.conf | 9 ++++ 14 files changed, 225 insertions(+), 101 deletions(-) create mode 100755 dev.sh create mode 100644 docker-compose.yml create mode 100644 vingo/.air.toml create mode 100644 vingo/.gitignore create mode 100644 vingo/Dockerfile.dev create mode 100644 vinscant/.gitignore create mode 100644 vinvoor/Dockerfile create mode 100644 vinvoor/Dockerfile.dev create mode 100644 vinvoor/nginx.conf diff --git a/.gitignore b/.gitignore index ae970e8..1343767 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,3 @@ -.config -*.o -*.pyc - -# gtags -GTAGS -GRTAGS -GPATH - # emacs .dir-locals.el @@ -21,52 +12,6 @@ GPATH # MacOS directory files .DS_Store -# cache dir -.cache/ - -# Doc build artifacts -docs/_build/ -docs/doxygen_sqlite3.db - -# Downloaded font files -docs/_static/DejaVuSans.ttf -docs/_static/NotoSansSC-Regular.otf - -# Components Unit Test Apps files -components/**/build/ -components/**/build_*_*/ -components/**/sdkconfig -components/**/sdkconfig.old - -# Example project files -examples/**/build/ -examples/**/build_*_*/ -examples/**/sdkconfig -examples/**/sdkconfig.old - -# Unit test app files -tools/unit-test-app/build -tools/unit-test-app/build_*_*/ -tools/unit-test-app/sdkconfig -tools/unit-test-app/sdkconfig.old - -# test application build files -tools/test_apps/**/build/ -tools/test_apps/**/build_*_*/ -tools/test_apps/**/sdkconfig -tools/test_apps/**/sdkconfig.old - -TEST_LOGS/ -build_summary_*.xml - -# gcov coverage reports -*.gcda -*.gcno -coverage.info -coverage_report/ - -test_multi_heap_host - # VS Code Settings .vscode/ @@ -78,44 +23,5 @@ test_multi_heap_host *.sublime-project *.sublime-workspace -# Clion IDE CMake build & config +# IDEA files .idea/ -cmake-build-*/ - -# Results for the checking of the Python coding style and static analysis -.mypy_cache -flake8_output.txt - -# ESP-IDF default build directory name -build - -# lock files for examples and components -dependencies.lock - -# managed_components for examples -managed_components - -# pytest log -pytest_embedded_log/ -list_job*.txt -size_info*.txt -XUNIT_RESULT*.xml - -# clang config (for LSP) -.clangd - -# Vale -.vale/styles/* - -# pio -.pio - -# sqlite -*.db - -.env - -# vinscant -key.txt -mfrc522.py -webrepl_cli.py diff --git a/README.md b/README.md index 76e90b1..06b6f1c 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,43 @@ # Zeus Scan Syst(e)em -*Formerly called Vincent* + +_Formerly called Vincent_ Check-in in the the kelder kelder (Will) scan a badge at the scanner (vinscant) which registers its serial number at the webserver (vingo). Goals: + - Support a check-in a day, keeping track of which days you have visited the kelder - Support check-in and check-out, keeping track of how many hours(, minutes(, seconds)) you have been in the kelder - Cool stats :D Secret goals: + - Streaks - Data - More Data - Skins -- Battlepass \ No newline at end of file +- Battlepass + +## Structure + +- `Vinscant` -> Scanner +- `Vingo` -> Backend +- `Vinvoor` -> Frontend + +## How to run (for development) + +### Easy & Quick + +- Install Docker and Docker Compose +- Run the script `./dev.sh` with optional flags: + - `-b`: Show the output of the backend. + - `-f`: Show the output of the frontend. + - If both flags or no flags are provided, the output of both the backend and frontend are shown. + +The backend is accessible at `localhost:3000`, and the frontend at `localhost:5173`. + +### Manual + +- Each part has it's own `README.md` with instructions on how to run. diff --git a/dev.sh b/dev.sh new file mode 100755 index 0000000..1eff078 --- /dev/null +++ b/dev.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Parse input + +backend=false +frontend=false + +while getopts 'bf' flag; do + case "${flag}" in + b) backend=true ;; + f) frontend=true ;; + *) echo "Unexpected option ${flag}" ;; + esac +done + +# Check for the required files + +if [ ! -f vingo/.env ]; then + cp vingo/dev.env vingo/.env +fi + +# Start the docker containers + +docker-compose -f docker-compose.yml up -d + +echo "-------------------------------------" +echo "Following logs..." +echo "Press CTRL + C to stop all containers" +echo "-------------------------------------" + +if [ "$backend" = true ] && [ "$frontend" = false ]; then + docker-compose -f docker-compose.yml logs -f zess-backend +elif [ "$backend" = false ] && [ "$frontend" = true ]; then + docker-compose -f docker-compose.yml logs -f zess-frontend +else + docker-compose -f docker-compose.yml logs -f zess-backend zess-frontend +fi + +docker-compose -f docker-compose.yml stop diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4a052b7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +services: + zess-db: + image: postgres + environment: + - POSTGRES_PASSWORD=zess + - POSTGRES_USER=postgres + - POSTGRES_DB=zess + expose: + - 5432 + network_mode: host + + zess-backend: + build: + context: vingo + dockerfile: Dockerfile.dev + expose: + - 4000 + volumes: + - ./vingo:/backend + - backend-cache:/root/.cache/go-build + depends_on: + - zess-db + network_mode: host + + zess-frontend: + build: + context: vinvoor + dockerfile: Dockerfile.dev + expose: + - 5173 + volumes: + - ./vinvoor:/frontend + network_mode: host + +volumes: + backend-cache: diff --git a/vingo/.air.toml b/vingo/.air.toml new file mode 100644 index 0000000..58fff2a --- /dev/null +++ b/vingo/.air.toml @@ -0,0 +1,51 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ." + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/vingo/.gitignore b/vingo/.gitignore new file mode 100644 index 0000000..a703a4a --- /dev/null +++ b/vingo/.gitignore @@ -0,0 +1,2 @@ +.env +tmp/ diff --git a/vingo/Dockerfile.dev b/vingo/Dockerfile.dev new file mode 100644 index 0000000..94460f4 --- /dev/null +++ b/vingo/Dockerfile.dev @@ -0,0 +1,10 @@ +FROM golang:1.22.1-alpine3.19 + +WORKDIR /backend + +RUN go install github.com/air-verse/air@latest +COPY .air.toml . + +COPY go.mod go.sum ./ + +CMD go mod tidy && air -c .air.toml diff --git a/vingo/README.md b/vingo/README.md index 3b0a440..b0357ea 100644 --- a/vingo/README.md +++ b/vingo/README.md @@ -1,4 +1,5 @@ # Vingo + The webserver that keeps track of the scans :D Register a scan by posting `card_serial;scan_key` to the `/scans` endpoint. @@ -7,6 +8,7 @@ Register a scan by posting `card_serial;scan_key` to the `/scans` endpoint. To register a card, click the "Start registering a new card" button in the cards view, after which the server will register the next scanned card for the user that initiated the request. Only 1 user can register a card at a time. ## How to run (for development) + - install go - install docker - `docker run --name zess-postgres -e POSTGRES_PASSWORD=zess -d -p 5432:5432 postgres` diff --git a/vinscant/.gitignore b/vinscant/.gitignore new file mode 100644 index 0000000..3e652d3 --- /dev/null +++ b/vinscant/.gitignore @@ -0,0 +1,6 @@ +key.txt +mfrc522.py +webrepl_cli.py + +# ESP-IDF default build directory name +build diff --git a/vinscant/README.md b/vinscant/README.md index d4f468a..e0e4779 100644 --- a/vinscant/README.md +++ b/vinscant/README.md @@ -1,10 +1,12 @@ ono # Hardware: + - ESP32-S2 - RFID-RC522 Connect RFID-RC522 Rfid reader on these pins: + ``` SDA: 34 MOSI: 35 @@ -14,19 +16,21 @@ RST: 0 ``` # Setup: + - If you're on windows and the board is not detected: install the ESP32-S2 toolchain from https://dl.espressif.com/dl/esp-idf/ - - Or just install the usb chip driver for your board (eg. for CP2102N: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads) + - Or just install the usb chip driver for your board (eg. for CP2102N: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads) - Install micropython - - Get binary for correct chip. Current: https://micropython.org/download/ESP32_GENERIC_S2/ - - install using commands on that website + - Get binary for correct chip. Current: https://micropython.org/download/ESP32_GENERIC_S2/ + - install using commands on that website - connect to serial - connect to wifi and setup webrepl (see https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html) - get `webrepl_cli.py` from https://github.com/micropython/webrepl - copy `boot.py`, `main.py` and `key.txt` (with the correct key set on vingo) to the microcontroller using `upload_file.sh` -- download https://github.com/danjperron/micropython-mfrc522/blob/master/mfrc522.py and copy it to the microcontroller as well +- download https://github.com/danjperron/micropython-mfrc522/blob/master/mfrc522.py and copy it to the microcontroller as well - beep boop # Future additions + - Beeps - Boops - Status light based on server response diff --git a/vinvoor/Dockerfile b/vinvoor/Dockerfile new file mode 100644 index 0000000..c48e4cd --- /dev/null +++ b/vinvoor/Dockerfile @@ -0,0 +1,22 @@ +FROM node:22.2.0-alpine3.19 as build-stage + +WORKDIR /app + +COPY package.json yarn.lock ./ + +RUN yarn install + +COPY ./ . + +RUN yarn run build + + +FROM nginx:alpine-slim as production-stage + +EXPOSE 3000 + +RUN mkdir /app + +COPY nginx.conf /etc/nginx/conf.d/default.conf + +COPY --from=build-stage /app/dist /app diff --git a/vinvoor/Dockerfile.dev b/vinvoor/Dockerfile.dev new file mode 100644 index 0000000..5bb0f50 --- /dev/null +++ b/vinvoor/Dockerfile.dev @@ -0,0 +1,7 @@ +FROM node:22.2.0-alpine3.19 + +WORKDIR /frontend + +COPY package.json yarn.lock ./ + +CMD yarn install && yarn run host diff --git a/vinvoor/README.md b/vinvoor/README.md index 6366e9e..dad3819 100644 --- a/vinvoor/README.md +++ b/vinvoor/README.md @@ -10,3 +10,8 @@ That's what this does! - Install dependencies `yarn install` - Start the frontend `yarn run dev` - Visit: http://localhost:5173 + +## How to run (for production) + +- Build the image `docker build -t vinvoor:latest .`. +- Run the image `docker run -p 80:3000 vinvoor:latest`. diff --git a/vinvoor/nginx.conf b/vinvoor/nginx.conf new file mode 100644 index 0000000..cdb8eaa --- /dev/null +++ b/vinvoor/nginx.conf @@ -0,0 +1,9 @@ +server { + listen 3000; + + location / { + root /app; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } +}