import express , { Application , Request , Response } from 'express' ; import next from 'next' ; import path from 'path' ; import bodyParser from 'body-parser' ; import cookieParser from 'cookie-parser' ; import cors from 'cors' ; import session from 'express-session' ; import multer from 'multer' ; import { buildUrl } from '@contentpi/utils' ; import { getFileInfo , availableLanguages } from '@contentpi/core' ; import { isConnected } from './shared/lib/middlewares/user' ; import config from './config' ; const dev = process . env . NODE_ENV !== 'production' ; const nextApp = next ( { dev } ) ; const handle = nextApp . getRequestHandler ( ) ; const storage = multer . diskStorage ( { destination : ( req , file , cb ) : any => { const { extension } = getFileInfo ( file . originalname ) ; let dir = 'public/files' ; const isDocument = config . files . types . documents . includes ( extension ) ; const isImage = config . files . types . images . includes ( extension ) ; const isVideo = config . files . types . videos . includes ( extension ) ; if ( isDocument ) { dir += '/documents' ; } if ( isImage ) { dir += '/images' ; } if ( isVideo ) { dir += '/videos' ; } return cb ( null , dir ) ; } , filename : ( req , file , cb ) : any => cb ( null , req . params . fileName ) } ) ; const upload = multer ( { storage } ) . single ( 'file' ) ; nextApp . prepare ( ) . then ( ( ) => { const app = express ( ) ; app . use ( express . static ( path . join ( __dirname , '../public' ) ) ) ; app . use ( session ( { resave : false , saveUninitialized : true , secret : config . security . secretKey } ) ) ; app . use ( bodyParser . json ( ) ) ; app . use ( bodyParser . urlencoded ( { extended : false } ) ) ; app . use ( cookieParser ( config . security . secretKey ) ) ; app . use ( cors ( { credentials : true , origin : true } ) ) ; app . post ( '/upload/:fileName' , ( req , res ) => { upload ( req , res , ( err ) => { if ( err instanceof multer . MulterError ) { return res . status ( 500 ) . json ( err ) ; } if ( err ) { return res . status ( 500 ) . json ( err ) ; } return res . status ( 200 ) . send ( req . file ) ; } ) ; } ) ; app . get ( `/:language(${ availableLanguages ( ) })/login` , isConnected ( false ) , ( req , res ) => { const { language = config . languages . default } = req . params ; return nextApp . render ( req , res , `/${ language }/login` , { language } ) ; } ) ; app . get ( '/login' , isConnected ( false ) , ( req , res ) => { res . redirect ( `${ config . languages . default }/login` ) ; } ) ; app . get ( `/:language(${ availableLanguages ( ) })/logout` , ( req , res ) => { const redirect = req . query . redirectTo || '/' ; res . clearCookie ( 'at' ) ; res . redirect ( redirect ) ; } ) ; app . get ( `/logout` , ( req , res ) => { const redirect = req . query . redirectTo || '/' ; res . clearCookie ( 'at' ) ; res . redirect ( redirect ) ; } ) ; app . use ( `/:language(${ availableLanguages ( ) })/dashboard/:appId?/:stage?/:moduleName?/:section?/:model?` , isConnected ( true , [ 'god' , 'admin' , 'editor' ] , `/${ config . languages . default }/login?redirectTo=/dashboard` ) , ( req , res ) => { const { appId , stage , moduleName , section , model , language = config . languages . default } = req . params ; const entryId = req . query . entryId ? String ( req . query . entryId ) : '' ; const url = buildUrl ( [ language , 'dashboard' , appId , stage , moduleName , section , model ] ) ; return nextApp . render ( req , res , `/${ url }` , { language , entryId } ) ; } ) ; app . get ( '/dashboard' , isConnected ( true , [ 'god' , 'admin' , 'editor' ] , `/${ config . languages . default }/login?redirectTo=/dashboard` ) , ( req , res ) => { res . redirect ( `${ config . languages . default }/dashboard` ) ; } ) ; app . all ( '*' , ( req , res ) => { return handle ( req , res ) ; } ) ; app . listen ( config . server . port ) ; } ) ;