89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
import { serve } from "@hono/node-server";
|
|
import tracer from "dd-trace";
|
|
import { Hono } from "hono";
|
|
import { cors } from "hono/cors";
|
|
import { logger } from "hono/logger";
|
|
import path from "path";
|
|
import Stripe from "stripe";
|
|
import { fileURLToPath } from "url";
|
|
import { createConfig } from "./config.js";
|
|
import { initializeMiddlewares } from "./middleware.js";
|
|
import { getPublicRouter } from "./public.js";
|
|
import { getMainRouter } from "./routers.js";
|
|
import { loadSecrets, type Secrets } from "./secrets.js";
|
|
import { createStripeSync } from "./stripeSync.js";
|
|
import { createTransporter } from "./transporter.js";
|
|
|
|
tracer.init({
|
|
logInjection: true,
|
|
});
|
|
|
|
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
|
|
const __dirname = path.dirname(__filename); // get the name of the directory
|
|
|
|
async function startServer(secrets: Secrets) {
|
|
// Load configuration (will load from Google Secret Manager in production/staging)
|
|
console.log("Initializing application configuration...");
|
|
const config = createConfig(secrets);
|
|
console.log(`✓ Configuration initialized for ${config.NODE_ENV} environment`);
|
|
|
|
// Initialize middlewares
|
|
const middlewares = initializeMiddlewares(config);
|
|
|
|
// Initialize Stripe client
|
|
const stripe = new Stripe(config.STRIPE_SECRET_KEY || "", {
|
|
apiVersion: "2025-10-29.clover",
|
|
});
|
|
|
|
// Initialize Stripe Sync
|
|
const stripeSync = createStripeSync(config);
|
|
|
|
// Initialize transporter
|
|
const transporter = createTransporter(config);
|
|
|
|
const app = new Hono();
|
|
|
|
app.use(logger());
|
|
|
|
app.use("*", async (c, next) => {
|
|
const corsMiddleware = cors({
|
|
origin: config.CORS_ORIGIN.split(","),
|
|
allowHeaders: [
|
|
"Authorization",
|
|
"Content-Type",
|
|
"Access-Control-Allow-Origin",
|
|
"Access-Control-Allow-Credentials",
|
|
"Access-Control-Expose-Headers",
|
|
],
|
|
allowMethods: ["GET", "POST", "PATCH", "OPTIONS", "DELETE"],
|
|
exposeHeaders: ["set-cookie"],
|
|
credentials: true,
|
|
});
|
|
|
|
return corsMiddleware(c, next);
|
|
});
|
|
|
|
app.route("/api/v1", getMainRouter(middlewares, config, stripe, stripeSync, transporter));
|
|
app.route("/api/public", getPublicRouter(middlewares));
|
|
|
|
serve(
|
|
{
|
|
fetch: app.fetch,
|
|
port: 8080,
|
|
},
|
|
(info) => {
|
|
console.log(`✓ Server is running on http://localhost:${info.port}`);
|
|
}
|
|
);
|
|
// TODO: Add health check endpoint
|
|
}
|
|
|
|
loadSecrets()
|
|
.then((secrets) => {
|
|
console.log("Secrets loaded successfully");
|
|
startServer(secrets);
|
|
})
|
|
.catch((error) => {
|
|
console.error("Failed to load secrets:", error);
|
|
process.exit(1);
|
|
});
|