Upload 279 files
Browse files- artifacts/api-server/src/app.ts +63 -63
artifacts/api-server/src/app.ts
CHANGED
|
@@ -1,63 +1,63 @@
|
|
| 1 |
-
import express, { type Express } from "express";
|
| 2 |
-
import cors from "cors";
|
| 3 |
-
import cookieParser from "cookie-parser";
|
| 4 |
-
import pinoHttp from "pino-http";
|
| 5 |
-
import path from "path";
|
| 6 |
-
import { fileURLToPath } from "url";
|
| 7 |
-
import router from "./routes";
|
| 8 |
-
import openaiRouter from "./routes/openai";
|
| 9 |
-
import publicRouter from "./routes/public";
|
| 10 |
-
import accountsRouter from "./routes/accounts";
|
| 11 |
-
import { logger } from "./lib/logger";
|
| 12 |
-
|
| 13 |
-
const __filename = fileURLToPath(import.meta.url);
|
| 14 |
-
const __dirname = path.dirname(__filename);
|
| 15 |
-
|
| 16 |
-
const app: Express = express();
|
| 17 |
-
|
| 18 |
-
app.use(
|
| 19 |
-
pinoHttp({
|
| 20 |
-
logger,
|
| 21 |
-
serializers: {
|
| 22 |
-
req(req) {
|
| 23 |
-
return {
|
| 24 |
-
id: req.id,
|
| 25 |
-
method: req.method,
|
| 26 |
-
url: req.url?.split("?")[0],
|
| 27 |
-
};
|
| 28 |
-
},
|
| 29 |
-
res(res) {
|
| 30 |
-
return {
|
| 31 |
-
statusCode: res.statusCode,
|
| 32 |
-
};
|
| 33 |
-
},
|
| 34 |
-
},
|
| 35 |
-
}),
|
| 36 |
-
);
|
| 37 |
-
|
| 38 |
-
app.use(cors({ credentials: true, origin: true }));
|
| 39 |
-
app.use(cookieParser());
|
| 40 |
-
app.use(express.json({ limit: "20mb" }));
|
| 41 |
-
app.use(express.urlencoded({ extended: true, limit: "20mb" }));
|
| 42 |
-
|
| 43 |
-
app.use("/api/public", publicRouter);
|
| 44 |
-
app.use("/api/admin/accounts", accountsRouter);
|
| 45 |
-
app.use("/api", router);
|
| 46 |
-
app.use("/v1", openaiRouter);
|
| 47 |
-
app.use("/api/v1", openaiRouter);
|
| 48 |
-
|
| 49 |
-
// ζδΎεη«―ιζ
ζͺζ‘
|
| 50 |
-
const frontendDistPath = path.join(__dirname, "../../image-gen/dist/public");
|
| 51 |
-
app.use(express.static(frontendDistPath));
|
| 52 |
-
|
| 53 |
-
// SPA fallback - εͺθηι API θ«ζ±
|
| 54 |
-
app.use((req, res, next) => {
|
| 55 |
-
// ε¦ζζ― API θ«ζ±δ½ζ²ζθ’«θηοΌθΏε 404
|
| 56 |
-
if (req.path.startsWith('/api') || req.path.startsWith('/v1')) {
|
| 57 |
-
return res.status(404).json({ error: 'API endpoint not found' });
|
| 58 |
-
}
|
| 59 |
-
// ε
Άδ»θ«ζ±θΏε SPA η index.html
|
| 60 |
-
res.sendFile(path.join(frontendDistPath, "index.html"));
|
| 61 |
-
});
|
| 62 |
-
|
| 63 |
-
export default app;
|
|
|
|
| 1 |
+
import express, { type Express } from "express";
|
| 2 |
+
import cors from "cors";
|
| 3 |
+
import cookieParser from "cookie-parser";
|
| 4 |
+
import pinoHttp from "pino-http";
|
| 5 |
+
import path from "path";
|
| 6 |
+
import { fileURLToPath } from "url";
|
| 7 |
+
import router from "./routes";
|
| 8 |
+
import openaiRouter from "./routes/openai";
|
| 9 |
+
import publicRouter from "./routes/public";
|
| 10 |
+
import accountsRouter from "./routes/accounts";
|
| 11 |
+
import { logger } from "./lib/logger";
|
| 12 |
+
|
| 13 |
+
const __filename = fileURLToPath(import.meta.url);
|
| 14 |
+
const __dirname = path.dirname(__filename);
|
| 15 |
+
|
| 16 |
+
const app: Express = express();
|
| 17 |
+
|
| 18 |
+
app.use(
|
| 19 |
+
pinoHttp({
|
| 20 |
+
logger,
|
| 21 |
+
serializers: {
|
| 22 |
+
req(req) {
|
| 23 |
+
return {
|
| 24 |
+
id: req.id,
|
| 25 |
+
method: req.method,
|
| 26 |
+
url: req.url?.split("?")[0],
|
| 27 |
+
};
|
| 28 |
+
},
|
| 29 |
+
res(res) {
|
| 30 |
+
return {
|
| 31 |
+
statusCode: res.statusCode,
|
| 32 |
+
};
|
| 33 |
+
},
|
| 34 |
+
},
|
| 35 |
+
}),
|
| 36 |
+
);
|
| 37 |
+
|
| 38 |
+
app.use(cors({ credentials: true, origin: true }));
|
| 39 |
+
app.use(cookieParser());
|
| 40 |
+
app.use(express.json({ limit: "20mb" }));
|
| 41 |
+
app.use(express.urlencoded({ extended: true, limit: "20mb" }));
|
| 42 |
+
|
| 43 |
+
app.use("/api/public", publicRouter);
|
| 44 |
+
app.use("/api/admin/accounts", accountsRouter);
|
| 45 |
+
app.use("/api", router);
|
| 46 |
+
app.use("/v1", openaiRouter);
|
| 47 |
+
app.use("/api/v1", openaiRouter);
|
| 48 |
+
|
| 49 |
+
// ζδΎεη«―ιζ
ζͺζ‘
|
| 50 |
+
const frontendDistPath = path.join(__dirname, "../../image-gen/dist/public");
|
| 51 |
+
app.use(express.static(frontendDistPath));
|
| 52 |
+
|
| 53 |
+
// SPA fallback - εͺθηι API θ«ζ±
|
| 54 |
+
app.use((req, res, next) => {
|
| 55 |
+
// ε¦ζζ― API θ«ζ±δ½ζ²ζθ’«θηοΌθΏε 404
|
| 56 |
+
if (req.path.startsWith('/api') || req.path.startsWith('/v1')) {
|
| 57 |
+
return res.status(404).json({ error: 'API endpoint not found' });
|
| 58 |
+
}
|
| 59 |
+
// ε
Άδ»θ«ζ±θΏε SPA η index.html
|
| 60 |
+
res.sendFile(path.join(frontendDistPath, "index.html"));
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
export default app;
|