61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { Hono } from "hono";
|
|
import { serveStatic } from "hono/bun";
|
|
import { seedDefaults } from "../db/seed.ts";
|
|
import { mcpRoutes } from "./mcp/index.ts";
|
|
import { requireAuth } from "./middleware/auth.ts";
|
|
import { authRoutes } from "./routes/auth.ts";
|
|
import { categoryRoutes } from "./routes/categories.ts";
|
|
import { imageRoutes } from "./routes/images.ts";
|
|
import { itemRoutes } from "./routes/items.ts";
|
|
import { settingsRoutes } from "./routes/settings.ts";
|
|
import { setupRoutes } from "./routes/setups.ts";
|
|
import { threadRoutes } from "./routes/threads.ts";
|
|
import { totalRoutes } from "./routes/totals.ts";
|
|
|
|
// Seed default data on startup
|
|
seedDefaults();
|
|
|
|
const app = new Hono();
|
|
|
|
// Health check
|
|
app.get("/api/health", (c) => {
|
|
return c.json({ status: "ok" });
|
|
});
|
|
|
|
// Auth middleware for write operations (POST/PUT/DELETE) on non-auth routes
|
|
app.use("/api/*", async (c, next) => {
|
|
// Skip auth routes — they handle their own auth
|
|
if (c.req.path.startsWith("/api/auth")) return next();
|
|
// Skip GET requests — read is public
|
|
if (c.req.method === "GET") return next();
|
|
// All other methods require auth
|
|
return requireAuth(c, next);
|
|
});
|
|
|
|
// API routes
|
|
app.route("/api/auth", authRoutes);
|
|
app.route("/api/items", itemRoutes);
|
|
app.route("/api/categories", categoryRoutes);
|
|
app.route("/api/totals", totalRoutes);
|
|
app.route("/api/images", imageRoutes);
|
|
app.route("/api/settings", settingsRoutes);
|
|
app.route("/api/threads", threadRoutes);
|
|
app.route("/api/setups", setupRoutes);
|
|
|
|
// MCP server (conditionally mounted)
|
|
if (process.env.GEARBOX_MCP !== "false") {
|
|
app.route("/mcp", mcpRoutes);
|
|
}
|
|
|
|
// Serve uploaded images
|
|
app.use("/uploads/*", serveStatic({ root: "./" }));
|
|
|
|
// Serve Vite-built SPA in production
|
|
if (process.env.NODE_ENV === "production") {
|
|
app.use("/*", serveStatic({ root: "./dist/client" }));
|
|
app.get("*", serveStatic({ path: "./dist/client/index.html" }));
|
|
}
|
|
|
|
export default { port: 3000, fetch: app.fetch };
|
|
export { app };
|