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

Initial commits #1

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ Below is a guide for setting up the Heroku application from scratch should there

== Environment variables

`PROXY`::
(Required) Set to `true` if se-edu-bot is served behind a reverse proxy (e.g. ngrok or heroku).
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not required, but optional. Defaults to false.

Given that we host se-edu-bot on heroku and use ngrok for development,
this should usually be set to `true`.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, given that for both development and production this should be set to true, we shouldn't even need this environment variable in the first place?


`PORT`::
TCP port which the server will listen on.
There is no need to explicitly set this on Heroku,
as Heroku will automatically set the `PORT` environment variable.
(Default: 5000)

== Coding standard

We follow the oss-generic coding standard.
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
},
"dependencies": {
"dotenv": "^4.0.0",
"http-errors": "^1.6.1",
"koa": "^2.2.0",
"koa-compose": "^4.0.0",
"koa-route": "^3.2.0",
"source-map-support": "^0.4.15"
},
"devDependencies": {
"@types/dotenv": "^4.0.0",
"@types/http-errors": "^1.5.34",
"@types/koa": "^2.0.39",
"@types/koa-compose": "^3.2.2",
"@types/koa-route": "^3.2.0",
"@types/node": "^7.0.31",
"@types/source-map-support": "^0.4.0",
"nodemon": "^1.11.0",
Expand Down
54 changes: 53 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,58 @@ sourceMapSupport.install();
import dotenv = require('dotenv');
dotenv.config();

import Koa = require('koa');
import koaRoute = require('koa-route');

/**
* Server app configuration.
*/
export interface AppConfig {
/**
* True if the server is being served behind a proxy.
*/
proxy: boolean;
}

/**
* Creates a Koa Application with the provided `appConfig`.
*/
export function createApp(appConfig: AppConfig): Koa {
const app = new Koa();
app.proxy = appConfig.proxy;

app.use(koaRoute.get('/', async ctx => {
ctx.body = 'Hello world!';
}));

return app;
}

/**
* Extracts {@link AppConfig} from `process.env`.
*/
function extractAppConfigFromEnv(): AppConfig {
return {
proxy: !!extractEnvVar('PROXY', ''),
};
}

/**
* Extracts an environment variable from `process.env`.
* @throws {Error} Environment variable not defined and `defaultValue` not provided.
*/
function extractEnvVar(key: string, defaultValue?: string): string {
if (typeof process.env[key] === 'string' && process.env[key]) {
return process.env[key];
} else if (typeof defaultValue !== 'undefined') {
return defaultValue;
} else {
throw new Error(`$${key} not defined`);
}
}

if (require.main === module) {
console.log('Hello world!');
const port = parseInt(extractEnvVar('PORT', '5000'), 10);
const app = createApp(extractAppConfigFromEnv());
app.listen(port);
}