From fa2f3acb351c6dec687c6f12c3012897a7a32e00 Mon Sep 17 00:00:00 2001 From: ngn Date: Sat, 18 Jan 2025 03:31:37 +0300 Subject: [PATCH] cleanup for the docker setup Signed-off-by: ngn --- .gitignore | 5 + api/Dockerfile | 10 +- api/Makefile | 2 +- api/config/config.go | 7 +- api/config/option.go | 2 +- api/routes/index.go | 5 +- app/.dockerignore | 2 +- app/Dockerfile | 33 ++++- app/package.json | 5 +- app/src/lib/api.js | 2 +- app/src/lib/doc.js | 2 +- app/src/lib/error.svelte | 2 +- app/src/lib/footer.svelte | 6 +- app/src/lib/head.svelte | 4 +- app/src/lib/lang.js | 4 - app/src/lib/link.svelte | 4 +- app/src/lib/locale.js | 66 +++++++++ app/src/lib/navbar_switch.svelte | 34 ++--- app/src/lib/service.svelte | 6 +- app/src/lib/util.js | 89 +++--------- app/src/routes/+layout.js | 39 +---- app/src/routes/+layout.svelte | 26 ++-- app/src/routes/+page.js | 14 ++ app/src/routes/+page.svelte | 169 ++++++++++++---------- app/src/routes/doc/[name]/+page.server.js | 7 +- app/src/routes/doc/[name]/+page.svelte | 56 +++---- app/src/routes/services/+page.js | 14 ++ app/src/routes/services/+page.svelte | 46 +++--- app/vite.config.js | 16 +- deploy/compose.yml | 50 +++++++ deploy/run.sh | 9 ++ doc/Dockerfile | 6 +- doc/src/config.c | 4 +- redeploy.sh | 6 - 34 files changed, 433 insertions(+), 319 deletions(-) delete mode 100644 app/src/lib/lang.js create mode 100644 app/src/lib/locale.js create mode 100644 app/src/routes/+page.js create mode 100644 app/src/routes/services/+page.js create mode 100644 deploy/compose.yml create mode 100644 deploy/run.sh delete mode 100755 redeploy.sh diff --git a/.gitignore b/.gitignore index 6c4cd8f..866d243 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ +data.db *.yaml +*.yml *.env + +# don't ignore example deployment stuff +!deploy/* diff --git a/api/Dockerfile b/api/Dockerfile index 4a29454..84e53d2 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -2,9 +2,16 @@ FROM golang:1.23.4 WORKDIR /api -COPY *.go ./ +RUN useradd runner -r -u 1001 -d /api +RUN chown -R runner:runner /api +USER runner + COPY *.mod ./ COPY *.sum ./ + +RUN go mod download + +COPY *.go ./ COPY Makefile ./ COPY config ./config COPY database ./database @@ -14,7 +21,6 @@ COPY status ./status COPY util ./util COPY views ./views -EXPOSE 7001 RUN make ENTRYPOINT ["/api/api.elf"] diff --git a/api/Makefile b/api/Makefile index 062716b..e496f4e 100644 --- a/api/Makefile +++ b/api/Makefile @@ -6,7 +6,7 @@ api.elf: $(GOSRCS) go build -o $@ run: - API_DEBUG=true API_APP_URL=http://localhost:7002 API_PASSWORD=test ./api.elf + WEBSITE_DEBUG=true WEBSITE_PASSWORD=test ./api.elf format: gofmt -s -w . diff --git a/api/config/config.go b/api/config/config.go index 2913a04..ca90066 100644 --- a/api/config/config.go +++ b/api/config/config.go @@ -39,12 +39,11 @@ func (c *Type) Load() (err error) { c.Options = []Option{ {Name: "debug", Value: "false", Type: OPTION_TYPE_BOOL, Required: true}, // should display debug messgaes? - {Name: "url", Value: "http://localhost:7001/", Type: OPTION_TYPE_URL, Required: true}, // API URL for the website - {Name: "app_url", Value: "http://localhost:7002/", Type: OPTION_TYPE_URL, Required: true}, // frontend application URL for the website - {Name: "doc_url", Value: "http://localhost:7003/", Type: OPTION_TYPE_URL, Required: true}, // documentation URL for the website + {Name: "app_url", Value: "http://localhost:7001/", Type: OPTION_TYPE_URL, Required: true}, // frontend application URL for the website + {Name: "api_url", Value: "http://localhost:7002/", Type: OPTION_TYPE_URL, Required: true}, // API URL for the website {Name: "password", Value: "", Type: OPTION_TYPE_STR, Required: true}, // admin password - {Name: "host", Value: "0.0.0.0:7001", Type: OPTION_TYPE_STR, Required: true}, // host the server should listen on + {Name: "host", Value: "0.0.0.0:7002", Type: OPTION_TYPE_STR, Required: true}, // host the server should listen on {Name: "ip_header", Value: "X-Real-IP", Type: OPTION_TYPE_STR, Required: false}, // header that should be checked for obtaining the client IP {Name: "interval", Value: "1h", Type: OPTION_TYPE_STR, Required: false}, // service status check interval {Name: "timeout", Value: "15s", Type: OPTION_TYPE_STR, Required: false}, // timeout for the service status check diff --git a/api/config/option.go b/api/config/option.go index ae667bd..cae1f4a 100644 --- a/api/config/option.go +++ b/api/config/option.go @@ -25,7 +25,7 @@ type Option struct { } func (o *Option) Env() string { - return strings.ToUpper(fmt.Sprintf("API_%s", o.Name)) + return strings.ToUpper(fmt.Sprintf("WEBSITE_%s", o.Name)) } func (o *Option) Load() (err error) { diff --git a/api/routes/index.go b/api/routes/index.go index 786070c..f2272f4 100644 --- a/api/routes/index.go +++ b/api/routes/index.go @@ -7,7 +7,8 @@ import ( func GET_Index(c *fiber.Ctx) error { conf := c.Locals("config").(*config.Type) - doc := conf.GetURL("doc_url") + app := conf.GetURL("app_url") - return c.Redirect(doc.JoinPath("/api").String()) + // redirect to the API documentation + return c.Redirect(app.JoinPath("/doc/api").String()) } diff --git a/app/.dockerignore b/app/.dockerignore index b02546f..a136c83 100644 --- a/app/.dockerignore +++ b/app/.dockerignore @@ -1,4 +1,4 @@ node_modules .svelte-kit -build public +build diff --git a/app/Dockerfile b/app/Dockerfile index 524db3a..9d5af1a 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -1,22 +1,39 @@ -FROM node:23.5.0 as build +# build the application with node +FROM node:23.5.0 AS build + +ARG WEBSITE_REPORT_URL +ARG WEBSITE_SOURCE_URL +ARG WEBSITE_APP_URL +ARG WEBSITE_API_URL +ARG WEBSITE_DOC_URL + +ENV WEBSITE_REPORT_URL=$WEBSITE_REPORT_URL +ENV WEBSITE_SOURCE_URL=$WEBSITE_SOURCE_URL +ENV WEBSITE_APP_URL=$WEBSITE_APP_URL +ENV WEBSITE_API_URL=$WEBSITE_API_URL +ENV WEBSITE_DOC_URL=$WEBSITE_DOC_URL WORKDIR /app COPY . /app -ARG API_URL -ENV VITE_API_URL_DEV $API_URL - RUN npm install && npm run build -FROM oven/bun:1.1.20 as main +# run it with bun (a lot faster) +FROM oven/bun:latest AS main WORKDIR /app -COPY --from=build /app/build ./build -COPY --from=build /app/package.json ./package.json +COPY --from=build /app/build ./build +COPY --from=build /app/package.json ./package.json COPY --from=build /app/package-lock.json ./package-lock.json -EXPOSE 7001 +RUN useradd runner -r -u 1001 -d /app +RUN chown -R runner:runner /app + +USER runner RUN bun install +EXPOSE 7001 + +ENV PORT=7001 CMD ["bun", "build/index.js"] diff --git a/app/package.json b/app/package.json index cee0f74..65e2ae0 100644 --- a/app/package.json +++ b/app/package.json @@ -3,9 +3,9 @@ "version": "6.0", "private": true, "scripts": { - "dev": "vite --port 7002 dev", + "dev": "vite dev", "build": "vite build", - "preview": "vite preview --host", + "preview": "vite preview", "lint": "prettier --check .", "format": "prettier --write ." }, @@ -21,7 +21,6 @@ }, "type": "module", "dependencies": { - "@types/dompurify": "^3.2.0", "dompurify": "^3.2.3", "marked": "^15.0.6", "svelte-i18n": "^4.0.1" diff --git a/app/src/lib/api.js b/app/src/lib/api.js index 99bbb0e..1de72b9 100644 --- a/app/src/lib/api.js +++ b/app/src/lib/api.js @@ -1,7 +1,7 @@ import { urljoin } from "$lib/util.js"; const api_version = "v1"; -const api_url = urljoin(import.meta.env.APP_API_URL, api_version); +const api_url = urljoin(import.meta.env.WEBSITE_API_URL, api_version); function api_urljoin(path = null, query = {}) { return urljoin(api_url, path, query); diff --git a/app/src/lib/doc.js b/app/src/lib/doc.js index fa9bbb8..c7dc91b 100644 --- a/app/src/lib/doc.js +++ b/app/src/lib/doc.js @@ -1,7 +1,7 @@ import { urljoin } from "$lib/util.js"; function doc_urljoin(path = null, query = {}) { - return urljoin(import.meta.env.APP_DOC_URL, path, query); + return urljoin(import.meta.env.WEBSITE_DOC_URL, path, query); } function doc_check_err(json) { diff --git a/app/src/lib/error.svelte b/app/src/lib/error.svelte index 1df9488..728589d 100644 --- a/app/src/lib/error.svelte +++ b/app/src/lib/error.svelte @@ -15,7 +15,7 @@ {error} {/if} - + {$_("error.report")} diff --git a/app/src/lib/footer.svelte b/app/src/lib/footer.svelte index 691771c..a2412c4 100644 --- a/app/src/lib/footer.svelte +++ b/app/src/lib/footer.svelte @@ -17,17 +17,17 @@