Skip to content

Commit

Permalink
🍻
Browse files Browse the repository at this point in the history
  • Loading branch information
frodebjerke committed Mar 23, 2016
0 parents commit d6de352
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"es2015"
],
"plugins": [
"transform-object-rest-spread"
]
}
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

*.DS_Store
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("babel-polyfill");
require('babel-core/register');
require('./server');
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "something-smart",
"version": "0.0.1",
"description": "Quotes and stuff",
"scripts": {
"local": "nodemon index.js"
},
"keywords": [],
"author": "frode bjerke @frodebjerke",
"license": "ISC",
"dependencies": {
"babel": "^6.5.2",
"babel-core": "^6.7.4",
"babel-plugin-transform-object-rest-spread": "^6.6.5",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"co": "^4.6.0",
"express": "^4.13.4",
"express-handlebars": "^3.0.0",
"morgan": "^1.7.0",
"rethinkdbdash": "^2.2.18",
"winston": "^2.2.0"
},
"devDependencies": {
"nodemon": "^1.9.1"
}
}
16 changes: 16 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express';
import morgan from 'morgan';
import pages from './pages';
import exphbs from 'express-handlebars'

export default function App() {
const app = express();
app.use(morgan('combined'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.use('/assets', express.static('dist'));
app.use('/', pages())

return app;
}
18 changes: 18 additions & 0 deletions server/config/rethinkdb/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import rethinkdbdash from 'rethinkdbdash';

const host = 'localhost' || process.env.SS_RDB_HOST;
const dbname = 'somethingsmart' || process.env.SS_RDB_DB;

const r = rethinkdbdash({
host: host,
db: dbname
});

export default r;
export function* setupDb() {
var dbList = yield r.dbList();

if (dbList.indexOf(dbname) === -1) {
yield r.dbCreate(dbname);
}
}
6 changes: 6 additions & 0 deletions server/config/rethinkdb/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import r, { setupDb } from './connection';
import indices from './indices';
import tables, { ensureTables} from './tables';

export default r;
export { indices, tables, setupDb, ensureTables };
17 changes: 17 additions & 0 deletions server/config/rethinkdb/indices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const r = require('./connection');
const tables = require('./tables');

export default function* () {
return yield [
];
}

function* ensureIndex(table, column) {
try {
yield r.table(table).indexCreate(column)
} catch (error) {
if (error.name !== 'ReqlRuntimeError') {
throw new Error(error);
}
}
}
19 changes: 19 additions & 0 deletions server/config/rethinkdb/tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const r = require('./connection');

const tables = {
quotes: 'quotes'
}

export default tables;

export function* ensureTables(r) {
return yield Object.keys(tables).map(function* (tableName) {
try {
yield r.tableCreate(tableName);
} catch (error) {
if (error.name !== 'ReqlOpFailedError') {
throw new Error(error);
}
}
});
}
31 changes: 31 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import winston from 'winston';
import App from './app';
import co from 'co';
import r, {
setupDb,
ensureTables,
indices
} from './config/rethinkdb';

co(setupDb())
.then(() => {
winston.info("db setup done")
return co(ensureTables(r))
.catch((err) => {
winston.error('Failed to run database setup', err);
throw new Error('Could not start server');
})
})
.then(() => {
return co(indices())
.catch((err) => {
winston.error('Failed to ensure indices');
throw new Error('Could not start server', err);
})
}).catch((err) => { console.log(err)})

const app = App();
const port = process.env.PORT || 3000;
app.listen(port, () => {
winston.info(`Application running on port ${port}`);
});
20 changes: 20 additions & 0 deletions server/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Router } from 'express';
import co from 'co';
import { get as getQuotes } from '../repos/quote-repo';

export default function Views() {
const router = Router();

router.get('*', (req, res, next) => {
co(getQuotes())
.then((quotes) => {
const quote = quotes[0];
res.render('index', { quote });
})
.catch((err) => {
res.render('error', { err })
})
});

return router;
}
5 changes: 5 additions & 0 deletions server/repos/quote-repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import r, { tables } from '../config/rethinkdb';

export function* get() {
return r.table(tables.quotes);
}
4 changes: 4 additions & 0 deletions views/error.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<article>
<h1>Shit went the bucket</h1>
{{err}}
</article>
4 changes: 4 additions & 0 deletions views/index.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<article>
<h1>{{quote.text}}</h1>
<p>{{quote.by}}</p>
</article>
12 changes: 12 additions & 0 deletions views/layouts/main.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Something-smart</title>
</head>
<body>

{{{body}}}

</body>
</html>

0 comments on commit d6de352

Please sign in to comment.