Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.use(
// keeps serverRender updated with arg: { clientStats, outputPath }
webpackHotServerMiddleware(multiCompiler, {
serverRendererOptions: {outputPath},
}),
);
}
else {
const clientStats = require('../../build/client/stats.json'); // eslint-disable-line import/no-unresolved
const serverRender = require('../../build/server/main.js').default; // eslint-disable-line import/no-unresolved
app.use(publicPath, express.static(outputPath));
app.use(serverRender({clientStats, outputPath}));
}
app.listen(config.apps.frontend.api_port, () => {
console.log(`Listening @ http://localhost:${config.apps.frontend.api_port}/`);
});
// allow to deactivate service worker caching on search parameter, make search bar initializes correctly with right REDUX_STATE from server
navigateFallbackWhitelist: [/\?search=/],
staticFileGlobsIgnorePatterns: [/\.map$/, /manifest\.json$/, /index\.html$/, /404\.html$/],
// https://github.com/GoogleChromeLabs/sw-precache#handlefetch-boolean
// handleFetch: true, // pass to false for debugging
}),
] : [
// dll,
new BrowserSyncPlugin(
// BrowserSync options
{
// browse to http://localhost:3001/ during development
open: false,
port: config.apps.frontend.api_port + 1,
proxy: {
target: `localhost:${config.apps.frontend.api_port}`,
},
ghostMode: false,
},
// plugin options
{
// prevent BrowserSync require(reloading the page
// and let Webpack Dev Server take care of this
reload: false,
callback: () => console.log('Finished proxifying...'),
},
),
]),
] : [
// The LimitChunkCountPlugin with maxChunks: 1 insures only one file is generated for your server bundle so it can be run synchronously.
// https://github.com/faceyspacey/webpack-flush-chunks
new webpack.optimize.LimitChunkCountPlugin({
serverRendererOptions: {outputPath},
createHandler: webpackHotServerMiddleware.createKoaHandler,
}));
// uncomment this code block for debugging service worker behaviour in development
// / https://github.com/goldhand/sw-precache-webpack-plugin#webpack-dev-server-support
// do not forget to regenerate service-worker.js when modifying SWPrecacheWebpackPlugin config
// with this command:
// NODE_ENV=development ./node_modules/.bin/babel-node ./node_modules/webpack/bin/webpack --progress --config webpack/ssr/server.js
// app.get('/service-worker.js', function (req, res) {
// res.set({ 'Content-Type': 'application/javascript; charset=utf-8' });
// res.send(fs.readFileSync('./assets/service-worker.js'));
// });
http.createServer(app.callback()).listen(config.apps.frontend.api_port, () => console.log(`Listening @ http://localhost:${config.apps.frontend.api_port}/`),
);
}
else {
const clientStats = require('../../build/ssr/client/stats.json'); // eslint-disable-line import/no-unresolved
const serverRender = require('../../build/ssr/server/main.js').default; // eslint-disable-line import/no-unresolved
// look if require basic auth
if (typeof config.auth.name !== 'undefined' && typeof config.auth.pass !== 'undefined' && config.auth.name !== '' && config.auth.pass !== '') {
app.use(auth(config.auth));
}
app.use(mount(publicPath, serve(outputPath)));
app.use(serverRender({clientStats, outputPath}));
// uncomment this code for local encryption (debug)
// self signed
import config from 'config';
import path from 'path';
import koa from 'koa';
import koaCors from 'koa-cors';
import koaMount from 'koa-mount';
import koaHelmet from 'koa-helmet';
import compress from 'koa-compressor';
import dbClient from './lib/db/client';
import logger from './lib/logger';
import xdomainRoute from './lib/xdomainRoute';
const env = process.env.NODE_ENV || 'development';
const port = config.apps.api.port;
const app = koa();
const appLogger = logger(config.apps.api.logs.app);
const httpLogger = logger(config.apps.api.logs.http);
// Server logs
app.use(function* logHttp(next) {
this.httpLog = {
method: this.request.method,
remoteIP: this.request.ip,
userAgent: this.request.headers['user-agent'],
app: this.request.url.indexOf('/admin') === 0 ? 'admin' : 'api',
};
const sessionId = this.cookies.get('koa:sess');
if (sessionId) {
co(function* renameStartImageInS3() {
yield updateCurrentFileFactory(config.apps.api.s3)(args[0]);
process.exit();
}).catch(err => {
console.error(err.message);
co(function* createAdmin() {
const connection = yield dbClient(config.apps.api.db);
const userRepository = userRepositoryFactory(connection.client);
const user = yield userRepository.insertOne({
email: args[1],
password: args[2],
});
console.log('\n');
console.log(user);
process.exit();
}).catch(err => {
console.error(err.message);
export default async (ctx, next) => {
const token = ctx.get('Authorization');
const cookieToken = ctx.cookies.get('token');
const expectedCookieToken = crypto.createHmac('sha256', config.apps.api.security.secret)
.update(token)
.digest('hex');
if (cookieToken !== expectedCookieToken) {
ctx.status = 401;
return;
}
try {
const tokenData = await jwt.verify(token, config.apps.api.security.jwt.privateKey);
ctx.user = omit(tokenData, 'expires');
} catch (e) {
ctx.status = 401;
return;
}
let api;
try {
internetAccess = yield internetAccessCheck(config.apps.api.healthcare, fetch);
} catch (err) {
internetAccess = false;
}
try {
db = yield dbCheck(config.apps.api.db, PgPool);
} catch (err) {
db = false;
}
try {
api = yield apiCheck(config.apps.api.healthcare, fetch);
} catch (err) {
api = false;
}
this.status = internetAccess && db && api ? 200 : 500;
this.body = {
internetAccess: internetAccess ? 'OK' : 'KO',
db: db ? 'OK' : 'KO',
api: api ? 'OK' : 'KO',
};
}));
app.use(koaRoute.post('/sign-up', async (ctx) => {
const { email, password } = ctx.request.body;
const user = await userRepository.insertOne({ email, password });
if (!user) {
ctx.status = 401;
return;
}
authenticate(ctx, user, config.apps.api.security);
}));
app.use(koaRoute.post('/', async (ctx) => {
const { email, password } = ctx.request.body;
const userRepository = userRepositoryFactory(ctx.client);
const user = await userRepository.authenticate(email, password);
if (!user) {
this.body = 'Invalid credentials.';
this.status = 401;
return;
}
authenticate(ctx, user, config.apps.api.security);
}));