-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
75 lines (66 loc) · 1.62 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import express from 'express'
// import morgan from "morgan"
import 'dotenv/config'
import morgan from 'morgan'
import bodyParser from 'body-parser'
import bearerToken from 'express-bearer-token'
import errorMiddleware from './src/middlewares/error.middleware'
import swaggerUi from 'swagger-ui-express'
import { Umzug, SequelizeStorage } from 'umzug'
import { startListeners } from './src/web3'
import swaggerDocument from './swagger.json'
const app = express()
const port = process.env.PORT ?? 3000
const cors = require('cors')
if (process.env.NODE_ENV === 'production') {
app.use(
cors({
origin: 'http://localhost:3000',
credentials: true,
})
)
} else {
app.use(
cors({
origin: '*',
credentials: false,
})
)
}
app.use(bearerToken())
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
import db from './src/db'
db.sequelize
// .sync({ force: true })
.sync()
.then(() => {
console.log('Synced db.')
})
.then(() => {
const seeder = new Umzug({
migrations: { glob: 'seeders/*.js' },
context: db,
storage: new SequelizeStorage({ sequelize: db.sequelize }),
logger: console,
})
seeder.up()
})
.catch(err => {
console.log('Failed to sync db: ' + err.message)
})
startListeners()
app.use(morgan('tiny'))
app.get('/', async (req, res) => {
res.send('Hello there')
})
require('./src/routes/index.routes.ts')(app)
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
app.use(errorMiddleware)
app.listen(port, () => {
console.log(`Tasks server listening at http://localhost:${port}`)
})