190 lines
5.8 KiB
TypeScript
190 lines
5.8 KiB
TypeScript
import path from 'path'
|
|
import bodyParser from 'body-parser'
|
|
import express from 'express'
|
|
import mongoose from 'mongoose'
|
|
import AdminTagController from './controllers/AdminTagController'
|
|
import AdminOrganizationController from './controllers/AdminOrganizationController'
|
|
import DefaultController from './controllers/DefaultController'
|
|
import MediaController from './controllers/MediaController'
|
|
import dotenv from 'dotenv'
|
|
import DelegateController from './controllers/DelegateController'
|
|
import AdminAuthMiddleware from './middlewares/AdminAuthMiddleware'
|
|
import DelegateAuthMiddleware from './middlewares/DelegateAuthMiddleware'
|
|
import PublicController from './controllers/PublicController'
|
|
import cors from 'cors'
|
|
import twig from 'twig'
|
|
import EmailService from './EmailService'
|
|
import ErrorController from './controllers/ErrorController'
|
|
import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
|
|
import ExpressRedisCache from 'express-redis-cache'
|
|
import Utils from './Utils'
|
|
|
|
process.env.TZ = "Europe/Paris"
|
|
|
|
process.on('unhandledRejection', (err) => {
|
|
console.error(err)
|
|
console.log('unhandledRejection!')
|
|
process.exit()
|
|
})
|
|
|
|
dotenv.config({
|
|
path: __dirname + '/../.env'
|
|
})
|
|
|
|
const app: express.Application = express()
|
|
const host: string = process.env.HOST === undefined ? '127.0.0.1' : process.env.HOST
|
|
const port: number = 8001
|
|
|
|
if (process.env.DISABLE_TWIG_CACHE === 'true') {
|
|
twig.cache(false)
|
|
}
|
|
|
|
const cache = ExpressRedisCache({
|
|
host: process.env.REDIS_HOST,
|
|
port: process.env.REDIS_PORT,
|
|
auth_pass: process.env.REDIS_PASSWORD,
|
|
prefix: process.env.REDIS_PREFIX
|
|
})
|
|
|
|
const cacheDuration = {
|
|
home: 3600 * 0.75,
|
|
countdown: 3600 * 2,
|
|
page: 3600 * 6
|
|
}
|
|
|
|
// reset cache
|
|
cache.del('*', (err, deleted) => {
|
|
console.log('> App: Reset cache', err, deleted)
|
|
})
|
|
|
|
let main = async () => {
|
|
EmailService.init()
|
|
|
|
mongoose.connection.on('error', err => {
|
|
console.error(err)
|
|
})
|
|
|
|
mongoose.connect(
|
|
process.env.MONGO_URI === undefined ? 'mongodb://root:root@localhost:27017/forumvirt' : process.env.MONGO_URI,
|
|
{
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true
|
|
}
|
|
).then(() => {
|
|
console.log('> App: Connected to mongodb')
|
|
})
|
|
|
|
app.set('twig options', {
|
|
allow_async: true,
|
|
strict_variables: false
|
|
})
|
|
app.set('cache', cache)
|
|
|
|
app.use(cors())
|
|
app.use(bodyParser.json())
|
|
|
|
app.get(
|
|
'/',
|
|
(req, res, next) => {
|
|
if (process.env.OPEN_DATE == null) {
|
|
next()
|
|
return
|
|
}
|
|
// redirect to countdown if needed
|
|
let isProposed = Utils.isStrUsable(req.query, 'only')
|
|
let byPass = req.query.bypass != null
|
|
let target: any = new Date(process.env.OPEN_DATE)
|
|
let now: any = new Date()
|
|
if (!byPass && !isProposed && target > now) {
|
|
return res.redirect('/c')
|
|
} else {
|
|
next()
|
|
}
|
|
|
|
let withId = req.query.only != null
|
|
if (byPass || withId) {
|
|
res.use_express_redis_cache = false
|
|
}
|
|
},
|
|
cache.route('/home', cacheDuration.home),
|
|
PublicController.home
|
|
)
|
|
app.get('/c', cache.route('/c', cacheDuration.countdown), PublicController.countdown)
|
|
app.get('/association/:slug', cache.route(cacheDuration.page), PublicController.organization)
|
|
app.get('/a-propos', PublicController.about)
|
|
app.get('/mentions-legales', PublicController.legals)
|
|
|
|
app.get('/icon/:id', DefaultController.viewIcon)
|
|
app.get('/email', DefaultController.sendEmail)
|
|
|
|
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()
|
|
.post('/import', AdminOrganizationController.import)
|
|
.get('/', AdminOrganizationController.getMany)
|
|
.get('/:id', AdminOrganizationController.getOne)
|
|
.post('', AdminOrganizationController.store)
|
|
.put('/:id', AdminOrganizationController.update)
|
|
.delete('/:id', AdminOrganizationController.destroy)
|
|
.post('/:id/reset-token', AdminOrganizationController.resetToken)
|
|
.post('/:id/send-email-token', AdminOrganizationController.sendEmailToken)
|
|
.post('/:id/approve', AdminOrganizationController.approve)
|
|
.post('/:id/reject', AdminOrganizationController.reject)
|
|
)
|
|
)
|
|
|
|
app.use('/delegate', express.Router()
|
|
.use(DelegateAuthMiddleware.handle)
|
|
.get('/', DelegateController.get)
|
|
.get('/size', DelegateController.getCurrentSize)
|
|
//.post('/test', DelegateController.test)
|
|
.put('/', DelegateController.update)
|
|
.post('/submit', DelegateController.submit)
|
|
.post('/thumbnail', DelegateController.uploadThumbnail())
|
|
.post('/cover', DelegateController.uploadCover())
|
|
.post('/medias', DelegateController.uploadMedias())
|
|
.delete('/', DelegateController.destroy)
|
|
)
|
|
|
|
app.use('/proxy-s3', createProxyMiddleware({
|
|
target: 'https://fva-condorcet.s3.fr-par.scw.cloud',
|
|
changeOrigin: true,
|
|
pathRewrite: {
|
|
'^/proxy-s3/': '/'
|
|
},
|
|
}))
|
|
|
|
/*
|
|
|
|
.put('/tags/:id', AdminTagController.update)
|
|
.put('/tags/:id', AdminTagController.destroy)
|
|
.use('/organizations', () => express.Router()
|
|
.get('/', AdminOrganizationController.getMany)
|
|
)
|
|
*/
|
|
|
|
//app.post('/api/media', MediaController.uploadRoute())
|
|
//app.delete('/api/media/:key', MediaController.delete)
|
|
//const assetsPath: string = process.env.ASSETS_PATH === undefined ? './assets/development' : process.env.ASSETS_PATH
|
|
|
|
app.use('/static', express.static(path.resolve('./assets/development')))
|
|
|
|
app.get('/500', ErrorController.internalError)
|
|
|
|
app.use(ErrorController.handleServerError)
|
|
|
|
app.use(ErrorController.notFoundHandler())
|
|
|
|
app.listen(port, host, () => {
|
|
console.log(`> App: API listening on ${host}:${port}`)
|
|
})
|
|
}
|
|
|
|
main()
|