68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import express from 'express'
|
|
import cors from 'cors'
|
|
import bodyParser from 'body-parser'
|
|
import swaggerJsdoc from "swagger-jsdoc"
|
|
import swaggerUi from "swagger-ui-express"
|
|
import { api } from './routes/api.routes'
|
|
import { startDatabase } from './database'
|
|
import { order } from './routes/order.routes'
|
|
import { account } from './routes/account.routes'
|
|
import { concert } from './routes/concert.routes'
|
|
import { band } from './routes/band.routes'
|
|
import { genre } from './routes/genre.routes'
|
|
import { location } from './routes/location.routes'
|
|
import { city } from './routes/city.routes'
|
|
import { exercises } from './routes/exercise.routes'
|
|
import { files } from './routes/files.routes'
|
|
import swaggerFile from './swagger.json'
|
|
|
|
const app = express()
|
|
const port = 3000
|
|
|
|
// Same origin access
|
|
app.use(cors())
|
|
|
|
// Process JSON parameter
|
|
app.use(bodyParser.json())
|
|
|
|
// Create database and tables
|
|
startDatabase()
|
|
|
|
// Static files
|
|
const path = require('path')
|
|
app.use('/static', express.static(path.join(__dirname, 'images')))
|
|
app.use("/exercises", exercises)
|
|
app.use("/files", files)
|
|
|
|
// Add delay for more realistic response times
|
|
app.use((req, res, next) => {
|
|
setTimeout(next, Math.floor((Math.random() * 500) + 100))
|
|
})
|
|
|
|
// Routes
|
|
app.use("/api", api)
|
|
app.use("/bands", band)
|
|
app.use("/locations", location)
|
|
app.use("/genres", genre)
|
|
app.use("/orders", order)
|
|
app.use("/accounts", account)
|
|
app.use("/cities", city)
|
|
app.use("/concerts", concert)
|
|
|
|
|
|
// Swagger API documentation
|
|
const specs = swaggerJsdoc(swaggerFile);
|
|
|
|
app.use(
|
|
"/api-docs",
|
|
swaggerUi.serve,
|
|
swaggerUi.setup(specs, { explorer: true })
|
|
)
|
|
|
|
|
|
// Start server
|
|
const server = app.listen(port, () => {
|
|
console.log(`Server is running and listening to port ${port}`)
|
|
})
|
|
|
|
module.exports = server |