Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLEANUP core] Move schedule loading out of web controller #69

Merged
merged 5 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 23 additions & 153 deletions src/app/PagerBeautyApp.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
// ------- Imports -------------------------------------------------------------

import http from 'http';
import path from 'path';

import auth from 'koa-basic-auth';
import Koa from 'koa';
import logger from 'winston';
import mount from 'koa-mount';
import nunjucks from 'nunjucks';
import route from 'koa-route';
import serve from 'koa-static';
import views from 'koa-views';

// ------- Internal imports ----------------------------------------------------

import { SchedulesController } from '../controllers/SchedulesController';
import { redirect } from '../middleware/redirect';
import { PagerBeautyHttpServerStartError } from '../errors';
import { PagerBeautyWeb } from './PagerBeautyWeb';
import { PagerBeautyWorker } from './PagerBeautyWorker';

// ------- Class ---------------------------------------------------------------

Expand All @@ -26,17 +15,17 @@ export class PagerBeautyApp {
this.start = this.start.bind(this);
this.stop = this.stop.bind(this);

// Config
// Config.
this.config = config;

// Nothing running yet.
this.httpServer = false;
// Database.
this.db = new Map();

// Init controllers mapping.
this.controllers = PagerBeautyApp.initControllersRegistry();
// Web functionality.
this.web = new PagerBeautyWeb(this);

// Configure web app.
this.webApp = this.initWebApp();
// Loading and managing data.
this.worker = new PagerBeautyWorker(this);
}

// ------- Public API -------------------------------------------------------
Expand All @@ -45,147 +34,28 @@ export class PagerBeautyApp {
const { config } = this;
logger.info(`Starting PagerBeauty v${config.version} in ${config.env} mode`);

// Controllers
await this.startControllers();

// HTTP Server
let server;
try {
server = await PagerBeautyApp.startHttpServerAsync(this.webApp.callback());
} catch (error) {
if (error instanceof PagerBeautyHttpServerStartError) {
this.stop(error.server);
}
return false;
}
this.httpServer = server;
// Concurrent start.
const webStart = this.web.start();
const workerStart = this.worker.start();

// Waits for slowest to finish.
await webStart;
await workerStart;
return true;
}

async stop(httpServer) {
async stop() {
logger.info('Graceful shut down');
await this.stopControllers();
// Concurrent stop.
const webStop = this.web.stop();
const workerStop = this.worker.stop();

await PagerBeautyApp.stopHttpServerAsync(httpServer || this.httpServer);
// Waits for slowest to finish.
await webStop;
await workerStop;
return true;
}

// ------- Internal machinery -----------------------------------------------


initWebApp() {
const app = new Koa();

// @todo: Set app env?
// @todo: Web proxy?

// -------- Setup web middleware --------

// @todo: Enforce https?
// @todo: Generate unique request id?
if (this.config.auth && this.config.auth.name && this.config.auth.pass) {
app.use(auth(this.config.auth));
}

// Static assets
app.use(mount('/assets', serve('assets')));

// Templates
const viewsPath = path.resolve('src', 'views');
const nunjucksEnv = new nunjucks.Environment(
new nunjucks.FileSystemLoader(viewsPath),
);
// Global template variables.
const assetsPath = this.config.env === 'production'
? '/assets/dist-prod'
: '/assets/dist';
nunjucksEnv.addGlobal('assetsPath', assetsPath);
// Apply nunjucks to all *.j2
app.use(views(viewsPath, {
options: {
nunjucksEnv,
},
map: { j2: 'nunjucks' },
extension: 'j2',
}));

// Custom Routes

const schedulesController = this.controllers.get('SchedulesController');
// Redirects
app.use(route.get('/', redirect('/v1')));
app.use(route.get('/v1', redirect('/v1/schedules.html')));
app.use(route.get('/v1/schedules', redirect('/v1/schedules.html')));
// Controllers
app.use(route.get(
'/v1/schedules.(json|html)',
schedulesController.index,
));
app.use(route.get(
'/v1/schedules/:scheduleId.(json|html)',
schedulesController.show,
));
return app;
}

async startControllers() {
const controllerEntries = Array.from(this.controllers.entries());
return Promise.all(controllerEntries.map(([name, controller]) => {
logger.debug(`Starting web controller ${name}`);
return controller.start(this);
}));
}

async stopControllers() {
const controllerEntries = Array.from(this.controllers.entries());
return Promise.all(controllerEntries.map(([name, controller]) => {
logger.debug(`Stopping web controller ${name}`);
return controller.stop(this);
}));
}

static initControllersRegistry() {
const controllers = new Map();
controllers.set('SchedulesController', new SchedulesController());
return controllers;
}

static startHttpServerAsync(connectionListener) {
// Wrap HTTP server callbacks into a promise
return new Promise((resolve, reject) => {
// Start HTTP server
const server = http.createServer(connectionListener);
server.on('listening', () => {
const address = server.address();
const readableUrl = `http://${address.address}:${address.port}`;
logger.info(`HTTP server is listening on ${readableUrl}`);
resolve(server);
});
server.on('error', (error) => {
logger.error(error.toString());
reject(new PagerBeautyHttpServerStartError(error.message, server));
});
server.listen({
host: '0.0.0.0',
// @todo: make configurable.
port: 8080,
});
});
}

static stopHttpServerAsync(server) {
// Wrap HTTP server callbacks into a promise
return new Promise((resolve) => {
server.close((error) => {
if (error) {
// Already stopped.
logger.verbose(`HTTP server: graceful shut down error: ${error.message}`);
}
resolve();
});
});
}

// ------- Class end --------------------------------------------------------
}

Expand Down
163 changes: 163 additions & 0 deletions src/app/PagerBeautyWeb.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// ------- Imports -------------------------------------------------------------

import http from 'http';
import path from 'path';

import auth from 'koa-basic-auth';
import Koa from 'koa';
import logger from 'winston';
import mount from 'koa-mount';
import nunjucks from 'nunjucks';
import route from 'koa-route';
import serve from 'koa-static';
import views from 'koa-views';

// ------- Internal imports ----------------------------------------------------

import { SchedulesController } from '../controllers/SchedulesController';
import { redirect } from '../middleware/redirect';
import { PagerBeautyHttpServerStartError } from '../errors';

// ------- Class ---------------------------------------------------------------

export class PagerBeautyWeb {
constructor(app) {
// Attach Public API functions to object context.
this.start = this.start.bind(this);
this.stop = this.stop.bind(this);

// App
this.app = app;

// Config
this.config = app.config;

// Nothing running yet.
this.httpServer = false;

// Init controllers mapping.
this.controllers = new Map();
this.controllers.set('SchedulesController', new SchedulesController(app));

// Configure web app.
this.webApp = this.initWebApp();
}

// ------- Public API -------------------------------------------------------

async start() {
// HTTP Server
let server;
try {
server = await PagerBeautyWeb.startHttpServerAsync(this.webApp.callback());
} catch (error) {
if (error instanceof PagerBeautyHttpServerStartError) {
this.stop(error.server);
}
return false;
}
this.httpServer = server;
return true;
}

async stop(httpServer) {
await PagerBeautyWeb.stopHttpServerAsync(httpServer || this.httpServer);
return true;
}

// ------- Internal machinery -----------------------------------------------

initWebApp() {
const app = new Koa();

// @todo: Set app env?
// @todo: Web proxy?

// -------- Setup web middleware --------

// @todo: Enforce https?
// @todo: Generate unique request id?
if (this.config.auth && this.config.auth.name && this.config.auth.pass) {
app.use(auth(this.config.auth));
}

// Static assets
app.use(mount('/assets', serve('assets')));

// Templates
const viewsPath = path.resolve('src', 'views');
const nunjucksEnv = new nunjucks.Environment(
new nunjucks.FileSystemLoader(viewsPath),
);
// Global template variables.
const assetsPath = this.config.env === 'production'
? '/assets/dist-prod'
: '/assets/dist';
nunjucksEnv.addGlobal('assetsPath', assetsPath);
// Apply nunjucks to all *.j2
app.use(views(viewsPath, {
options: {
nunjucksEnv,
},
map: { j2: 'nunjucks' },
extension: 'j2',
}));

// Redirects
app.use(route.get('/', redirect('/v1')));
app.use(route.get('/v1', redirect('/v1/schedules.html')));
app.use(route.get('/v1/schedules', redirect('/v1/schedules.html')));

// Controller routes
const schedulesController = this.controllers.get('SchedulesController');
app.use(route.get(
'/v1/schedules.(json|html)',
schedulesController.index,
));
app.use(route.get(
'/v1/schedules/:scheduleId.(json|html)',
schedulesController.show,
));
return app;
}

static startHttpServerAsync(connectionListener) {
// Wrap HTTP server callbacks into a promise
return new Promise((resolve, reject) => {
// Start HTTP server
const server = http.createServer(connectionListener);
server.on('listening', () => {
const address = server.address();
const readableUrl = `http://${address.address}:${address.port}`;
logger.info(`HTTP server is listening on ${readableUrl}`);
resolve(server);
});
server.on('error', (error) => {
logger.error(error.toString());
reject(new PagerBeautyHttpServerStartError(error.message, server));
});
server.listen({
host: '0.0.0.0',
// @todo: make configurable.
port: 8080,
});
});
}

static stopHttpServerAsync(server) {
// Wrap HTTP server callbacks into a promise
return new Promise((resolve) => {
server.close((error) => {
if (error) {
// Already stopped.
logger.verbose(`HTTP server: graceful shut down error: ${error.message}`);
}
resolve();
});
});
}

// ------- Class end --------------------------------------------------------
}

// ------- End -----------------------------------------------------------------
Loading