Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.schema
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ SMTP_PASS=
# @public @optional @type=url
LOGO_URL=

# Sentry project suni-server. Empty disables error reporting (local development and tests).
# @optional @type=url
SENTRY_DSN=

# @required @public @type=email
ADMIN_EMAIL=if(forEnv(development), "suni@localhost.com")

Expand Down
2 changes: 1 addition & 1 deletion apps/server/.env.schema
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @import(../../, pick=[NODE_ENV, BETTER_AUTH_*, CORS_ORIGIN, DATABASE_URL, SMTP_*, LOGO_URL])
# @import(../../, pick=[NODE_ENV, BETTER_AUTH_*, CORS_ORIGIN, DATABASE_URL, SMTP_*, LOGO_URL, SENTRY_DSN])
# @currentEnv=$NODE_ENV
# @generateTsTypes(path=./src/env.ts, exposeEnv=local)
# ---
5 changes: 3 additions & 2 deletions apps/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:24-slim AS builder
RUN npm install -g pnpm@11
RUN npm install -g pnpm@12.4.1
WORKDIR /app

COPY . .
Expand All @@ -16,4 +16,5 @@ COPY --from=builder /app /app
EXPOSE 3000

WORKDIR /app/apps/server
CMD ["bun", "dist/index.mjs"]
# Apply pending migrations and the idempotent admin seed before serving; a failure stops the container so the deploy fails.
CMD ["sh", "-c", "cd /app/packages/db && bunx drizzle-kit migrate && bunx varlock run -- bun ./src/seeds/index.ts && cd /app/apps/server && exec bun dist/index.mjs"]
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@orpc/openapi": "catalog:",
"@orpc/server": "catalog:",
"@orpc/zod": "catalog:",
"@sentry/bun": "11.10.0",
"@suni/api": "workspace:*",
"@suni/auth": "workspace:*",
"@suni/db": "workspace:*",
Expand Down
33 changes: 29 additions & 4 deletions apps/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ import { OpenAPIReferencePlugin } from "@orpc/openapi/plugins";
import { ORPCError, onError, ValidationError } from "@orpc/server";
import { RPCHandler } from "@orpc/server/fetch";
import { ZodToJsonSchemaConverter } from "@orpc/zod/zod4";
import { captureException, init as initSentry } from "@sentry/bun";
import { appRouter } from "@suni/api/routers/index";
import { initLogger } from "evlog";
import { createAuthMiddleware } from "evlog/better-auth";
import { createFsDrain } from "evlog/fs";
import { evlog } from "evlog/hono";
import type { EvlogVariables } from "evlog/hono";
import { Hono } from "hono";
import type { Context } from "hono";
import { cors } from "hono/cors";
import { HTTPException } from "hono/http-exception";
import { z } from "zod";

import { createContext } from "./context";
import { ENV } from "./env.server";
import { auth } from "./services";

// Without a DSN (local development and tests) Sentry stays disabled.
initSentry({ dsn: ENV.SENTRY_DSN, environment: ENV.NODE_ENV });

initLogger({
env: { service: "suni-server" },
});
Expand All @@ -29,9 +35,28 @@ const identifyUser = createAuthMiddleware(auth, {
const app = new Hono<EvlogVariables>();

function handleError<T>(error: T) {
// 4xx oRPC errors (validation, auth, not found) are expected answers, not bugs.
if (!(error instanceof ORPCError) || error.status >= 500) {
captureException(error);
Comment thread
adrrian17 marked this conversation as resolved.
Resolved
}

console.error(error);
}

// Errors outside oRPC, e.g. reading the session when the database is down.
function handleUnexpectedError(error: Error, c: Context) {
if (error instanceof HTTPException) {
return error.getResponse();
}

captureException(error);
console.error(error);

return c.text("Internal Server Error", 500);
}

app.onError(handleUnexpectedError);

// Default Zod messages (e.g. a missing field) in Spanish; schemas keep their own messages.
z.config(z.locales.es());

Expand Down Expand Up @@ -84,13 +109,13 @@ export const apiHandler = new OpenAPIHandler(appRouter, {
schemaConverters: [new ZodToJsonSchemaConverter()],
}),
],
interceptors: [onError(handleError)],
clientInterceptors: [schemaErrorMessage],
// Client interceptors wrap only the procedure, so malformed request bodies (a 400) never reach handleError.
clientInterceptors: [onError(handleError), schemaErrorMessage],
});

export const rpcHandler = new RPCHandler(appRouter, {
interceptors: [onError(handleError)],
clientInterceptors: [schemaErrorMessage],
// Client interceptors wrap only the procedure, so malformed request bodies (a 400) never reach handleError.
clientInterceptors: [onError(handleError), schemaErrorMessage],
});

app.use("/*", async (c, next) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:24-slim AS builder
RUN npm install -g pnpm@11
RUN npm install -g pnpm@12.4.1
WORKDIR /app

COPY . .
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"knip": "knip",
"check:type-aware": "oxlint -c oxlint-type-aware.config.ts",
"check:changed": "ultracite check && pnpm run check:type-aware && turbo run check-types --affected && knip",
"prepare": "lefthook install"
"prepare": "test ! -e .git || lefthook install"
},
"devDependencies": {
"@coderabbitai/config": "1.1.1",
Expand Down
Loading
Loading