Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import Hapi from 'hapi'
import config from 'config'
import Log from './log'
const log = new Log('hubert-worker');
// Setup the server
const server = new Hapi.Server()
server.connection({
port: config.app.port,
})
// Load our plugins
server.register([
require('./plugin/healthcheck'),
require('./plugin/hubert'),
require('./plugin/processor'),
], (err) => {
if (err) {
log.error('Failed to load plugin: %s', err)
process.exit(1)
}
// Run!
server.start(() => {
log.info('Server running at %s', server.info.uri)
import config from 'config'
import { clone, map, fill, flatten } from 'lodash'
import cassandra from 'cassandra-driver'
import db from './../../cassandra'
import Log from './../../log'
import hue from './../../hue'
// Setup logger
const log = new Log('hubert-worker')
// Declare an interval that will check for the state of our lights every X ms
let intervalGetLightState = false
// We can ignore errors up to a certain limit
let errCount = config.app.errorThreshold
// Declare an error handler
const handleError = (err) => {
log.error('Something went terribly wrong: %s', err)
if (errCount > 0) {
log.error('Will ignore %s more times ...', errCount - 1)
errCount--
} else {
// Cleanup and shutdown
clearInterval(intervalGetLightState)
log.error('Shutting down the application ...')
process.exit(1)
}
}
// Declare a function to save states of our lights
async componentDidMount() {
const sessionJSON = await mapseedApiClient.session.get(config.app.api_root);
if (sessionJSON) {
Util.cookies.save("sa-api-sessionid", sessionJSON.sessionid);
}
// Fetch and load user information.
const authedUser = await mapseedApiClient.user.get(config.app.api_root);
const user = authedUser
? {
token: `user:${authedUser.id}`,
name: authedUser.username,
...authedUser,
// avatar_url and `name` are backup values that can get overidden:
avatar_url: authedUser.avatar_url || "/static/css/images/user-50.png",
isAuthenticated: true,
isLoaded: true,
},
};
/**
* Instantiate a new Winston Logger with the settings defined above
*/
const logger = createLogger({
format: combine(
format.timestamp({
format: 'YYYY-MM-DD hh:mm:ss',
}),
prettyPrint(),
),
transports: [
/* istanbul ignore next line */
...(config.app.logging.file ? [new transports.File(options.file)] : []),
new transports.Console(options.console),
],
exitOnError: false, // Do not exit on handled exceptions
});
/**
* Create a 'stdout/stderr' stream object with a 'write' function that will be used by `morgan`
*/
logger.stream = {
stdout: {
// eslint-disable-next-line no-unused-vars
write(message, encoding) {
// Use the 'info' log level so the output will be picked up
// By both transports (file and console)
logger.info(message);
},
app.set('view engine', 'handlebars');
app.engine('handlebars', hbs.__express);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.query());
app.use(express.cookieParser(CONF.app.cookie_secret));
app.use(express.session());
//app.use(express.responseTime());
// This is not needed if you handle static files with, say, Nginx (recommended in production!)
// Additionally you should probably pre-compile your LESS stylesheets in production
// Last, but not least: Express' default error handler is very useful in dev, but probably not in prod.
if (('NODE_SERVE_STATIC' in process.env) && process.env['NODE_SERVE_STATIC'] == 1) {
var pub_dir = CONF.app.pub_dir;
if (pub_dir[0] != '/') { pub_dir = '/' + pub_dir; } // humans are forgetful
var root_dir = require('path').dirname(module.parent.filename);
pub_dir = root_dir + pub_dir;
app.use(require('less-middleware')({ src: pub_dir }));
app.use(express.static(pub_dir));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}
callback(app);
//-- ATTENTION: make sure app.router and errorHandler are the very last two app.use() calls
//-- ATTENTION: and in the sequence they are in, or it won't work!!!
app.use(app.router);
// Catch-all error handler. Modify as you see fit, but don't overuse.
app.listen(config.server.port, function () {
console.log('%s listening at port %d', config.app.name, config.server.port);
});
get config() {
return config.app.locale.metadata;
}
const koa = require('koa');
const app = new koa();
const config = require('config');
const router = require('./router');
require('./db')();
app.keys = config.app.keys;
app.proxy = true;
app.use(require('koa-logger')());
app.use(require('koa-compress')());
app.use(require('koa-static-cache')('./public', {
maxAge: config.app.cacheAge
}));
app.use(require('koa-views')(__dirname + '/views', {
extension: 'pug'
}));
app.use(router.routes(), router.allowedMethods());
module.exports = app;
export default function eventFilters({ snapshotState = false } = {}) {
const eventOptions = Object.keys(config.app.locale.event_types).map(key => {
return {
label: `Type is "${config.app.locale.event_types[key]}"`,
value: key
};
});
return {
config: {
snapshotState
},
params: [
{
label: "Search...",
name: "keyword",
value: ""
},
async version(ctx, next) {
ctx.body = {
name: config.app.name,
version: config.app.version
};
await next();
}
};