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

Feature/refactor server #289

Merged
merged 3 commits into from
Apr 23, 2021
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
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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability


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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability


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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be lowecase like the rest of consts here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll update. casing ultimately has no difference in javascript but its good to keep to convention.

Copy link
Contributor Author

@just-at-uber just-at-uber Apr 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code more closely, the code actually calls new on Koa. this would indicate that this is a class (or constructor). Typically in javascript we notate classes vs functions by using first capital letter. So i think in this particular case the casing is correct.

You can look at this convention here:
https://stackoverflow.com/questions/1564398/javascript-method-naming-lowercase-vs-uppercase/1564489#:~:text=5%20Answers&text=A%20popular%20convention%20in%20Javascript,mistakenly%20called%20%22classes%22).&text=Anything%20that's%20not%20a%20constructor%20usually%20starts%20with%20lowercase%20and%20is%20camelCased.

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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of file to match import compatibility in the future


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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

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')({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

koaCompress({
filter: contentType => !contentType.startsWith('text/event-stream'),
})
)
.use(require('./middleware/tchannel-client'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

.use(tchannelClient)
.use(
useWebpack
? koaWebpack({
compiler,
dev: { stats: { colors: true } },
hot: { port: process.env.TEST_RUN ? 8082 : 8081 },
})
: require('koa-static')(staticRoot)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

: 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