initial commit

This commit is contained in:
Riku Rouvila
2015-01-14 18:14:37 +02:00
commit d9421f9996
13 changed files with 203 additions and 0 deletions

13
.editorconfig Normal file
View File

@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
config.json
node_modules

3
.jshintrc Normal file
View File

@@ -0,0 +1,3 @@
{
"node": true
}

1
README.md Normal file
View File

@@ -0,0 +1 @@
# koodiklinikka.fi API

18
deploy/deploy.sh Normal file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
set -e
export ANSIBLE_SSH_ARGS="-o ForwardAgent=yes"
# Install dependencies
if ! which pip > /dev/null; then
echo "Installing pip (requires sudo password)"
sudo apt-get install python-pip || sudo easy_install pip
fi
if ! which ansible > /dev/null; then
echo "Installing ansible (requires sudo password)"
sudo pip install ansible
fi
cd $(dirname $0)
ansible-playbook deploy.yml -i hosts

56
deploy/deploy.yml Normal file
View File

@@ -0,0 +1,56 @@
---
- name: Deploy API
hosts: all:!localhost
remote_user: "{{ service_user }}"
gather_facts: no
vars_files:
- vars.yml
tasks:
- name: Deploy from git
action: >
git
repo="{{ repository_url }}"
dest="{{ projects_path }}"
accept_hostkey=True
notify: restart service
- name: Setup bot config
copy: >
src=/opt/web/config/api.json
dest="{{ projects_path }}/config.json"
owner=web
group=web
mode=0644
notify: restart service
- name: Install NVM
action: >
git
repo="https://github.com/creationix/nvm"
dest="{{ nvm_path }}"
- name: Make sure Node.js is installed and properly aliased
command: >
bash -c "source {{ nvm_script }} && nvm install {{ nodejs_version }} && nvm alias {{ project_name }} {{ nodejs_version }}"
register: nvm_result
changed_when: >
"already installed" not in nvm_result.stdout
notify: restart service
- name: Install NPM dependencies and build assets
command: >
bash -c "source {{ nvm_script }} && nvm use {{ project_name }} && cd {{ projects_path }} && npm install"
notify: restart service
- name: Setup Upstart config
template: >
src=templates/upstart.j2
dest="/etc/init/{{ project_name }}.conf"
mode=664
notify: restart service
handlers:
- name: restart service
service: >
name={{ project_name }}
state=restarted

1
deploy/hosts Normal file
View File

@@ -0,0 +1 @@
koodiklinikka.fi

View File

@@ -0,0 +1,12 @@
description "koodiklinikka.fi API"
author "Riku Rouvila <riku.rouvila@gmail.com>"
start on runlevel [2345]
stop on runlevel [016]
respawn
respawn limit 10 5
env NODE_ENV=production
exec su -s /bin/bash -c 'source {{ nvm_script }} && nvm use {{ project_name }} && cd {{ projects_path }} && exec "$0" "$@"' {{ service_user }} -- \
node index.js \
>> /var/log/{{ project_name }}.log 2>&1

8
deploy/vars.yml Normal file
View File

@@ -0,0 +1,8 @@
---
project_name: koodiklinikka.fi-api
projects_path: /opt/web/koodiklinikka.fi-api
repository_url: git@github.com:koodiklinikka/koodiklinikka.fi-api.git
service_user: web
nvm_path: /opt/web/nvm
nvm_script: /opt/web/nvm/nvm.sh
nodejs_version: v0.10.25

16
index.js Normal file
View File

@@ -0,0 +1,16 @@
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
require('./routes/invite')(app);
app.use(function(err, req, res, next) {
console.error(err.message);
console.error(err.stack);
res.status(500).send('Internal server error');
});
app.listen(9000);

7
lib/config.js Normal file
View File

@@ -0,0 +1,7 @@
'use strict';
var _ = require('lodash');
var config = require('../config.json');
var env = process.env.NODE_ENV || 'development';
module.exports = _.merge({}, config.all, config[env]);

26
package.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "koodiklinikka.fi-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@github.com:koodiklinikka/koodiklinikka.fi-api.git"
},
"author": "Riku Rouvila",
"license": "MIT",
"bugs": {
"url": "https://github.com/koodiklinikka/koodiklinikka.fi-api/issues"
},
"homepage": "https://github.com/koodiklinikka/koodiklinikka.fi-api",
"dependencies": {
"body-parser": "^1.10.1",
"express": "^4.11.0",
"lodash": "^2.4.1",
"superagent": "^0.21.0",
"validator": "^3.27.0"
}
}

40
routes/invite.js Normal file
View File

@@ -0,0 +1,40 @@
'use strict';
var validator = require('validator');
var config = require('../lib/config');
var request = require('superagent');
module.exports = function (app) {
/*
* POST /invites
* Endpoint for sending invitations automatically
*/
app.post('/invites', function(req, res, next) {
if(!validator.isEmail(req.body.email)) {
res.status(400).send('Invalid email');
}
request
.post('https://koodiklinikka.slack.com/api/users.admin.invite')
.field('email', req.body.email)
.field('channels', config.slack.channels)
.field('token', config.slack.token)
.field('set_active', 'true')
.end(function(error, response){
if(error) {
return next(error);
}
if(!response.body.ok) {
return next(new Error('Creating slack invitation failed'));
}
res.status(200).end();
});
});
};