2020-07-16 15:43:18 +00:00
|
|
|
import path from 'path'
|
2020-07-04 21:25:25 +00:00
|
|
|
import bodyParser from 'body-parser'
|
2020-06-27 21:23:09 +00:00
|
|
|
import express from 'express'
|
|
|
|
import mongoose from 'mongoose'
|
2020-07-04 21:25:25 +00:00
|
|
|
import AdminTagController from './controllers/AdminTagController'
|
2020-07-10 20:44:01 +00:00
|
|
|
import AdminOrganizationController from './controllers/AdminOrganizationController'
|
2020-07-04 21:25:25 +00:00
|
|
|
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'
|
2020-07-15 20:32:42 +00:00
|
|
|
import twig from 'twig'
|
2020-07-16 15:43:18 +00:00
|
|
|
import EmailService from './EmailService'
|
2020-07-18 10:43:13 +00:00
|
|
|
import ErrorController from './controllers/ErrorController'
|
2020-07-23 10:43:20 +00:00
|
|
|
import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
|
2020-07-04 21:25:25 +00:00
|
|
|
|
2020-08-03 16:04:22 +00:00
|
|
|
process.env.TZ = "Europe/Paris"
|
|
|
|
|
2020-07-19 13:26:57 +00:00
|
|
|
process.on('unhandledRejection', (err) => {
|
|
|
|
console.error(err)
|
|
|
|
console.log('unhandledRejection!')
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
2020-07-04 21:25:25 +00:00
|
|
|
dotenv.config({
|
|
|
|
path: __dirname + '/../.env'
|
|
|
|
})
|
2020-06-27 21:23:09 +00:00
|
|
|
|
|
|
|
const app: express.Application = express()
|
2020-07-23 10:43:20 +00:00
|
|
|
const host: string = process.env.HOST === undefined ? '127.0.0.1' : process.env.HOST
|
2020-06-27 21:23:09 +00:00
|
|
|
const port: number = 8001
|
|
|
|
|
2020-08-05 12:50:52 +00:00
|
|
|
if (process.env.DISABLE_TWIG_CACHE === 'true') {
|
|
|
|
twig.cache(false)
|
|
|
|
}
|
2020-07-15 20:32:42 +00:00
|
|
|
|
2020-07-04 21:25:25 +00:00
|
|
|
let main = async () => {
|
2020-07-16 15:43:18 +00:00
|
|
|
EmailService.init()
|
|
|
|
|
2020-07-04 21:25:25 +00:00
|
|
|
mongoose.connection.on('error', err => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
2020-06-27 21:23:09 +00:00
|
|
|
|
2020-07-04 21:25:25 +00:00
|
|
|
mongoose.connect(
|
|
|
|
process.env.MONGO_URI === undefined ? 'mongodb://root:root@localhost:27017/forumvirt' : process.env.MONGO_URI,
|
|
|
|
{
|
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true
|
|
|
|
}
|
|
|
|
).then(() => {
|
2020-07-16 15:43:18 +00:00
|
|
|
console.log('> App: Connected to mongodb')
|
2020-07-04 21:25:25 +00:00
|
|
|
})
|
2020-07-18 10:43:13 +00:00
|
|
|
|
2020-07-15 20:32:42 +00:00
|
|
|
app.set("twig options", {
|
|
|
|
allow_async: true,
|
|
|
|
strict_variables: false
|
|
|
|
})
|
2020-07-10 20:44:01 +00:00
|
|
|
app.use(cors())
|
2020-07-04 21:25:25 +00:00
|
|
|
app.use(bodyParser.json())
|
2020-07-10 20:44:01 +00:00
|
|
|
|
|
|
|
app.get('/', PublicController.home)
|
2020-08-03 16:04:22 +00:00
|
|
|
app.get('/c', PublicController.countdown)
|
2020-07-10 20:44:01 +00:00
|
|
|
app.get('/association/:slug', PublicController.organization)
|
2020-07-15 20:32:42 +00:00
|
|
|
app.get('/a-propos', PublicController.about)
|
|
|
|
app.get('/mentions-legales', PublicController.legals)
|
2020-07-10 20:44:01 +00:00
|
|
|
|
|
|
|
app.get('/icon/:id', DefaultController.viewIcon)
|
2020-07-04 21:25:25 +00:00
|
|
|
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()
|
2020-07-17 22:12:11 +00:00
|
|
|
.post('/import', AdminOrganizationController.import)
|
2020-07-10 20:44:01 +00:00
|
|
|
.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-29 20:12:23 +00:00
|
|
|
.get('/size', DelegateController.getCurrentSize)
|
2020-07-16 15:43:18 +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-15 20:32:42 +00:00
|
|
|
.post('/cover', DelegateController.uploadCover())
|
|
|
|
.post('/medias', DelegateController.uploadMedias())
|
2020-07-10 20:44:01 +00:00
|
|
|
.delete('/', DelegateController.destroy)
|
|
|
|
)
|
|
|
|
|
2020-07-23 10:43:20 +00:00
|
|
|
app.use('/proxy-s3', createProxyMiddleware({
|
|
|
|
target: 'https://fva-condorcet.s3.fr-par.scw.cloud',
|
|
|
|
changeOrigin: true,
|
|
|
|
pathRewrite: {
|
|
|
|
'^/proxy-s3/': '/'
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
2020-07-10 20:44:01 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
.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
|
|
|
|
2020-07-16 15:43:18 +00:00
|
|
|
//app.post('/api/media', MediaController.uploadRoute())
|
2020-07-15 20:32:42 +00:00
|
|
|
//app.delete('/api/media/:key', MediaController.delete)
|
2020-08-30 12:51:05 +00:00
|
|
|
//const assetsPath: string = process.env.ASSETS_PATH === undefined ? './assets/development' : process.env.ASSETS_PATH
|
|
|
|
|
|
|
|
app.use('/static', express.static(path.resolve('./assets/development')))
|
2020-07-14 21:58:55 +00:00
|
|
|
|
2020-07-18 10:43:13 +00:00
|
|
|
app.get('/500', ErrorController.internalError)
|
|
|
|
|
2020-07-19 13:26:57 +00:00
|
|
|
app.use(ErrorController.handleServerError)
|
2020-07-18 10:43:13 +00:00
|
|
|
|
|
|
|
app.use(ErrorController.notFoundHandler())
|
|
|
|
|
2020-07-10 20:44:01 +00:00
|
|
|
app.listen(port, host, () => {
|
2020-07-16 15:43:18 +00:00
|
|
|
console.log(`> App: API listening on ${host}:${port}`)
|
2020-07-04 21:25:25 +00:00
|
|
|
})
|
|
|
|
}
|
2020-06-27 21:23:09 +00:00
|
|
|
|
2020-07-19 13:26:57 +00:00
|
|
|
main()
|