Skip to content

Commit

Permalink
Feature/refactor server (#289)
Browse files Browse the repository at this point in the history
* refactoring server

* more refactoring

* fix lint
  • Loading branch information
just-at-uber authored Apr 23, 2021
1 parent e49ff81 commit ebb847d
Show file tree
Hide file tree
Showing 20 changed files with 808 additions and 329 deletions.
26 changes: 26 additions & 0 deletions server/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const momentToLong = require('./moment-to-long');

module.exports = {
momentToLong,
};
26 changes: 26 additions & 0 deletions server/helpers/moment-to-long.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const Long = require('long');

const momentToLong = m => Long.fromValue(m.unix()).mul(1000000000);

module.exports = momentToLong;
40 changes: 22 additions & 18 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const path = require('path'),
Koa = require('koa'),
bodyparser = require('koa-bodyparser'),
send = require('koa-send'),
staticRoot = path.join(__dirname, '../dist'),
app = new Koa(),
router = require('./routes');
const path = require('path');
const Koa = require('koa');
const koaBodyparser = require('koa-bodyparser');
const koaCompress = require('koa-compress');
const koaSend = require('koa-send');
const koaStatic = require('koa-static');
const koaWebpack = require('koa-webpack');
const webpack = require('webpack');

app.webpackConfig = require('../webpack.config');
const webpackConfig = require('../webpack.config');
const tchannelClient = require('./middleware/tchannel-client');
const router = require('./router');

const staticRoot = path.join(__dirname, '../dist');
const app = new Koa();

app.webpackConfig = webpackConfig;

app.init = function(options) {
options = options || {};
Expand All @@ -37,14 +45,10 @@ app.init = function(options) {
? options.useWebpack
: process.env.NODE_ENV !== 'production';

let koaWebpack;
let compiler;

if (useWebpack) {
const Webpack = require('webpack');

koaWebpack = require('koa-webpack');
compiler = Webpack(app.webpackConfig);
compiler = webpack(app.webpackConfig);
}

app
Expand All @@ -63,21 +67,21 @@ app.init = function(options) {
ctx.body = { message: err.message };
}
})
.use(bodyparser())
.use(koaBodyparser())
.use(
require('koa-compress')({
koaCompress({
filter: contentType => !contentType.startsWith('text/event-stream'),
})
)
.use(require('./middleware/tchannel-client'))
.use(tchannelClient)
.use(
useWebpack
? koaWebpack({
compiler,
dev: { stats: { colors: true } },
hot: { port: process.env.TEST_RUN ? 8082 : 8081 },
})
: require('koa-static')(staticRoot)
: koaStatic(staticRoot)
)
.use(router.routes())
.use(router.allowedMethods())
Expand All @@ -97,7 +101,7 @@ app.init = function(options) {
ctx.set('content-type', 'text/html');
ctx.body = compiler.outputFileSystem.readFileSync(filename);
} else {
await send(ctx, 'index.html', { root: staticRoot });
await koaSend(ctx, 'index.html', { root: staticRoot });
}
} catch (err) {
if (err.status !== 404) {
Expand Down
Loading

0 comments on commit ebb847d

Please sign in to comment.