Skip to content

Commit

Permalink
feat: add options for template params
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-johnson committed Feb 29, 2016
1 parent 6da4ea1 commit 4ea61ff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
"tape": "^4.4.0",
"semantic-release": "^4.3.5"
},
"keywords": ["tyler","johnson","tylerjohnson.me","tjme"],
"keywords": [
"tyler",
"johnson",
"tylerjohnson.me",
"tjme"
],
"license": "MIT"
}
4 changes: 2 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import minimist from "minimist";
import {merge,repeat} from "lodash";
import {merge,repeat,pick} from "lodash";
import {readFileSync} from "fs";
import config from "cloud-env";

Expand Down Expand Up @@ -42,7 +42,7 @@ if (argv.config) {
if (src) merge(argv, JSON.parse(src));
}

const app = createApp(argv);
const app = createApp(pick(argv, "hostname"));

app.listen(
argv.port || config.PORT || 8080,
Expand Down
29 changes: 26 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import express from "express";
import nunjucks from "nunjucks";
import {colors} from "./colors";
import {assign,pick} from "lodash";
import {hostname} from "os";

export default function createApp() {
const defaults = {
version: "edge",
...pick(require("./package.json"), "name", "version"),
hostname: hostname()
};

export default function createApp(opts) {
let app = express();
opts = assign({}, defaults, opts);

// create nunjucks enviornment
app.set("view engine", "html");
Expand All @@ -12,17 +21,31 @@ export default function createApp() {
autoescape: true
});

app.locals.title = "My Name is Tyler Johnson.";
app.locals.colors = colors;
// construct template data
assign(app.locals, {
...pick(opts, "name", "version"),
...opts.template,
colors,
title: "My Name is Tyler Johnson."
});

app.disable("x-powered-by");
app.use(function(req, res, next) {
res.set("X-Served-By", `${opts.name} ${opts.version} (${opts.hostname})`);
next();
});

// home page
app.get("/", function(req, res) {
res.render("index");
});

// styles
app.get("/styles.css", function(req, res) {
res.sendFile(__dirname + "/styles.css");
});

// 404
app.use(function(req, res) {
res.status(404).render("notfound");
});
Expand Down
1 change: 1 addition & 0 deletions views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<html lang="en-US">
<head>
<!-- {{ name }} {{ version }} -->
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{{ title }}</title>
Expand Down

0 comments on commit 4ea61ff

Please sign in to comment.