43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import express from 'express'
|
|
import cors from 'cors'
|
|
import rateLimit from 'express-rate-limit'
|
|
import swaggerUi from 'swagger-ui-express'
|
|
import { swaggerSpec } from './config/swagger'
|
|
import { getSearchResults, getLinkForResource } from './controllers/searchController'
|
|
import { RedisService } from './services/redisService'
|
|
import logger from './lib/logger'
|
|
|
|
const app = express()
|
|
|
|
// 初始化 Redis 服务
|
|
const redisService = RedisService.getInstance();
|
|
redisService.initialize().catch(err => {
|
|
logger.error('Redis 服务初始化失败,应用启动终止', { error: err })
|
|
process.exit(1)
|
|
})
|
|
|
|
app.use(cors())
|
|
app.use(express.json())
|
|
app.use(rateLimit({ windowMs: 1000, max: 5 }))
|
|
|
|
// Swagger UI
|
|
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))
|
|
|
|
// API Documentation in JSON format
|
|
app.get('/api-docs.json', (req, res) => {
|
|
res.setHeader('Content-Type', 'application/json')
|
|
res.send(swaggerSpec)
|
|
})
|
|
|
|
// 搜索接口
|
|
app.get('/api/search', getSearchResults)
|
|
|
|
// 生成临时链接接口
|
|
app.get('/api/link/:resourceId', getLinkForResource)
|
|
|
|
// 健康检查
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'ok' })
|
|
})
|
|
|
|
export default app
|