* docker: initial config

* docker: add entrypoint for headless run

* docker: resolve path from env variable

* docker: initial config

* docker: rename preloaded data directories

* docker: update README

* docker: prevent issue with port mappings

* docker: update README.md

* docker: add docker-compose

* docker: cleanup logs

* docker: add CI

* docker: cleanup unused

* docker: use static port in development

* refactor: isolate tasks

* docker: add demo db

* fix: download db resolves according to OS

* docker: config build

* fix: ensure db path exists

* fix: add object type to block

* docker: config build

* docker: config build

* docker: env is production

* docker: preload db

* fix: remove test dir

* docker: graceful shutdown node app

* docker: env is production

* docker: add docker-compose

* docker: remove unused mappings

* docker: version bump
This commit is contained in:
Carlos Valente
2022-06-08 21:57:22 +02:00
committed by GitHub
parent 94a7ef6318
commit 9c5c7ee69a
23 changed files with 214 additions and 187 deletions
+9 -11
View File
@@ -26,7 +26,7 @@ import { initiateOSC, shutdownOSCServer } from './controllers/OscController.js';
import { fileURLToPath } from 'url';
// get environment
const env = process.env.NODE_ENV || 'prod';
const env = process.env.NODE_ENV || 'production';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -56,11 +56,11 @@ app.use('/ontime', ontimeRouter);
app.use('/playback', playbackRouter);
// serve react
app.use(express.static(join(__dirname, env === 'prod' ? '../' : '../../', 'client/build')));
app.use(express.static(join(__dirname, env === 'production' ? '../' : '../../', 'client/build')));
app.get('*', (req, res) => {
res.sendFile(
resolve(__dirname, env === 'prod' ? '../' : '../../', 'client', 'build', 'index.html')
resolve(__dirname, env === 'production' ? '../' : '../../', 'client', 'build', 'index.html'),
);
});
@@ -107,14 +107,7 @@ export const startOSCServer = async (overrideConfig = null) => {
const server = http.createServer(app);
export const startServer = async (overrideConfig = null) => {
// Setup default port
// const port = overrideConfig?.port || serverPort;
/* Note: Port is hardcoded
* need to find a good solution for sharing
* the dynamic info with the frontend
*/
const port = 4001;
const port = 4001; // port hardcoded
// Start server
const returnMessage = `Ontime is listening on port ${port}`;
@@ -145,4 +138,9 @@ export const shutdown = async () => {
global.timer.shutdown();
};
// register shutdown signals
process.once('SIGHUP', shutdown)
process.once('SIGINT', shutdown)
process.once('SIGTERM', shutdown)
export { server, app };
+1 -1
View File
@@ -1403,7 +1403,7 @@ export class EventTimer extends Timer {
this.messageStack.unshift(m);
this.io.emit('logger', m);
if (process.env.NODE_ENV !== 'prod') {
if (process.env.NODE_ENV !== 'production') {
console.log(`[${m.level}] \t ${m.origin} \t ${m.text}`);
}
+1
View File
@@ -6,6 +6,7 @@ export const config = {
port: 4001,
},
database: {
directory: 'preloaded-db',
filename: 'db.json',
tablename: 'events',
},
+3 -9
View File
@@ -1,15 +1,9 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// get database
import { data, db } from '../app.js';
import { networkInterfaces } from 'os';
import { fileHandler } from '../utils/parser.js';
import { generateId } from '../utils/generate_id.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { resolveDbPath } from '../modules/loadDb.js';
// Create controller for GET request to '/ontime/poll'
// Returns data for current state
@@ -28,9 +22,9 @@ export const poll = async (req, res) => {
// Returns -
export const dbDownload = async (req, res) => {
const fileTitle = data?.event?.title || 'ontime events';
const dbFile = path.resolve(__dirname, '../', 'data/db.json');
const dbInDisk = resolveDbPath();
res.download(dbFile, `${fileTitle}.json`, (err) => {
res.download(dbInDisk, `${fileTitle}.json`, (err) => {
if (err) {
res.status(500).send({
message: 'Could not download the file. ' + err,
+19 -8
View File
@@ -8,16 +8,27 @@ import { dbModelv1 as dbModel } from '../models/dataModel.js';
import { parseJson_v1 as parseJson } from '../utils/parser.js';
/**
* @description ensures directories exist and picks available db path
* @description Decides which path the database is in
* @param ensure - whether it should create directory if it doesnt exist
* @return {string}
*/
export const resolveDbPath = (ensure = true) => {
const appPath = getAppDataPath();
const dbDirectory = join(appPath, config.database.directory);
if (ensure) {
ensureDirectory(dbDirectory);
}
return join(dbDirectory, config.database.filename);
};
/**
* @description ensures directories exist and populates database
* @param runningDirectory
* @return {string}
*/
const checkDirectories = (runningDirectory) => {
const appPath = getAppDataPath();
const dbDirectory = join(appPath, 'data');
const dbInDisk = join(dbDirectory, config.database.filename);
const startupDb = join(runningDirectory, 'data', config.database.filename);
ensureDirectory(dbDirectory);
const populateDb = (runningDirectory) => {
const startupDb = join(runningDirectory, config.database.directory, config.database.filename);
const dbInDisk = resolveDbPath();
// if dbInDisk doesnt exist we want to use startup db
if (!existsSync(dbInDisk)) {
@@ -53,7 +64,7 @@ const parseDb = async (fileToRead, adapterToUse) => {
* @return {Promise<{data: (number|*), db: Low<unknown>}>}
*/
export default async function loadDb(runningDirectory) {
const dbInDisk = checkDirectories(runningDirectory);
const dbInDisk = populateDb(runningDirectory);
const adapter = new JSONFile(dbInDisk);
const db = new Low(adapter);
+2 -1
View File
@@ -22,6 +22,7 @@
},
"scripts": {
"nodestart": "nodemon app.js",
"start": "node app.js"
"start": "node app.js",
"start:headless": "NODE_ENV=production node run.js"
}
}
+14
View File
@@ -0,0 +1,14 @@
(async () => {
let loaded;
try {
const { startServer, startOSCServer } = await import('./app.js');
// Start express server
loaded = await startServer();
// Start OSC Server (API)
await startOSCServer();
} catch (error) {
console.log(error);
}
console.log(loaded);
})();
+5 -9
View File
@@ -15,20 +15,16 @@ export function ensureDirectory(directory) {
}
}
/**
* @description Whether a file exists
* @param directory
* @return {boolean}
*/
export function doesFileExist(directory) {
return existsSync(directory);
}
/**
* @description Returns public path depending on OS
* @return {string|*}
*/
export function getAppDataPath() {
// handle docker
if (process.env.ONTIME_DATA) {
return path.join(process.env.ONTIME_DATA);
}
switch (process.platform) {
case 'darwin': {
return path.join(process.env.HOME, 'Library', 'Application Support', 'Ontime');