feat: scaffold Hono server with auth and health check
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
11
server/src/auth.ts
Normal file
11
server/src/auth.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { betterAuth } from "better-auth";
|
||||
import { apiKey } from "better-auth/plugins";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { db } from "./db.js";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg",
|
||||
}),
|
||||
plugins: [apiKey()],
|
||||
});
|
||||
6
server/src/db.ts
Normal file
6
server/src/db.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import { env } from "./env.js";
|
||||
|
||||
const client = postgres(env.DATABASE_URL);
|
||||
export const db = drizzle(client);
|
||||
9
server/src/env.ts
Normal file
9
server/src/env.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export const env = {
|
||||
DATABASE_URL: process.env.DATABASE_URL!,
|
||||
PORT: parseInt(process.env.PORT || "8080"),
|
||||
CORS_ORIGIN: process.env.CORS_ORIGIN || "http://localhost:5173",
|
||||
};
|
||||
|
||||
if (!env.DATABASE_URL) {
|
||||
throw new Error("DATABASE_URL is not set");
|
||||
}
|
||||
44
server/src/index.ts
Normal file
44
server/src/index.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Hono } from "hono";
|
||||
import { cors } from "hono/cors";
|
||||
import { auth } from "./auth.js";
|
||||
import { env } from "./env.js";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
// CORS for dashboard
|
||||
app.use(
|
||||
"*",
|
||||
cors({
|
||||
origin: env.CORS_ORIGIN,
|
||||
allowHeaders: ["Content-Type", "Authorization"],
|
||||
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
credentials: true,
|
||||
})
|
||||
);
|
||||
|
||||
// Better Auth handler
|
||||
app.on(["POST", "GET"], "/api/auth/*", (c) => {
|
||||
return auth.handler(c.req.raw);
|
||||
});
|
||||
|
||||
// Health check
|
||||
app.get("/health", (c) => c.json({ status: "ok" }));
|
||||
|
||||
// Start server with WebSocket support
|
||||
const server = Bun.serve({
|
||||
port: env.PORT,
|
||||
fetch: app.fetch,
|
||||
websocket: {
|
||||
open(ws) {
|
||||
console.log("WebSocket connected");
|
||||
},
|
||||
message(ws, message) {
|
||||
// placeholder — Task 4 implements device/dashboard handlers
|
||||
},
|
||||
close(ws) {
|
||||
console.log("WebSocket disconnected");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`Server running on port ${server.port}`);
|
||||
Reference in New Issue
Block a user