Skip to content

Commit

Permalink
Initialize repo
Browse files Browse the repository at this point in the history
  • Loading branch information
coco98 committed Aug 2, 2016
0 parents commit 0f19ef9
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rundocker.sh
node_modules
npm-debug.log
.DS_STORE
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:4.4.4

COPY app/node_modules /app/node_modules
COPY app/bin /app/bin
COPY app/static /app/static
COPY app/hasuraconfig.js /app/hasuraconfig.js
COPY app/package.json /app/package.json
COPY app/runserver.sh /app/runserver.sh
COPY app/.babelrc /app/.babelrc
COPY app/server.babel.js /app/server.babel.js
COPY app/src /app/src

CMD /app/runserver.sh
17 changes: 17 additions & 0 deletions app/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"presets": ["es2015", "stage-0"],

"plugins": [
"transform-runtime",
"add-module-exports",
"transform-decorators-legacy"
],

"env": {
"development": {
"plugins": [
"typecheck"
]
}
}
}
10 changes: 10 additions & 0 deletions app/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[*]
indent_style = space
end_of_line = lf
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false
31 changes: 31 additions & 0 deletions app/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": "airbnb-base",
"root": true,
"rules": {
"object-curly-spacing": ["error", "always"],
"strict": 0,
"no-underscore-dangle": 0,
"curly": 0,
"no-multi-spaces": 0,
"key-spacing": 0,
"no-return-assign": 0,
"consistent-return": 0,
"no-shadow": 0,
"comma-dangle": 0,
"no-use-before-define": 0,
"no-empty": 0,
"new-parens": 0,
"no-console": 0,
"no-cond-assign": 0,
"no-fallthrough": 0,
"new-cap": 0,
"no-loop-func": 0,
"no-unreachable": 0,
"no-process-exit": 0,
"quotes": [1, "single", "avoid-escape"]
},
"env": {
"node": true,
"es6": true
}
}
2 changes: 2 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
tmp
8 changes: 8 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Installation
* Run `npm install`, if not already.
* Run this example that uses the StackExchange API.
* Change the namespace, schema and other details on the `package.json` file to use your API server.

# Usage
* For development environment, run the commands `npm run start-dev`.
* For production environment, run the command `npm run start-prod`.
10 changes: 10 additions & 0 deletions app/bin/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node
require('../server.babel'); // babel registration (runtime transpilation for node)
var path = require('path');
var rootDir = path.resolve(__dirname, '..');

global.__SERVER__ = true;
global.__DEVELOPMENT__ = process.env.NODE_ENV !== 'production';

// Load the server
require('../src/server');
4 changes: 4 additions & 0 deletions app/hasuraconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
appHost: '0.0.0.0',
port: { development: 3000, production: 8080},
};
76 changes: 76 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "hasura-example-app",
"description": "Example of a web server using Express with Babel, ESLint and reloading.",
"author": "<AuthorName> <<AuthorEmail>> (<AuthorSite>)",
"license": "MIT",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://<git-site>/<author/<repo>"
},
"homepage": "",
"keywords": [
"hasura",
"nodemon",
"express",
"starter",
"boilerplate",
"babel",
"eslint"
],
"main": "bin/server.js",
"scripts": {
"start-prod": "better-npm-run start-prod",
"lint": "eslint -c .eslintrc src",
"start-dev": "better-npm-run start-dev"
},
"betterScripts": {
"start-prod": {
"command": "node ./bin/server.js",
"env": {
"NODE_PATH": "./src",
"NODE_ENV": "production",
"PORT": 8080
}
},
"start-dev": {
"command": "nodemon ./bin/server.js --exec '`npm bin`/eslint -c .eslintrc src && node'",
"env": {
"NODE_PATH": "./src",
"NODE_ENV": "development",
"PORT": 3000,
"NAMESPACE": "api",
"BASE_DOMAIN": "stackexchange.com",
"SCHEME": "http"
}
}
},
"dependencies": {
"babel-polyfill": "^6.3.14",
"express": "^4.13.3",
"isomorphic-fetch": "^2.2.1",
"morgan": "^1.7.0",
"nodemon": "^1.10.0",
"pretty-error": "^1.2.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-eslint": "^6.1.2",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.3.13",
"babel-plugin-typecheck": "^3.6.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"babel-register": "^6.3.13",
"babel-runtime": "^6.3.19",
"better-npm-run": "^0.0.8",
"eslint": "^3.1.0",
"eslint-config-airbnb-base": "^5.0.0",
"eslint-plugin-babel": "^3.3.0",
"eslint-plugin-import": "^1.0.0"
},
"engines": {
"node": "4.4.7"
}
}
2 changes: 2 additions & 0 deletions app/runserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run start-prod
15 changes: 15 additions & 0 deletions app/server.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// enable runtime transpilation to use ES6/7 in node

var fs = require('fs');

var babelrc = fs.readFileSync('./.babelrc');
var config;

try {
config = JSON.parse(babelrc);
} catch (err) {
console.error('==> ERROR: Error parsing your .babelrc.');
console.error(err);
}

require('babel-core/register')(config);
8 changes: 8 additions & 0 deletions app/src/Globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Set globals from env variables
const globals = {
namespace: process.env.NAMESPACE,
projectDomain: process.env.BASE_DOMAIN,
scheme: process.env.SCHEME
};

export default globals;
25 changes: 25 additions & 0 deletions app/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require('babel-polyfill');
const hasuraConfig = require('../hasuraconfig');

const host = hasuraConfig.appHost;
const port = hasuraConfig.port[process.env.NODE_ENV || 'development'];

const environment = {
development: {
isProduction: false,
},
production: {
isProduction: true,
},
}[process.env.NODE_ENV || 'development'];


module.exports = Object.assign({
host,
port,
apiHost: process.env.APIHOST || 'localhost',
apiPort: process.env.APIPORT,
app: {
title: 'Express Server Example'
}
}, environment);
42 changes: 42 additions & 0 deletions app/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Express from 'express';
import path from 'path';
import PrettyError from 'pretty-error';
import http from 'http';
import morgan from 'morgan';

import config from './config';

const pretty = new PrettyError();
const app = new Express();
const server = new http.Server(app);

// Express Logging Middleware
if (global.__DEVELOPMENT__)
app.use(morgan('combined'));
else
app.use(morgan('[:date[clf]]: :method :url :status :res[content-length] - :response-time ms'));

app.use('/static', Express.static(path.join(__dirname, '..', 'static')));

app.use((req, res) => {
if (req.originalUrl === '/404') {
res.status(404).send('Not Found');
if (global.__DEVELOPMENT__)
console.error(pretty.render(new Error('Ooh no! /404 asked.')));
} else {
res.status(200).send('Works');
}
});

// Listen at the server
if (config.port) {
server.listen(config.port, config.host, (err) => {
if (err) {
console.error(err);
}
console.info('----\n==> ✅ %s is running, talking to API server.', config.app.title);
console.info('==> 💻 Open http://%s:%s in a browser to view the app.', config.host, config.port);
});
} else {
console.error('==> ERROR: No PORT environment variable has been specified');
}

0 comments on commit 0f19ef9

Please sign in to comment.