| require('dotenv').config(); |
| const express = require('express'); |
| const cors = require('cors'); |
| const helmet = require('helmet'); |
| const path = require('path'); |
| const mime = require('mime-types'); |
| const apiRouter = require('./routes/api'); |
| const uiRouter = require('./routes/ui'); |
| const swaggerUi = require('swagger-ui-express'); |
| const swaggerDocument = require('./swagger/swagger.json'); |
|
|
| const app = express(); |
| const PORT = process.env.PORT || 7860; |
| const DOMAIN = process.env.DOMAIN || 'localhost'; |
|
|
| |
| app.use(helmet()); |
| app.disable('x-powered-by'); |
|
|
| |
| const corsOptions = { |
| origin: [ |
| `http://${DOMAIN}`, |
| `https://${DOMAIN}`, |
| `http://${DOMAIN}:${PORT}`, |
| `https://${DOMAIN}:${PORT}`, |
| 'http://localhost:7860', |
| 'http://127.0.0.1:7860' |
| ], |
| methods: ['GET', 'POST', 'OPTIONS'], |
| allowedHeaders: ['Content-Type', 'Authorization'], |
| optionsSuccessStatus: 200 |
| }; |
| app.use(cors(corsOptions)); |
|
|
| |
| app.use(express.json()); |
| app.use(express.urlencoded({ extended: true })); |
| app.use(express.static(path.join(__dirname, '../public'))); |
|
|
| |
| app.use('/api/files', express.static(path.join(__dirname, '../uploads'), { |
| setHeaders: (res, filePath) => { |
| const contentType = mime.lookup(filePath) || 'application/octet-stream'; |
| res.setHeader('Content-Type', contentType); |
| } |
| })); |
|
|
| |
| app.use('/api', apiRouter); |
| app.use('/', uiRouter); |
| app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); |
|
|
| |
| app.use((err, req, res, next) => { |
| console.error(err.stack); |
| res.status(500).json({ error: 'Internal Server Error' }); |
| }); |
|
|
| |
| app.listen(PORT, () => { |
| if (DOMAIN === 'localhost') { |
| console.log(`Server running on http://localhost:${PORT}`); |
| console.log(`API Docs: http://localhost:${PORT}/api-docs`); |
| console.log(`Upload UI: http://localhost:${PORT}/upload`); |
| } else { |
| console.log(`Server running on https://${DOMAIN}`); |
| console.log(`API Docs: https://${DOMAIN}/api-docs`); |
| console.log(`Upload UI: https://${DOMAIN}/upload`); |
| } |
| }); |