From b8b1192d88d0aec995e65df07a3453826872f7ac Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 21 Jan 2024 11:30:02 +0800 Subject: [PATCH] Add docker file --- .github/workflows/docker.yaml | 44 ++++++++++++++++++++++++++ Dockerfile | 34 ++++++++++++++++++++ config.ts | 2 +- fresh.gen.ts | 58 ++++++++++++++++++----------------- routes/_middleware.ts | 20 ++++++++---- routes/api/_middleware.ts | 1 + routes/api/health_check.ts | 7 +++++ static/.gitignore | 2 ++ 8 files changed, 133 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/docker.yaml create mode 100644 Dockerfile create mode 100644 routes/api/health_check.ts diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..2fa15c3 --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,44 @@ +name: Docker image deploy + +on: + push: + branches: + - master + paths-ignore: + - docker-compose.yml + - README.md + workflow_dispatch: + +concurrency: + group: docker + cancel-in-progress: false + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: lifegpc + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Extract metadata for docker + id: meta + uses: docker/metadata-action@v5 + with: + images: lifegpc/eh_downloader_base + - name: Switch docker drive + run: docker buildx create --use + - name: Build and push Docker Image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + DENO_DEPLOYMENT_ID=${{ github.sha }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1752088 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +FROM denoland/deno:latest + +ARG DENO_DEPLOYMENT_ID + +WORKDIR /app + +COPY ./components ./components +COPY ./islands ./islands +COPY ./page ./page +COPY ./routes ./routes +COPY ./server ./server +COPY ./static/*.css ./static/ +COPY ./static/*.ts ./static/ +COPY ./static/*.ico ./static/ +COPY ./static/*.svg ./static/ +COPY ./tasks ./tasks +COPY ./thumbnail ./thumbnail +COPY ./translation ./translation +COPY ./utils ./utils +COPY ./*.ts ./ +COPY ./deno.json ./ +COPY ./import_map.json ./ +COPY ./LICENSE ./ + +RUN deno task fetch +ENV DENO_DEPLOYMENT_ID=${DENO_DEPLOYMENT_ID} +RUN deno task server-build + +EXPOSE 8000 +ENTRYPOINT deno +CMD ["task", "server"] + +HEALTHCHECK --interval=30s --timeout=30s --start-period=10s --retries=3 \ + CMD curl -Lk -fsS http://localhost:8000/api/health_check || exit 1 diff --git a/config.ts b/config.ts index 3c7bfbd..6f6a8a6 100644 --- a/config.ts +++ b/config.ts @@ -114,7 +114,7 @@ export class Config { return this._return_bool("export_zip_jpn_title") || false; } get hostname() { - return this._return_string("hostname") || "localhost"; + return this._return_string("hostname") || "0.0.0.0"; } get meili_host() { return this._return_string("meili_host"); diff --git a/fresh.gen.ts b/fresh.gen.ts index c64d2e5..2942943 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -16,20 +16,21 @@ import * as $10 from "./routes/api/filemeta/[token].ts"; import * as $11 from "./routes/api/files/[token].ts"; import * as $12 from "./routes/api/gallery/[gid].ts"; import * as $13 from "./routes/api/gallery/list.ts"; -import * as $14 from "./routes/api/status.ts"; -import * as $15 from "./routes/api/tag/[id].ts"; -import * as $16 from "./routes/api/tag/rows.ts"; -import * as $17 from "./routes/api/task.ts"; -import * as $18 from "./routes/api/thumbnail/[id].ts"; -import * as $19 from "./routes/api/token.ts"; -import * as $20 from "./routes/api/user.ts"; -import * as $21 from "./routes/file/[id].ts"; -import * as $22 from "./routes/file/_middleware.ts"; -import * as $23 from "./routes/index.tsx"; -import * as $24 from "./routes/manifest.json.ts"; -import * as $25 from "./routes/thumbnail/[id].ts"; -import * as $26 from "./routes/thumbnail/_middleware.ts"; -import * as $27 from "./routes/upload.tsx"; +import * as $14 from "./routes/api/health_check.ts"; +import * as $15 from "./routes/api/status.ts"; +import * as $16 from "./routes/api/tag/[id].ts"; +import * as $17 from "./routes/api/tag/rows.ts"; +import * as $18 from "./routes/api/task.ts"; +import * as $19 from "./routes/api/thumbnail/[id].ts"; +import * as $20 from "./routes/api/token.ts"; +import * as $21 from "./routes/api/user.ts"; +import * as $22 from "./routes/file/[id].ts"; +import * as $23 from "./routes/file/_middleware.ts"; +import * as $24 from "./routes/index.tsx"; +import * as $25 from "./routes/manifest.json.ts"; +import * as $26 from "./routes/thumbnail/[id].ts"; +import * as $27 from "./routes/thumbnail/_middleware.ts"; +import * as $28 from "./routes/upload.tsx"; import * as $$0 from "./islands/Container.tsx"; import * as $$1 from "./islands/Settings.tsx"; import * as $$2 from "./islands/TaskManager.tsx"; @@ -51,20 +52,21 @@ const manifest = { "./routes/api/files/[token].ts": $11, "./routes/api/gallery/[gid].ts": $12, "./routes/api/gallery/list.ts": $13, - "./routes/api/status.ts": $14, - "./routes/api/tag/[id].ts": $15, - "./routes/api/tag/rows.ts": $16, - "./routes/api/task.ts": $17, - "./routes/api/thumbnail/[id].ts": $18, - "./routes/api/token.ts": $19, - "./routes/api/user.ts": $20, - "./routes/file/[id].ts": $21, - "./routes/file/_middleware.ts": $22, - "./routes/index.tsx": $23, - "./routes/manifest.json.ts": $24, - "./routes/thumbnail/[id].ts": $25, - "./routes/thumbnail/_middleware.ts": $26, - "./routes/upload.tsx": $27, + "./routes/api/health_check.ts": $14, + "./routes/api/status.ts": $15, + "./routes/api/tag/[id].ts": $16, + "./routes/api/tag/rows.ts": $17, + "./routes/api/task.ts": $18, + "./routes/api/thumbnail/[id].ts": $19, + "./routes/api/token.ts": $20, + "./routes/api/user.ts": $21, + "./routes/file/[id].ts": $22, + "./routes/file/_middleware.ts": $23, + "./routes/index.tsx": $24, + "./routes/manifest.json.ts": $25, + "./routes/thumbnail/[id].ts": $26, + "./routes/thumbnail/_middleware.ts": $27, + "./routes/upload.tsx": $28, }, islands: { "./islands/Container.tsx": $$0, diff --git a/routes/_middleware.ts b/routes/_middleware.ts index 7ab72e3..ca1cef1 100644 --- a/routes/_middleware.ts +++ b/routes/_middleware.ts @@ -77,15 +77,23 @@ export async function handler(req: Request, ctx: MiddlewareHandlerContext) { } if (url.pathname == "/flutter" || url.pathname.startsWith("/flutter/")) { const m = get_task_manager(); + let flutter_base = import.meta.resolve("../static/flutter").slice(7); + if (Deno.build.os === "windows") { + flutter_base = flutter_base.slice(1); + } if (!m.cfg.flutter_frontend) { - return new Response("Flutter frontend is not enabled", { - status: 404, - }); + if (!await exists(flutter_base)) { + return new Response("Flutter frontend is not enabled", { + status: 404, + }); + } + } else { + flutter_base = m.cfg.flutter_frontend; } const u = new URL(req.url); - let p = join(m.cfg.flutter_frontend, u.pathname.slice(8)); - if (!(await exists(p)) || p === m.cfg.flutter_frontend) { - p = join(m.cfg.flutter_frontend, "/index.html"); + let p = join(flutter_base, u.pathname.slice(8)); + if (!(await exists(p)) || p === flutter_base) { + p = join(flutter_base, "/index.html"); } const opts: GetFileResponseOptions = {}; opts.range = req.headers.get("range"); diff --git a/routes/api/_middleware.ts b/routes/api/_middleware.ts index e7e4bd1..47dc399 100644 --- a/routes/api/_middleware.ts +++ b/routes/api/_middleware.ts @@ -19,6 +19,7 @@ function handle_auth(req: Request, ctx: MiddlewareHandlerContext) { const check = () => { if (u.pathname === "/api/token" && req.method === "PUT") return true; if (u.pathname === "/api/status" && req.method === "GET") return true; + if (u.pathname === "/api/health_check" && req.method === "GET") return true; return false; }; if (!token) return check(); diff --git a/routes/api/health_check.ts b/routes/api/health_check.ts new file mode 100644 index 0000000..44a8e9d --- /dev/null +++ b/routes/api/health_check.ts @@ -0,0 +1,7 @@ +import { Handlers } from "$fresh/server.ts"; + +export const handler: Handlers = { + GET(_req, _ctx) { + return new Response("OK"); + }, +}; diff --git a/static/.gitignore b/static/.gitignore index 347db2c..386fb61 100644 --- a/static/.gitignore +++ b/static/.gitignore @@ -1,4 +1,6 @@ bootstrap/ +flutter +flutter/ preact-material-components/ sw.js sw.js.map