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' console.log('WOOWOWO') dotenv.config({ path: __dirname + '/../.env' }) 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) }) 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') }) app.use(cors()) app.use(bodyParser.json()) 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) 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) .post('/:id/approve', AdminOrganizationController.approve) .post('/:id/reject', AdminOrganizationController.reject) ) ) app.use('/delegate', express.Router() .use(DelegateAuthMiddleware.handle) .get('/', DelegateController.get) .put('/', DelegateController.update) .put('/submit', DelegateController.submit) .delete('/', DelegateController.destroy) ) /* .put('/tags/:id', AdminTagController.update) .put('/tags/:id', AdminTagController.destroy) .use('/organizations', () => express.Router() .get('/', AdminOrganizationController.getMany) ) */ app.post('/api/upload', MediaController.uploadRoute()) app.listen(port, host, () => { console.log(`API listening on ${host}:${port}`) }) } main()