Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Add REST API code from the chill-dashboard #1

Merged
merged 16 commits into from
Jun 23, 2017
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
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": ["es2015", "es2016", "es2017"],
"plugins": [
"dynamic-import-node"
]
}
18 changes: 18 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
engines:
eslint:
enabled: true
duplication:
enabled: true
config:
languages:
- javascript
fixme:
enabled: true
ratings:
paths:
- src/**
exclude_paths:
- dist/*
- test/**/*
- coverage/**/*
- node_modules/**/*
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
33 changes: 33 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
env:
es6: true
node: true
mocha: true

parserOptions:
ecmaVersion: 2017
sourceType: module

extends: 'eslint:recommended'
parser: babel-eslint
rules:
no-var: 2
key-spacing: 2
semi-spacing: 2
block-spacing: 2
spaced-comment: 2
callback-return: 2
space-infix-ops: 2
keyword-spacing: 2
newline-after-var: 2
space-before-blocks: 2
handle-callback-err: 2
newline-before-return: 2
semi: [2, 'always']
eqeqeq: [2, 'always']
eol-last: [2, 'always']
max-statements: [2, 30]
comma-dangle: [2, never]
indent: [2, 2, {'SwitchCase': 1}]
object-curly-spacing: [2, 'always']
comma-spacing: [2, {'before': false, 'after': true}]
quotes: [2, 'single', {'allowTemplateLiterals': true}]
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Dependency directories
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Yarn integrity file
.yarn-integrity

# IntelliJ IDE
.idea

# Build directory
dist

# Dotenv configuration
.env

# Visual Studio Code
.vscode

# Test Coverage
coverage

# Chill database
chill.db
chill.yml
9 changes: 9 additions & 0 deletions .nodemonignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Build directory
dist

# Test directory
test

# Other directories
src/seeds
src/migrations
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "8"
- "7"
- "6"
script:
- npm run test:coverage
after_script:
- npm run codecov
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ $ npm install
# Or using yarn
$ yarn
```
Create a config `chill.yml` file using the sample file.
```bash
$ cp chill.yml.dist chill.yml
```

Run migrations
```
$ yarn migrate
```

Then start the app
Then start the server by providing the config file path. If this is not provided, it will expect the file to be in the current directory.
```
$ npm start
$ CHILL_CONFIG=/path/to/chill.yml yarn start
```

## Contributing
Expand Down
46 changes: 46 additions & 0 deletions chill.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
logging:
level: debug

db:
client: 'sqlite3'
connection:
filename: './chill.db'

restApi:
port: 8000

monitoring:
minInterval: 1000
maxInterval: 10000
method: OPTIONS
downStatus: '^(5..|4..)$'

notifications:
slack:
enabled: false
endpoint: SLACK_WEBHOOK_ENDPOINT
hipchat:
enabled: false
authToken: HIPCHAT_WEBHOOK_TOKENID
roomId: HIPTCHAT_WEBHOOK_ROOMID
twilio:
enabled: false
sender: SENDING_NUMBER
receiver: RECEIVING_NUMBER
authToken: AUTH_TOKEN
accountSid: ACCOUNT_SID
email:
enabled: false
transport:
service: EMAIL_SERVICE # Check https://nodemailer.com/smtp/well-known/ for all email services
auth:
user: EMAIL_ACCOUNT_USERNAME
pass: EMAIL_ACCOUNT_PASSWORD
sender: SENDER_NAME_EMAIL
receivers:
- RECEIVER_EMAIL
templateDir: TEMPLATE_DIRECTORY

services:
- name: 'Localhost'
url: 'http://127.0.0.1'
11 changes: 11 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('babel-register');

const config = require('./src/config/config');
const dbConfig = config.resolve().db;

module.exports = Object.assign({}, dbConfig, {
migrations: {
directory: './src/migrations'
}
});

65 changes: 65 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "chill-rest-api",
"version": "1.0.0-alpha.1",
"description": "REST API for the chill monitoring service.",
"scripts": {
"lint": "eslint src test; exit 0",
"lint:fix": "eslint src test --fix; exit 0",
"start": "nodemon --watch src/ --exec babel-node src/ --source-maps",
"clean": "rimraf dist/",
"babel": "babel src/ -d dist/",
"build": "run-s lint clean babel",
"migrate": "knex migrate:latest",
"rollback": "knex migrate:rollback"
},
"keywords": [
"chill",
"rest-api"
],
"authors": [],
"license": "MIT",
"dependencies": {
"body-parser": "^1.15.2",
"bookshelf": "^0.10.2",
"bookshelf-camelcase": "^1.1.4",
"boom": "^4.2.0",
"compression": "^1.6.2",
"cors": "^2.8.1",
"express": "^4.14.1",
"helmet": "^3.2.0",
"http-status-codes": "^1.0.6",
"joi": "^10.1.0",
"knex": "^0.12.6",
"memory-cache": "^0.1.6",
"morgan": "^1.7.0",
"npm-run-all": "^4.0.2",
"serve-favicon": "^2.4.2",
"sqlite3": "^3.1.8",
"swagger-jsdoc": "^1.9.0",
"webpack-merge": "^4.1.0",
"winston": "^2.3.0",
"winston-daily-rotate-file": "^1.4.0",
"yamljs": "^0.2.10"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-jest": "^19.0.0",
"babel-loader": "^6.2.10",
"babel-plugin-dynamic-import-node": "^1.0.2",
"babel-preset-env": "^1.3.2",
"babel-preset-es2015": "^6.18.0",
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-register": "^6.24.1",
"eslint": "^3.19.0",
"nodemon": "^1.11.0",
"rimraf": "^2.6.1",
"semver": "^5.3.0"
},
"engines": {
"node": ">= 6.9.0",
"npm": ">= 3.10.8"
}
}
38 changes: 38 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import express from 'express';

import pkg from '../package';
import routes from './routes';
import logger from './utils/logger';
import bodyParser from 'body-parser';
import compression from 'compression';
import * as config from './config/config';
import * as errorHandler from './middlewares/errorHandler';

const app = express();

app.set('port', config.get().restApi.port);

app.locals.title = pkg.name;
app.locals.version = pkg.version;

app.use(cors());
app.use(helmet());
app.use(compression());
app.use(morgan('dev', { stream: logger().stream }));
app.use(bodyParser.json());

// API Routes
app.use('/api', routes);

// Error Middlewares
app.use(errorHandler.genericErrorHandler);
app.use(errorHandler.notFoundError);

app.listen(app.get('port'), () => {
logger().info(`Server listening is on port ${app.get('port')}`);
});

export default app;
51 changes: 51 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Note: This file is a total copy-paste from the chill repository
// we'll need to extract a new package called chill-core or chill-common where
// we can put all the common, initialization/config code.

import Yaml from 'yamljs';
import Promise from 'bluebird';
import cache from 'memory-cache';
import merge from 'webpack-merge';

import defaultConfig from './default.config';

global.Promise = Promise;

export const CACHE_KEY = 'config';
export const DEFAULT_FILENAME = 'chill.yml';

/**
* Resolve configuration by reading the configuration file.
*
* @param {String} [filename=DEFAULT_FILENAME]
* @returns {Object}
*/
export function resolve(filename = DEFAULT_FILENAME) {
process.stdout.write(`Loading config file: ${filename}\n`);

let loadedConfig = Yaml.load(filename);
let config = merge(defaultConfig, loadedConfig);

// Add monitoring config as defaults for each service configuration.
config.services = config.services.map(service => {
return merge(config.monitoring, service);
});

// Put the resolved config into the cache.
cache.put(CACHE_KEY, config);

return config;
}

/**
* Return the resolved configuration.
*
* @returns {Object}
*/
export function get() {
if (!cache.get(CACHE_KEY)) {
return resolve();
}

return cache.get(CACHE_KEY);
}
Loading