Skip to content

Commit d597a2c

Browse files
committed
feat!: serve frontent with elysia
BREAKING CHANGE: The 2 previous containers have been merged into one
1 parent 0e22f31 commit d597a2c

10 files changed

Lines changed: 87 additions & 80 deletions

File tree

.github/workflows/docker-images.yml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,12 @@ on:
1010
env:
1111
REGISTRY: ghcr.io
1212
CN_REGISTRY: docker.cnb.cool
13-
13+
IMAGE_NAME: em-geeklab/nexusgate
14+
CN_IMAGE_NAME: em-geeklab/nexusgate/server
1415
jobs:
1516
build-images:
1617
name: Build and push multi-arch images
1718
runs-on: ubuntu-24.04
18-
strategy:
19-
fail-fast: false
20-
matrix:
21-
image:
22-
- name: backend
23-
dockerfile: backend.Dockerfile
24-
image_name: em-geeklab/nexus-gate-server
25-
cn_image_name: em-geeklab/nexusgate/server
26-
- name: frontend
27-
dockerfile: frontend.Dockerfile
28-
image_name: em-geeklab/nexus-gate-web
29-
cn_image_name: em-geeklab/nexusgate/web
3019
permissions:
3120
contents: read
3221
packages: write
@@ -57,8 +46,8 @@ jobs:
5746
uses: docker/metadata-action@v5
5847
with:
5948
images: |
60-
${{ env.REGISTRY }}/${{ matrix.image.image_name }}
61-
${{ github.event_name == 'release' && format('{0}/{1}', env.CN_REGISTRY, matrix.image.cn_image_name) || '' }}
49+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
50+
${{ github.event_name == 'release' && format('{0}/{1}', env.CN_REGISTRY, env.CN_IMAGE_NAME) || '' }}
6251
- name: Build and push Docker image
6352
id: push
6453
uses: docker/build-push-action@v6
@@ -67,11 +56,10 @@ jobs:
6756
push: true
6857
tags: ${{ steps.meta.outputs.tags }}
6958
labels: ${{ steps.meta.outputs.labels }}
70-
file: ${{ matrix.image.dockerfile }}
7159
platforms: linux/amd64,linux/arm64
7260
build-args: COMMIT_SHA=${{ github.sha }}
7361
- name: Generate artifact attestation
7462
uses: actions/attest-build-provenance@v2
7563
with:
76-
subject-name: ${{ env.REGISTRY }}/${{ matrix.image.image_name }}
64+
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
7765
subject-digest: ${{ steps.push.outputs.digest }}

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ node_modules
1313
# Some other stuff that should not be pushed to remote
1414
.local
1515

16-
.env
16+
.env
17+
18+
dist/
19+
out/

backend.Dockerfile renamed to Dockerfile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@ RUN --mount=type=cache,target=/cache \
1414
BUN_INSTALL_CACHE_DIR=/cache \
1515
bun install --frozen-lockfile --filter "!nexus-gate-docs"
1616

17-
FROM deps AS builder
17+
FROM deps AS builder-common
1818
COPY . .
19-
RUN cd backend && bun build src/index.ts --target bun --outdir out/
19+
20+
FROM builder-common AS builder-backend
21+
RUN cd backend && bun run build
22+
23+
FROM builder-common AS builder-frontend
24+
ENV VITE_BASE_URL=/ VITE_COMMIT_SHA=${COMMIT_SHA}
25+
RUN cd frontend && bun run build
2026

2127
FROM base AS runner
2228
ARG COMMIT_SHA
2329
ENV COMMIT_SHA=${COMMIT_SHA}
24-
COPY --from=builder /app/backend/out/index.js /app/index.js
25-
COPY --from=builder /app/backend/drizzle /app/drizzle
30+
COPY --from=builder-backend /app/backend/out/index.js /app/index.js
31+
COPY --from=builder-backend /app/backend/drizzle /app/drizzle
32+
COPY --from=builder-frontend /app/frontend/dist /app/dist
33+
2634
USER bun
2735
EXPOSE 3000/tcp
36+
2837
ENTRYPOINT [ "bun", "run", "index.js" ]

Justfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
deps:
2+
bun install
3+
4+
[working-directory: 'frontend']
5+
build-frontend: deps
6+
bun run build
7+
8+
[working-directory: 'backend']
9+
build-backend: deps
10+
bun run build
11+
12+
build-all: build-frontend build-backend
13+
14+
dist: build-all
15+
mkdir -p dist
16+
cp -r backend/out/index.js dist/
17+
cp -r backend/drizzle dist/
18+
cp -r frontend/dist/ dist/

backend/src/index.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Elysia } from "elysia";
1+
import { Elysia, t } from "elysia";
22
import { cors } from "@elysiajs/cors";
33
import { swagger } from "@elysiajs/swagger";
44
import { serverTiming } from "@elysiajs/server-timing";
@@ -7,9 +7,16 @@ import { loggerPlugin } from "@/plugins/loggerPlugin";
77
import { ALLOWED_ORIGINS, PORT, PRODUCTION } from "@/utils/config";
88
import { consola } from "consola";
99
import { initConfig } from "./utils/init";
10+
import { staticPlugin } from "@elysiajs/static";
11+
import { exists, readFile } from "node:fs/promises"
1012

1113
await initConfig();
1214

15+
let indexHtml = "";
16+
if (await exists("dist/index.html")) {
17+
indexHtml = await readFile("dist/index.html", "utf-8");
18+
}
19+
1320
const app = new Elysia()
1421
.use(loggerPlugin)
1522
.use(
@@ -36,13 +43,33 @@ const app = new Elysia()
3643
title: "NexusGate API Documentation",
3744
version: "0.0.1",
3845
},
39-
},
46+
},
4047
path: "/api/docs",
4148
specPath: "/api/openapi.json"
4249
}),
4350
)
4451
.use(serverTiming())
4552
.use(routes)
53+
.use(staticPlugin({
54+
assets: "dist",
55+
alwaysStatic: true,
56+
prefix: "/",
57+
})).get("/*", ({ headers: { accept } }) => {
58+
if (typeof accept === "string" && !accept.includes("text/html")) {
59+
return new Response("Not Found", {
60+
status: 404,
61+
});
62+
}
63+
return new Response(indexHtml, {
64+
headers: {
65+
"Content-Type": "text/html",
66+
},
67+
});
68+
}, {
69+
headers: t.Object({
70+
accept: t.Optional(t.String())
71+
})
72+
})
4673
.listen({
4774
port: PORT,
4875
reusePort: PRODUCTION,

bun.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.cn.yaml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ services:
1919
image: "m.daocloud.io/docker.io/library/redis:latest"
2020
restart: on-failure
2121

22-
backend:
22+
nexusgate:
2323
image: "docker.cnb.cool/em-geeklab/nexusgate/server:latest"
2424
environment:
2525
- "DATABASE_URL=postgres://nexusgate:${POSTGRES_PASSWORD:-change_me_in_production}@database:5432/nexusgate"
@@ -30,15 +30,4 @@ services:
3030
database:
3131
condition: service_healthy
3232
redis:
33-
condition: service_started
34-
35-
frontend:
36-
image: "docker.cnb.cool/em-geeklab/nexusgate/web:latest"
37-
environment:
38-
- "BACKEND_URL=http://backend:3000"
39-
restart: on-failure
40-
depends_on:
41-
- backend
42-
ports:
43-
- "${WEB_PORT:-8080}:80"
44-
33+
condition: service_started

docker-compose.yaml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ services:
1919
image: "redis:latest"
2020
restart: on-failure
2121

22-
backend:
23-
image: "ghcr.io/em-geeklab/nexus-gate-server:latest"
22+
nexusgate:
23+
image: "ghcr.io/em-geeklab/nexusgate:latest"
2424
environment:
2525
- "DATABASE_URL=postgres://nexusgate:${POSTGRES_PASSWORD:-change_me_in_production}@database:5432/nexusgate"
2626
- "ADMIN_SUPER_SECRET=${ADMIN_SUPER_SECRET:-admin_key_change_in_production}"
@@ -30,15 +30,4 @@ services:
3030
database:
3131
condition: service_healthy
3232
redis:
33-
condition: service_started
34-
35-
frontend:
36-
image: "ghcr.io/em-geeklab/nexus-gate-web:latest"
37-
environment:
38-
- "BACKEND_URL=http://backend:3000"
39-
restart: on-failure
40-
depends_on:
41-
- backend
42-
ports:
43-
- "${WEB_PORT:-8080}:80"
44-
33+
condition: service_started

frontend.Dockerfile

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

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"esbuild",
1313
"protobufjs",
1414
"sharp"
15-
]
15+
],
16+
"dependencies": {
17+
"@elysiajs/static": "^1.3.0"
18+
}
1619
}

0 commit comments

Comments
 (0)