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 4 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
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
charset = utf-8
max_line_length = 120
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules/
/npm-debug.log
/se-edu-bot-*.tgz
/dist/
/.env
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js

node_js:
- "7"

script: npm run all build lint
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2017 seedu-bot contributors

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.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: npm start
149 changes: 149 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
= se-edu-bot
:toc: preamble
:toc-title:
:sectnums:
ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
endif::[]
ifdef::env-github,env-browser[:outfilesuffix: .adoc]

Helps out with managing SE-EDU projects.

== Local development setup

=== Prerequisites

. Node `7.9.0` or later.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Need to specify required npm version as well.

. An editor with Typescript language service support is strongly recommended.
(e.g. https://code.visualstudio.com/[VS Code])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe add a Recommended editors section.

Note that for VSCode, the editorconfig plugin must be installed for it to respect .editorconfig files.

. https://ngrok.com/[ngrok]

=== Cloning the repo

. Fork this repo, and clone the fork to your computer.
. `cd` into the project directory.
. Run `npm install` to install dependencies.

=== Setting up ngrok

We use https://ngrok.com/[ngrok] to expose our local server to the internet for Webhook testing.
Run

----
ngrok http 5000
----

You should see a line like this:

----
Forwarding https://abcdefg123.ngrok.io -> localhost:5000
----

This is your temporary ngrok URL which exposes your local server to the internet.
It is valid until the `ngrok` process terminates.
Take note of it, we'll use it in later steps.

=== Configuring environment variables

Environment variables are used to configure the bot.
See <<Environment variables>> for a full list of settings.

For ease of development,
se-edu-bot will automatically load environment variables defined in the `.env` file in the project root.

This is done using https://github.com/motdotla/dotenv[dotenv].
See the aforementioned link for information on the `.env` file format.

=== Building the project

* Build the project once.
+
[source,shell]
----
npm run build
----

* Do a clean build.
+
[source,shell]
----
npm run all clean build
----

=== Running the server

The project must be built first (`npm run build`).

* Run the server
+
[source,shell]
----
npm start
----

* Run the server and watch for changes,
incrementally rebuilding the project and restarting the server whenever source files change.
+
[source,shell]
----
npm run watch
----

The server should be accessible via your ngrok address which you got in <<Setting up ngrok>>.

=== Linting

[source,shell]
----
npm run lint
----

=== Cleaning build products

[source,shell]
----
npm run clean
----

== Deploying to Heroku

This repository is setup to automatically deploy to Heroku whenever new commits are pushed to `master`.
As such, there is no need for any manual deployment.

Below is a guide for setting up the Heroku application from scratch should there be any need to (e.g. for testing).

=== Setting up the Heroku application from scratch

. Go to the https://dashboard.heroku.com[Heroku dashboard] and login.
. `New` -> `Create new App`.
. Enter the app name (i.e. `se-edu-bot`) and click `Create App`.
. Under `Deployment method`, select `Github`.
. If Heroku Dashboard does not have access to your GitHub account,
it will display a single `Connect to GitHub` button.
Click on it, and then authorize the Heroku Dashboard to access your GitHub account and `se-edu` organization.
. Follow the instructions to connect the Heroku app to the `se-edu/se-edu-bot` repo.
. Ensure the `Wait for CI to pass before deploy` checkbox is checked, and then click `Enable Automatic Deploys`.
. Go to `Settings` -> `Config Variables`, and set `NPM_CONFIG_PRODUCTION` to `false`.
. Configure the rest of the <<Environment variables>>.

== 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.

== License

MIT License
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "se-edu-bot",
"description": "Helps out with SE-EDU tasks",
"license": "MIT",
"private": true,
"engines": {
"node": ">=7.9.0"
},
"files": [
"dist"
],
"scripts": {
"all": "npm-run-all",
"build": "tsc",
"postinstall": "npm run build",
"start": "node dist/server.js",
"watch:tsc": "tsc -w",
"watch:server": "nodemon --watch dist dist/server.js",
"watch": "npm-run-all --parallel \"watch:*\"",
"clean": "rimraf dist",
"lint": "tslint -p tsconfig.json --type-check"
},
"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",
"npm-run-all": "^4.0.2",
"rimraf": "^2.6.1",
"tslint": "^5.4.3",
"tslint-language-service": "^0.9.6",
"typescript": "^2.3.4"
}
}
61 changes: 61 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import sourceMapSupport = require('source-map-support');
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) {
const port = parseInt(extractEnvVar('PORT', '5000'), 10);
const app = createApp(extractAppConfigFromEnv());
app.listen(port);
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"newLine": "LF",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "dist",
"plugins": [
{"name": "tslint-language-service"}
],
"pretty": true,
"sourceMap": true,
"strict": true,
"target": "ES2017"
}
}
Loading