main.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import express from 'express';
  2. import expressWs from 'express-ws';
  3. import dotenv from 'dotenv';
  4. import cookieParser from 'cookie-parser';
  5. import Logs from './libs/logs.js';
  6. import authRoutes from './routes/auth.js';
  7. import gamesRoutes from './routes/games.js';
  8. import localesRoutes from './routes/locales.js';
  9. import platformsRoutes from './routes/platforms.js';
  10. import partnerGateRoutes from './routes/partnerGate.js';
  11. import requireAuth from './middleware/requireAuth.js';
  12. const app = express();
  13. const wsInstance = expressWs(app);
  14. dotenv.config();
  15. // 添加 CORS 支持.env
  16. app.use((req, res, next) => {
  17. res.header('Access-Control-Allow-Origin', '*');
  18. res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  19. res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  20. if (req.method === 'OPTIONS') {
  21. return res.sendStatus(200);
  22. }
  23. next();
  24. });
  25. // 中间件
  26. app.use(express.json({ limit: '10mb' }));
  27. app.use(cookieParser());
  28. app.use((req, res, next) => {
  29. res.badRequest = (data, msg) => {
  30. if (!msg && typeof data === 'string') {
  31. msg = data;
  32. data = undefined;
  33. }
  34. return res.status(400).json({ statusCode: 400, code: -1, message: msg ?? 'Bad Request', data });
  35. }
  36. res.unauthorized = (data, msg) => {
  37. if (!msg && typeof data === 'string') {
  38. msg = data;
  39. data = undefined;
  40. }
  41. return res.status(401).json({ statusCode: 401, code: -1, message: msg ?? 'Unauthorized', data });
  42. }
  43. res.notFound = (data, msg) => {
  44. if (!msg && typeof data === 'string') {
  45. msg = data;
  46. data = undefined;
  47. }
  48. return res.status(404).json({ statusCode: 404, code: -1, message: msg ?? 'Not Found', data });
  49. }
  50. res.serverError = (data, msg) => {
  51. if (!msg && typeof data === 'string') {
  52. msg = data;
  53. data = undefined;
  54. }
  55. return res.status(500).json({ statusCode: 500, code: -1, message: msg ?? 'Internal Server Error', data });
  56. }
  57. res.sendSuccess = (data, msg) => {
  58. const response = { statusCode: 200, code: 0, message: msg ?? 'OK' }
  59. if (data) {
  60. response.data = data;
  61. }
  62. return res.status(200).json(response);
  63. }
  64. res.sendError = (err) => {
  65. if (err.cause == 400) {
  66. return res.badRequest(err.data, err.message);
  67. }
  68. else if (err.cause == 401) {
  69. return res.unauthorized(err.data, err.message);
  70. }
  71. else if (err.cause == 404) {
  72. return res.notFound(err.data, err.message);
  73. }
  74. else {
  75. return res.serverError(err.data, err.message);
  76. }
  77. }
  78. next();
  79. });
  80. app.use('/api/auth', authRoutes);
  81. app.use('/api/games', requireAuth, gamesRoutes);
  82. app.use('/api/locales', requireAuth, localesRoutes);
  83. app.use('/api/platforms', requireAuth, platformsRoutes);
  84. app.use('/api/partner', partnerGateRoutes);
  85. // 启动服务
  86. const PORT = process.env.PORT || 9020;
  87. app.listen(PORT, () => Logs.out(`Server running on port ${PORT}`));