Skip to content

Commit 70bf7a8

Browse files
authored
Merge pull request #26 from codewithkyle/docker
Migrated to Docker
2 parents 8d13272 + 21b85ed commit 70bf7a8

File tree

16 files changed

+354
-280
lines changed

16 files changed

+354
-280
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.env
2+
.git
3+
.github
4+
node_modules/
5+
dist/

.github/workflows/deploy-docker.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Build and Deploy Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Setup Node and NPM
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: 18.14.0
19+
20+
- name: Install Client NPM Packages
21+
run: npm ci
22+
working-directory: client
23+
24+
- name: Compile Client
25+
run: npm run production
26+
working-directory: client
27+
28+
- name: Install WSS NPM Packages
29+
run: npm ci
30+
working-directory: wss
31+
32+
- name: Compile WSS
33+
run: npm run production
34+
working-directory: wss
35+
36+
- name: Build HTTP Docker Image
37+
run: docker build -f Dockerfile.http -t codewithkyle/tabletopper-http:prod .
38+
39+
- name: Save HTTP Docker Image as Tarball
40+
run: docker save -o tabletopper-http.tar codewithkyle/tabletopper-http:prod
41+
42+
- name: Build WSS Docker Image
43+
run: docker build -f Dockerfile.wss -t codewithkyle/tabletopper-wss:prod .
44+
45+
- name: Save WSS Docker Image as Tarball
46+
run: docker save -o tabletopper-wss.tar codewithkyle/tabletopper-wss:prod
47+
48+
- name: Load SSH Key
49+
uses: webfactory/[email protected]
50+
with:
51+
ssh-private-key: ${{ secrets.SSHKEY }}
52+
53+
- name: Deploy
54+
run: scp -o StrictHostKeyChecking=no ./tabletopper-http.tar ./tabletopper-wss.tar ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/
55+
56+
- name: Post Deployment
57+
uses: appleboy/ssh-action@master
58+
with:
59+
host: ${{ secrets.HOST }}
60+
USERNAME: ${{ secrets.USERNAME }}
61+
PORT: 22
62+
KEY: ${{ secrets.SSHKEY }}
63+
script: docker load -i ~/tabletopper-wss.tar && docker load -i ~/tabletopper-http.tar
64+
65+
- name: Purge cache
66+
uses: nathanvaughn/actions-cloudflare-purge@master
67+
if: success()
68+
with:
69+
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
70+
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
71+

.github/workflows/deploy.yml

Lines changed: 0 additions & 78 deletions
This file was deleted.

Dockerfile.http

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Step 1: Use an official Go image to build the Go application
2+
FROM golang:1.22-alpine AS builder
3+
4+
# Step 2: Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Step 3: Copy the Go modules and dependencies
8+
COPY ./server/. ./
9+
10+
# Step 4: Download necessary Go dependencies
11+
RUN go mod download
12+
13+
# Step 6: Build the Go application
14+
RUN go build -ldflags="-s -w" -o tabletopper .
15+
16+
# Step 7: Create a small image to run the application
17+
FROM alpine:latest
18+
19+
# Step 8: Set the working directory in the final image
20+
WORKDIR /root/
21+
22+
# Step 9: Copy the Go binary from the builder stage
23+
COPY --from=builder /app/tabletopper .
24+
25+
COPY ./server/views ./views
26+
27+
COPY ./client/public ./public
28+
29+
# Step 10: Expose the port that the app will run on
30+
EXPOSE 3000
31+
32+
# Step 11: Command to run the Go app
33+
CMD ["./tabletopper"]

Dockerfile.wss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use an official Node.js runtime as a parent image
2+
FROM node:18
3+
4+
# Set the working directory inside the container
5+
WORKDIR /usr/src/app
6+
7+
ENV PATH /usr/src/app/node_modules/.bin:$PATH
8+
9+
# Copy the package.json and package-lock.json files into the container
10+
COPY ./wss/. .
11+
12+
# Install the project dependencies
13+
RUN npm ci
14+
15+
RUN npm run production
16+
17+
# Expose the port that the app runs on (assuming the app runs on port 3000)
18+
EXPOSE 8080
19+
20+
# Start the Node.js application
21+
CMD ["npm", "start"]
22+

client/build/prod.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

client/public/static/config.example

Lines changed: 0 additions & 3 deletions
This file was deleted.

client/src/bootstrap.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import alerts from "~brixi/controllers/alerts";
22

33
(async () => {
4-
// @ts-ignore
5-
const { ENV } = await import("/static/config.js");
6-
if (ENV === "production"){
4+
if (location.host == "tabletopper.app"){
75
let update = false;
86
await new Promise<void>(resolve => {
97
navigator.serviceWorker.register("/service-worker.js", { scope: "/" });

client/src/controllers/ws.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ let socket:WebSocket;
55
let connected = false;
66
let wasReconnection = false;
77

8+
let SOCKET_URL:string = "ws://ws.tabletopper.local";
9+
if (location.host == "tabletopper.app") {
10+
SOCKET_URL = "wss://ws.tabletopper.app";
11+
}
12+
813
let offlineQueue = [];
914
function flushOfflineQueue(){
1015
while (offlineQueue.length > 0){
@@ -17,8 +22,7 @@ async function connect() {
1722
if (connected){
1823
return;
1924
}
20-
// @ts-expect-error
21-
const { SOCKET_URL } = await import("/static/config.js");
25+
2226
socket = new WebSocket(SOCKET_URL);
2327
socket.addEventListener("message", async (event) => {
2428
try {

compose.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
services:
2+
reverse-proxy:
3+
image: traefik:v3.1
4+
command:
5+
- "--providers.docker"
6+
ports:
7+
- "80:80"
8+
volumes:
9+
- /var/run/docker.sock:/var/run/docker.sock
10+
networks:
11+
- app-network
12+
tabletopper:
13+
build:
14+
context: ./
15+
dockerfile: Dockerfile.http
16+
labels:
17+
- "traefik.http.routers.tabletopper.rule=Host(`tabletopper.local`)"
18+
volumes:
19+
- .:/app
20+
env_file: "./server/.env"
21+
depends_on:
22+
- mysql
23+
- reverse-proxy
24+
- redis
25+
networks:
26+
- app-network
27+
wss:
28+
build:
29+
context: ./
30+
dockerfile: Dockerfile.wss
31+
labels:
32+
- "traefik.http.routers.wss.rule=Host(`ws.tabletopper.local`)"
33+
depends_on:
34+
- mysql
35+
- reverse-proxy
36+
- redis
37+
networks:
38+
- app-network
39+
mysql:
40+
image: mysql:8.0
41+
environment:
42+
MYSQL_ROOT_PASSWORD: "password"
43+
MYSQL_DATABASE: "tabletopper"
44+
MYSQL_USER: "ttadmin"
45+
MYSQL_PASSWORD: "password"
46+
volumes:
47+
- mysql_data:/var/lib/mysql
48+
ports:
49+
- "3306:3306"
50+
networks:
51+
- app-network
52+
redis:
53+
image: redis:7
54+
ports:
55+
- "6379:6379"
56+
volumes:
57+
- redis-data:/data
58+
volumes:
59+
mysql_data:
60+
redis-data:
61+
networks:
62+
app-network:
63+
driver: bridge
64+

0 commit comments

Comments
 (0)