Skip to content

Commit

Permalink
Add dedicated api app
Browse files Browse the repository at this point in the history
  • Loading branch information
omfj committed Sep 19, 2024
1 parent 6a038f4 commit bfe001a
Show file tree
Hide file tree
Showing 54 changed files with 1,216 additions and 790 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
.git
.env
logs
.DS_Store
.vscode
.github
.sanity
.turbo
.next
dist
postgres-data
redis-data
36 changes: 36 additions & 0 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM node:20-alpine AS base

RUN npm install -g turbo@2.0.14
RUN npm install -g pnpm@9.7.1

FROM base AS builder

RUN apk update
RUN apk add --no-cache libc6-compat

WORKDIR /app
COPY . .
RUN turbo prune @echo-webkom/api --docker

FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY --from=builder /app/out/json/ .
RUN pnpm install

COPY --from=builder /app/out/full/ .

RUN pnpm turbo build --filter=@echo-webkom/api...

FROM base AS runner
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 hono
USER hono

COPY --from=installer /app/apps/api/dist ./dist

CMD node ./dist/index.js
17 changes: 17 additions & 0 deletions apps/api/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
app = "fly-echo-api"
primary_region = "arn"

[build]

[http_service]
internal_port = 8000
force_https = true
auto_stop_machines = "stop"
auto_start_machines = true
min_machines_running = 1
processes = ["app"]

[[vm]]
size = "shared-cpu-2x"
memory = "1gb"
cpus = 4
34 changes: 34 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@echo-webkom/api",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "pnpm with-env tsx --watch ./src/index.ts",
"build": "pnpm with-env esbuild ./src/index.ts --bundle --platform=node --outfile=dist/index.js",
"start": "node ./dist/index.js",
"typecheck": "pnpm with-env tsc --noEmit",
"clean": "rm -rf dist .turbo node_modules",
"with-env": "dotenv -e ../../.env --"
},
"dependencies": {
"@echo-webkom/auth": "workspace:*",
"@echo-webkom/db": "workspace:*",
"@echo-webkom/email": "workspace:*",
"@echo-webkom/lib": "workspace:*",
"@echo-webkom/sanity": "workspace:*",
"@hono/node-server": "1.13.0",
"date-fns": "3.6.0",
"drizzle-orm": "0.33.0",
"hono": "4.6.2",
"zod": "3.23.8"
},
"devDependencies": {
"@echo-webkom/tsconfig": "workspace:*",
"@types/node": "20.16.5",
"dotenv-cli": "7.4.2",
"esbuild": "^0.23.1",
"tsx": "4.19.1",
"typescript": "5.5.4",
"vitest": "2.0.5"
}
}
22 changes: 22 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";

import adminApp from "./services/admin";
import feedbackApp from "./services/feedback";
import happeningApp from "./services/happening";
import healthApp from "./services/health";
import shoppingApp from "./services/shopping-list";

const app = new Hono();

app.use(logger());
app.use(cors());

app.route("/", healthApp);
app.route("/", adminApp);
app.route("/", happeningApp);
app.route("/", feedbackApp);
app.route("/", shoppingApp);

export default app;
15 changes: 15 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { serve } from "@hono/node-server";

import app from "./app";

const PORT = 8000;

serve(
{
fetch: app.fetch,
port: PORT,
},
(info) => {
console.log(`Listening on http://localhost:${info.port}`);
},
);
13 changes: 13 additions & 0 deletions apps/api/src/middleware/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createMiddleware } from "hono/factory";

export const admin = () => {
return createMiddleware(async (c, next) => {
const bearerToken = c.req.header("Authorization");

if (bearerToken !== "Bearer foobar") {
return c.json({ error: "Unauthorized" }, 401);
}

return await next();
});
};
Loading

0 comments on commit bfe001a

Please sign in to comment.