Skip to content

Commit b5bf4ed

Browse files
committed
Add config for cabotage
1 parent 5aaa290 commit b5bf4ed

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules/
2+
dist/
3+
.astro/
4+
.env
5+
.env.*
6+
.git/
7+
.gitignore
8+
.DS_Store
9+
README.md
10+
Dockerfile
11+
.dockerignore
12+
npm-debug.log*

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM oven/bun:1 AS build
2+
3+
WORKDIR /app
4+
5+
COPY package.json bun.lock ./
6+
RUN bun install --frozen-lockfile
7+
8+
COPY . .
9+
RUN bun run build
10+
11+
FROM oven/bun:1-slim
12+
13+
WORKDIR /app
14+
15+
COPY --from=build /app/dist ./dist
16+
COPY serve.ts ./
17+
18+
CMD ["bun", "run", "serve.ts"]

Procfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
web: bun run serve.ts
2+
release: echo 'deploy'

bun.lock

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"devDependencies": {
3030
"@astrojs/check": "^0.9.6",
3131
"@tailwindcss/postcss": "^4.1.18",
32+
"@types/bun": "^1.3.11",
3233
"@types/mdast": "^4.0.4",
3334
"@types/turndown": "^5.0.6",
3435
"fast-xml-parser": "^5.3.5",

serve.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { join, resolve } from "node:path";
2+
3+
const DIST = resolve(import.meta.dirname, "dist");
4+
const SOCK = "/var/run/cabotage/cabotage.sock";
5+
6+
async function serveFile(filePath: string): Promise<Response | null> {
7+
const resolved = resolve(filePath);
8+
if (!resolved.startsWith(DIST)) return null;
9+
const file = Bun.file(resolved);
10+
if (await file.exists()) return new Response(file);
11+
return null;
12+
}
13+
14+
Bun.serve({
15+
unix: SOCK,
16+
async fetch(req) {
17+
const url = new URL(req.url);
18+
const pathname = decodeURIComponent(url.pathname);
19+
20+
// Try exact file
21+
let resp = await serveFile(join(DIST, pathname));
22+
if (resp) return resp;
23+
24+
// Try as directory with index.html
25+
resp = await serveFile(join(DIST, pathname, "index.html"));
26+
if (resp) return resp;
27+
28+
// Try with .html extension
29+
resp = await serveFile(join(DIST, pathname + ".html"));
30+
if (resp) return resp;
31+
32+
// 404
33+
const notFound = await serveFile(join(DIST, "404.html"));
34+
if (notFound)
35+
return new Response(notFound.body, {
36+
status: 404,
37+
headers: notFound.headers,
38+
});
39+
return new Response("Not Found", { status: 404 });
40+
},
41+
});
42+
43+
console.log(`Listening on ${SOCK}`);

0 commit comments

Comments
 (0)