diff --git a/src/models/index.js b/src/models/index.js index 02c1617..5e08398 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -7,6 +7,8 @@ import DriveSegments from './drive_segments.model'; import Drives from './drives.model'; import OAuthAccounts from './oauth_accounts.model'; +import orm from './orm'; + export { Accounts, AthenaActionLog, @@ -17,3 +19,5 @@ export { Drives, OAuthAccounts, }; + +export default orm; diff --git a/src/models/orm.js b/src/models/orm.js index 46d11c4..acd0fc3 100644 --- a/src/models/orm.js +++ b/src/models/orm.js @@ -11,14 +11,4 @@ const sequelize = new Sequelize({ sequelize.options.logging = () => {}; -/** - * Synchronise the database (create new tables) to match the models defined - * above. - * - * WARNING: If force is set, sequelize will delete columns and create new ones - * if their types have changed! - * Use sequelize-cli and migrations instead! - */ -sequelize.sync({ force: process.env.DB_FORCE_SYNC === 'true' }); - export default sequelize; diff --git a/src/server/app.js b/src/server/app.js index d0d82c6..030f6e3 100644 --- a/src/server/app.js +++ b/src/server/app.js @@ -3,12 +3,8 @@ import cors from 'cors'; import express from 'express'; import log4js from 'log4js'; -import storageController from './controllers/storage'; import controllers from './controllers'; import router from './router'; -import { Accounts, Devices, Drives } from '../models'; - -const logger = log4js.getLogger(); function runAsyncWrapper(callback) { return function wrapper(req, res, next) { @@ -17,49 +13,40 @@ function runAsyncWrapper(callback) { }; } -const tasks = []; -const app = express(); +export default async () => { + const logger = log4js.getLogger(); + const app = express(); -storageController.initializeStorage(); -tasks.push(storageController.updateTotalStorageUsed()); + app.use(cors({ + origin: ['http://localhost:3000', 'https://connect.retropilot.org'], + credentials: true, + })); + app.use(cookieParser()); -// debug: print out some info from the database -Promise.all([Accounts.findAll(), Devices.findAll(), Drives.findAll()]) - .then(([accounts, devices, drives]) => { - logger.info(`Found ${accounts.length} accounts`); - logger.info(`Found ${devices.length} devices`); - logger.info(`Found ${drives.length} drives`); + app.use(router); + + app.use('/favicon.ico', express.static('static/favicon.ico')); + app.use(process.env.BASE_DRIVE_DOWNLOAD_PATH_MAPPING, express.static(process.env.STORAGE_PATH)); + + app.use('/.well-known', express.static('.well-known')); + + app.use('/cabana', express.static('cabana/')); + + app.get('/', async (req, res) => { + res.redirect('/useradmin'); }); -app.use(cors({ - origin: ['http://localhost:3000', 'https://connect.retropilot.org'], - credentials: true, -})); -app.use(cookieParser()); + app.get('*', runAsyncWrapper(async (req, res) => { + logger.error(`HTTP.GET unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`); + res.status(404); + res.send('Not Implemented'); + })); -app.use(router); + app.post('*', runAsyncWrapper(async (req, res) => { + logger.error(`HTTP.POST unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`); + res.status(404); + res.send('Not Implemented'); + })); -app.use('/favicon.ico', express.static('static/favicon.ico')); -app.use(process.env.BASE_DRIVE_DOWNLOAD_PATH_MAPPING, express.static(process.env.STORAGE_PATH)); - -app.use('/.well-known', express.static('.well-known')); - -app.use('/cabana', express.static('cabana/')); - -app.get('/', async (req, res) => { - res.redirect('/useradmin'); -}); - -app.get('*', runAsyncWrapper(async (req, res) => { - logger.error(`HTTP.GET unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`); - res.status(404); - res.send('Not Implemented'); -})); - -app.post('*', runAsyncWrapper(async (req, res) => { - logger.error(`HTTP.POST unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`); - res.status(404); - res.send('Not Implemented'); -})); - -export default Promise.all(tasks).then(() => app); + return app; +}; diff --git a/src/server/server.js b/src/server/server.js index 318dbf1..e7bc1d3 100644 --- a/src/server/server.js +++ b/src/server/server.js @@ -3,13 +3,36 @@ import http from 'http'; import log4js from 'log4js'; import app from './app'; +import storageController from './controllers/storage'; +import orm, { Accounts, Devices, Drives } from '../models'; export default async () => { - const server = await app; - const logger = log4js.getLogger(); - const httpServer = http.createServer(server); + storageController.initializeStorage(); + await storageController.updateTotalStorageUsed(); + + /** + * Synchronise the database (create new tables) to match the models defined + * above. + * + * WARNING: If force is set, sequelize will delete columns and create new ones + * if their types have changed! + * Use sequelize-cli and migrations instead! + */ + const options = { force: process.env.DB_FORCE_SYNC === 'true' }; + await orm.sync(options); + logger.info('Database synced', options); + + // debug: print out some info from the database + Promise.all([Accounts.findAll(), Devices.findAll(), Drives.findAll()]) + .then(([accounts, devices, drives]) => { + logger.info(`Found ${accounts.length} accounts`); + logger.info(`Found ${devices.length} devices`); + logger.info(`Found ${drives.length} drives`); + }); + + const httpServer = http.createServer(await app()); httpServer.listen(process.env.HTTP_PORT, () => { logger.info(`RetroPilot Server listening at ${process.env.BASE_URL}`); });