server/src/app.ts

104 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-06-27 21:23:09 +00:00
import bodyParser from 'body-parser'
2020-06-27 21:23:09 +00:00
import express from 'express'
import mongoose from 'mongoose'
import AdminTagController from './controllers/AdminTagController'
2020-07-10 20:44:01 +00:00
import AdminOrganizationController from './controllers/AdminOrganizationController'
import DefaultController from './controllers/DefaultController'
import MediaController from './controllers/MediaController'
import dotenv from 'dotenv'
2020-07-10 20:44:01 +00:00
import DelegateController from './controllers/DelegateController'
import AdminAuthMiddleware from './middlewares/AdminAuthMiddleware'
import DelegateAuthMiddleware from './middlewares/DelegateAuthMiddleware'
import PublicController from './controllers/PublicController'
import cors from 'cors'
dotenv.config({
path: __dirname + '/../.env'
})
2020-06-27 21:23:09 +00:00
const app: express.Application = express()
const host: string = "0.0.0.0"
const port: number = 8001
let main = async () => {
mongoose.connection.on('error', err => {
console.error(err)
})
2020-06-27 21:23:09 +00:00
mongoose.connect(
process.env.MONGO_URI === undefined ? 'mongodb://root:root@localhost:27017/forumvirt' : process.env.MONGO_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(() => {
console.log('> Connected to mongodb')
})
2020-07-10 20:44:01 +00:00
app.use(cors())
app.use(bodyParser.json())
2020-07-10 20:44:01 +00:00
app.use((err: any, req: express.Request, res: express.Response, next: express.RequestHandler) => {
console.error(err.stack)
res.status(res.statusCode).json({ success: false, error: err.stack })
})
app.get('/', PublicController.home)
app.get('/association/:slug', PublicController.organization)
app.get('/icon/:id', DefaultController.viewIcon)
app.get('/email', DefaultController.sendEmail)
2020-07-10 20:44:01 +00:00
app.use('/admin', express.Router()
.use(AdminAuthMiddleware.handle)
.get('/', DefaultController.success)
.use('/tags', express.Router()
.get('/', AdminTagController.getMany)
.post('/', AdminTagController.store)
.put('/:id', AdminTagController.update)
.delete('/:id', AdminTagController.destroy)
)
.use('/organizations', express.Router()
.get('/', AdminOrganizationController.getMany)
.get('/:id', AdminOrganizationController.getOne)
.post('', AdminOrganizationController.store)
.put('/:id', AdminOrganizationController.update)
.delete('/:id', AdminOrganizationController.destroy)
.post('/:id/reset-token', AdminOrganizationController.resetToken)
2020-07-11 22:42:00 +00:00
.post('/:id/send-email-token', AdminOrganizationController.sendEmailToken)
2020-07-10 20:44:01 +00:00
.post('/:id/approve', AdminOrganizationController.approve)
.post('/:id/reject', AdminOrganizationController.reject)
)
)
app.use('/delegate', express.Router()
.use(DelegateAuthMiddleware.handle)
.get('/', DelegateController.get)
2020-07-14 21:58:55 +00:00
.post('/test', DelegateController.test)
2020-07-10 20:44:01 +00:00
.put('/', DelegateController.update)
2020-07-14 21:58:55 +00:00
.post('/submit', DelegateController.submit)
.post('/thumbnail', DelegateController.uploadThumbnail())
2020-07-10 20:44:01 +00:00
.delete('/', DelegateController.destroy)
)
/*
.put('/tags/:id', AdminTagController.update)
.put('/tags/:id', AdminTagController.destroy)
.use('/organizations', () => express.Router()
.get('/', AdminOrganizationController.getMany)
)
*/
2020-07-14 21:58:55 +00:00
app.post('/api/media', MediaController.uploadRoute())
app.delete('/api/media/:key', MediaController.delete)
app.use(express.static('/apps/forum_server/static'))
2020-07-10 20:44:01 +00:00
app.listen(port, host, () => {
console.log(`API listening on ${host}:${port}`)
})
}
2020-06-27 21:23:09 +00:00
main()