-
Notifications
You must be signed in to change notification settings - Fork 0
Add production-like local Docker edge stack for docs + API #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| .git | ||
| .github | ||
| .worktrees | ||
| target | ||
| contracts/rust/api_contract/target | ||
| node_modules | ||
| web/node_modules | ||
| web/dist | ||
| web/test-results | ||
| web/playwright-report | ||
| .venv | ||
| .hypothesis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| name: epochapi-local | ||
|
|
||
| services: | ||
| api: | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile | ||
| environment: | ||
| ED25519_PRIVATE_KEY_HEX: "1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100" | ||
| expose: | ||
| - "8080" | ||
| ports: | ||
| - "8080:8080" | ||
|
|
||
| docs: | ||
| build: | ||
| context: . | ||
| dockerfile: web/Dockerfile | ||
| args: | ||
| PUBLIC_API_BASE_URL: "http://api.example.com" | ||
| expose: | ||
| - "3000" | ||
| ports: | ||
| - "3000:3000" | ||
|
|
||
| edge: | ||
| image: nginx:1.27-alpine | ||
| depends_on: | ||
| - api | ||
| - docs | ||
| ports: | ||
| - "80:80" | ||
| volumes: | ||
| - ./docker/nginx/local-edge.conf:/etc/nginx/conf.d/default.conf:ro |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| map $http_origin $cors_origin { | ||
| default ""; | ||
| "http://example.com" "http://example.com"; | ||
| "https://example.com" "https://example.com"; | ||
| "http://localhost" "http://localhost"; | ||
| "http://localhost:3000" "http://localhost:3000"; | ||
| } | ||
|
|
||
| server { | ||
| listen 80; | ||
| server_name example.com; | ||
|
|
||
| location / { | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Host $host; | ||
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| proxy_set_header X-Forwarded-Proto $scheme; | ||
| proxy_pass http://docs:3000; | ||
| } | ||
| } | ||
|
|
||
| server { | ||
| listen 80; | ||
| server_name api.example.com; | ||
|
|
||
| add_header Access-Control-Allow-Origin $cors_origin always; | ||
| add_header Access-Control-Allow-Methods "GET,POST,OPTIONS" always; | ||
| add_header Access-Control-Allow-Headers "Content-Type,Authorization" always; | ||
| add_header Access-Control-Max-Age 86400 always; | ||
| add_header Vary Origin always; | ||
|
|
||
| if ($request_method = OPTIONS) { | ||
| return 204; | ||
| } | ||
|
|
||
| location / { | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Host $host; | ||
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| proxy_set_header X-Forwarded-Proto $scheme; | ||
| proxy_pass http://api:8080; | ||
| } | ||
| } | ||
|
|
||
| server { | ||
| listen 80; | ||
| server_name localhost; | ||
|
|
||
| location /api/ { | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Host api.example.com; | ||
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| proxy_set_header X-Forwarded-Proto $scheme; | ||
| rewrite ^/api/(.*)$ /$1 break; | ||
| proxy_pass http://api:8080; | ||
| } | ||
|
|
||
| location / { | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Host example.com; | ||
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| proxy_set_header X-Forwarded-Proto $scheme; | ||
| proxy_pass http://docs:3000; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # syntax=docker/dockerfile:1 | ||
|
|
||
| FROM node:22-bookworm-slim AS builder | ||
| WORKDIR /app | ||
|
|
||
| COPY web/package.json web/package-lock.json ./ | ||
| RUN npm ci | ||
|
|
||
| COPY web/ ./ | ||
|
|
||
| ARG PUBLIC_API_BASE_URL=http://api.example.com | ||
| ENV PUBLIC_API_BASE_URL=${PUBLIC_API_BASE_URL} | ||
|
|
||
| RUN npm run build | ||
|
|
||
| FROM gcr.io/distroless/nodejs22-debian12:nonroot | ||
| WORKDIR /app | ||
|
|
||
| COPY --from=builder /app/dist ./dist | ||
| COPY --from=builder /app/server.mjs ./server.mjs | ||
|
|
||
| ENV HOST=0.0.0.0 | ||
| ENV PORT=3000 | ||
|
|
||
| EXPOSE 3000 | ||
|
|
||
| CMD ["server.mjs"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { createReadStream, existsSync, statSync } from "node:fs"; | ||
| import { extname, join, normalize } from "node:path"; | ||
| import { createServer } from "node:http"; | ||
|
|
||
| const host = process.env.HOST ?? "0.0.0.0"; | ||
| const port = Number(process.env.PORT ?? "3000"); | ||
| const root = join(process.cwd(), "dist"); | ||
|
|
||
| const contentTypes = { | ||
| ".css": "text/css; charset=utf-8", | ||
| ".html": "text/html; charset=utf-8", | ||
| ".ico": "image/x-icon", | ||
| ".js": "text/javascript; charset=utf-8", | ||
| ".json": "application/json; charset=utf-8", | ||
| ".map": "application/json; charset=utf-8", | ||
| ".png": "image/png", | ||
| ".svg": "image/svg+xml", | ||
| ".txt": "text/plain; charset=utf-8", | ||
| ".woff": "font/woff", | ||
| ".woff2": "font/woff2", | ||
| }; | ||
|
|
||
| function safePath(requestPath) { | ||
| const normalized = normalize(requestPath).replace(/^\/+/, ""); | ||
| return normalized.startsWith("..") ? "" : normalized; | ||
| } | ||
|
|
||
| function resolveFile(pathname) { | ||
| const relative = safePath(pathname); | ||
| if (!relative) return join(root, "index.html"); | ||
|
|
||
| const exact = join(root, relative); | ||
| if (existsSync(exact) && statSync(exact).isFile()) { | ||
| return exact; | ||
| } | ||
|
|
||
| const nestedIndex = join(root, relative, "index.html"); | ||
| if (existsSync(nestedIndex)) { | ||
| return nestedIndex; | ||
| } | ||
|
|
||
| const htmlVariant = join(root, `${relative}.html`); | ||
| if (existsSync(htmlVariant)) { | ||
| return htmlVariant; | ||
| } | ||
|
|
||
| return join(root, "index.html"); | ||
| } | ||
|
|
||
| const server = createServer((req, res) => { | ||
| const url = new URL(req.url ?? "/", "http://localhost"); | ||
|
|
||
| if (url.pathname === "/health") { | ||
| res.writeHead(200, { "content-type": "text/plain; charset=utf-8" }); | ||
| res.end("ok"); | ||
| return; | ||
| } | ||
|
|
||
| const filePath = resolveFile(url.pathname); | ||
| const extension = extname(filePath); | ||
| const contentType = contentTypes[extension] ?? "application/octet-stream"; | ||
|
|
||
| res.writeHead(200, { | ||
| "cache-control": extension === ".html" ? "no-cache" : "public, max-age=31536000, immutable", | ||
| "content-type": contentType, | ||
| "x-content-type-options": "nosniff", | ||
| }); | ||
|
|
||
| createReadStream(filePath).pipe(res); | ||
| }); | ||
|
|
||
| server.listen(port, host, () => { | ||
| console.log(`docs server listening on http://${host}:${port}`); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CORS map allows
http://localhost:3000but nothttp://localhost, even though this same file defines aserver_name localhostedge route for docs. When users open docs viahttp://localhostand the UI callshttp://api.example.com, the browser sendsOrigin: http://localhost; that falls through todefault "", soAccess-Control-Allow-Originis empty and the request is blocked by CORS. Addinghttp://localhostto the map avoids breaking the localhost edge workflow.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in a06c844. Added
http://localhostto the CORS origin map so the localhost edge workflow (Host: localhostdocs ->api.example.com) returns a validAccess-Control-Allow-Originheader. Also re-ran a preflight check withOrigin: http://localhostand it now returns 204 with the expected CORS header.