diff --git a/server/getApi.ts b/server/getApi.ts deleted file mode 100644 index ba3ae06b9..000000000 --- a/server/getApi.ts +++ /dev/null @@ -1,82 +0,0 @@ -import cookieParser from 'cookie-parser'; -import CrashReporter from './lib/CrashReporter'; -import ErrorHandler from './lib/misc/ErrorHandler'; -import morgan from 'morgan'; -import RequireAuthentication from './middleware/RequireAuthentication'; -import express, { Express } from 'express'; -import { ALLOWED_ORIGINS, BUILD_DIR, INDEX_FILE } from './lib/constants'; - -import _settings from './routes/settings'; -import checks from './routes/checks'; -import version from './routes/version'; -import upload from './routes/upload'; -import users from './routes/users'; -import notion from './routes/notion'; -import rules from './routes/rules'; -import download from './routes/download/u'; -import favorite from './routes/favorite'; -import { login } from './routes/login'; -import { search } from './routes/search'; - -const getApi = function (templateDir: string): Express { - const app = express(); - - app.use(express.json()); - app.use(cookieParser()); - if (process.env.NODE_ENV === 'production') { - CrashReporter.Configure(app); - } - - if (process.env.SPACES_DEFAULT_BUCKET_NAME !== 'dev.2anki.net') { - app.use(morgan('combined')); - } - - app.use('/templates', express.static(templateDir)); - app.use(express.static(BUILD_DIR)); - app.use('/checks', checks); - app.use('/version', version); - - app.get('/search*', RequireAuthentication, search); - app.get('/login', login); - app.get('/api/uploads*', RequireAuthentication, upload); - - app.use('/api/upload', upload); - app.use('/api/users', users); - app.use('/api/notion', notion); - app.use('/api/rules', rules); - app.use('/api/settings', _settings); - app.use('/api/download', download); - app.use('/api/favorite', favorite); - - // Note: this has to be the last handler - app.get('*', (_req, res) => { - res.sendFile(INDEX_FILE); - }); - - app.use( - ( - _req: express.Request, - res: express.Response, - next: express.NextFunction - ) => { - res.header('Access-Control-Allow-Origin', ALLOWED_ORIGINS.join(',')); - res.header( - 'Access-Control-Allow-Headers', - 'Origin, X-Requested-With, Content-Type, Accept, Content-Disposition' - ); - next(); - } - ); - - if (process.env.NODE_ENV === 'production') { - CrashReporter.AddErrorHandler(app); - } - - app.use((err: Error, _req: express.Request, res: express.Response) => - ErrorHandler(res, err) - ); - - return app; -}; - -export { getApi }; diff --git a/server/routes/login/index.ts b/server/routes/login/index.ts deleted file mode 100644 index e5553f443..000000000 --- a/server/routes/login/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Handler } from 'express'; -import { INDEX_FILE } from '../../lib/constants'; -import TokenHandler from '../../lib/misc/TokenHandler'; - -const login = function (): Handler { - return async (req, res) => { - const user = await TokenHandler.GetUserFrom(req.cookies.token); - if (!user) { - res.sendFile(INDEX_FILE); - } else { - res.redirect('/search'); - } - }; -}; - -export { login }; diff --git a/server/routes/search/index.ts b/server/routes/search/index.ts deleted file mode 100644 index 846db5467..000000000 --- a/server/routes/search/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Handler } from 'express'; -import { INDEX_FILE } from '../../lib/constants'; - -const search = function (): Handler { - return (_req, res) => { - res.sendFile(INDEX_FILE); - }; -}; - -export { search }; diff --git a/server/server.ts b/server/server.ts index 025b280c5..865b07694 100644 --- a/server/server.ts +++ b/server/server.ts @@ -1,13 +1,12 @@ -import ConversionJob from './lib/jobs/ConversionJob'; -import DB from './lib/storage/db'; import { existsSync } from 'fs'; -import { getApi } from './getApi'; import path from 'path'; -import { ScheduleCleanup } from './lib/jobs/JobHandler'; -import { IsDebug } from './lib/debug'; -import KnexConfig from './KnexConfig'; + +import morgan from 'morgan'; +import express from 'express'; +import cookieParser from 'cookie-parser'; import * as dotenv from 'dotenv'; +import { IsDebug } from './lib/debug'; if (IsDebug()) { const localEnvFile = path.join(__dirname, '.env'); if (existsSync(localEnvFile)) { @@ -15,27 +14,115 @@ if (IsDebug()) { } } -(async function (): Promise { - try { - const templateDir = path.join(__dirname, 'templates'); - const api = getApi(templateDir); +import { ALLOWED_ORIGINS, BUILD_DIR, INDEX_FILE } from './lib/constants'; +import ErrorHandler from './lib/misc/ErrorHandler'; - await DB.raw('SELECT 1'); - console.info('DB is ready'); +// Server Endpoints +import _settings from './routes/settings'; +import checks from './routes/checks'; +import version from './routes/version'; +import upload from './routes/upload'; +import users from './routes/users'; +import notion from './routes/notion'; +import rules from './routes/rules'; +import download from './routes/download/u'; +import favorite from './routes/favorite'; + +import DB from './lib/storage/db'; +import KnexConfig from './KnexConfig'; +import TokenHandler from './lib/misc/TokenHandler'; +import CrashReporter from './lib/CrashReporter'; +import { ScheduleCleanup } from './lib/jobs/JobHandler'; +import ConversionJob from './lib/jobs/ConversionJob'; +import RequireAuthentication from './middleware/RequireAuthentication'; + +function serve() { + const templateDir = path.join(__dirname, 'templates'); + const app = express(); + + app.use(express.json()); + app.use(cookieParser()); + if (process.env.NODE_ENV === 'production') { + CrashReporter.Configure(app); + } + + if (process.env.SPACES_DEFAULT_BUCKET_NAME !== 'dev.2anki.net') { + app.use(morgan('combined')); + } - const cwd = process.cwd(); - if (process.env.MIGRATIONS_DIR) { - process.chdir(path.join(process.env.MIGRATIONS_DIR, '..')); + app.use('/templates', express.static(templateDir)); + app.use(express.static(BUILD_DIR)); + app.use('/checks', checks); + app.use('/version', version); + + app.get('/search*', RequireAuthentication, async (_req, res) => { + res.sendFile(INDEX_FILE); + }); + + app.get('/login', async (req, res) => { + const user = await TokenHandler.GetUserFrom(req.cookies.token); + if (!user) { + res.sendFile(INDEX_FILE); + } else { + res.redirect('/search'); } - ScheduleCleanup(DB); - /* @ts-ignore */ - await DB.migrate.latest(KnexConfig); + }); + app.get('/api/uploads*', RequireAuthentication, upload); + + app.use('/api/upload', upload); + app.use('/api/users', users); + app.use('/api/notion', notion); + app.use('/api/rules', rules); + app.use('/api/settings', _settings); + app.use('/api/download', download); + app.use('/api/favorite', favorite); + + // Note: this has to be the last handler + app.get('*', (_req, res) => { + res.sendFile(INDEX_FILE); + }); + + app.use( + ( + _req: express.Request, + res: express.Response, + next: express.NextFunction + ) => { + res.header('Access-Control-Allow-Origin', ALLOWED_ORIGINS.join(',')); + res.header( + 'Access-Control-Allow-Headers', + 'Origin, X-Requested-With, Content-Type, Accept, Content-Disposition' + ); + next(); + } + ); + + if (process.env.NODE_ENV === 'production') { + CrashReporter.AddErrorHandler(app); + } + + app.use((err: Error, _req: express.Request, res: express.Response) => + ErrorHandler(res, err) + ); + process.on('uncaughtException', (err: Error) => { + console.error(err); + }); + + DB.raw('SELECT 1').then(() => { + console.info('DB is ready'); + }); + const cwd = process.cwd(); + if (process.env.MIGRATIONS_DIR) { + process.chdir(path.join(process.env.MIGRATIONS_DIR, '..')); + } + ScheduleCleanup(DB); + /* @ts-ignore */ + DB.migrate.latest(KnexConfig).then(() => { process.chdir(cwd); process.env.SECRET ||= 'victory'; const port = process.env.PORT || 2020; - - const server = api.listen(port, () => { + const server = app.listen(port, () => { console.info(`🟢 Running on http://localhost:${port}`); }); @@ -50,15 +137,13 @@ if (IsDebug()) { await HandleStartedJobs(); }); }); - process.on('SIGINT', async () => { server.close(async () => { console.debug('HTTP server closed'); await HandleStartedJobs(); }); }); - } catch (ex: unknown) { - console.info('uncaught exception'); - console.error(ex); - } -})(); + }); +} + +serve();