Change template engine from mustache to handlebars

This commit is contained in:
Annika Backstrom 2019-12-15 11:50:53 -05:00
parent 5782510af1
commit 7e8f11a33c
12 changed files with 1854 additions and 181 deletions

View File

@ -1 +1,5 @@
CREATE TABLE tags(uuid TEXT PRIMARY KEY, tag TEXT); CREATE TABLE tags(uuid TEXT PRIMARY KEY, tag TEXT, label TEXT);
INSERT INTO tags (uuid, tag, label) VALUES
("585687cb-1ff7-4106-a888-a40ff09b7fa1", "12121212", "OK Then"),
("85a6f785-51e1-4220-be75-fd306b973221", "12121212", "Other"),
("b4eabe35-9f96-4455-9294-56cc375cfbc4", "badc0ffee", " Brazil Theme");

View File

@ -1,13 +1,9 @@
"use strict"; "use strict";
const sqlite3 = require('sqlite3');
class SqliteBackend { class SqliteBackend {
constructor(config) { constructor(config, db) {
this.config = config; this.config = config;
sqlite3.verbose(); this.db = db;
this.db = new sqlite3.Database(this.config.db, sqlite3.OPEN_READONLY);
console.log(this.db);
} }
find(tag, callback) { find(tag, callback) {
@ -20,8 +16,8 @@ class SqliteBackend {
} }
} }
module.exports = function(config) { module.exports = function(config, db) {
return new SqliteBackend(config); return new SqliteBackend(config, db);
}; };
// vim:ts=2 sw=2 et: // vim:ts=2 sw=2 et:

View File

@ -1,61 +0,0 @@
"use strict";
const mustache = require('mustache');
const readFileSync = require('fs').readFileSync;
var template_dir = undefined;
class View {
constructor() {
this.template = readFileSync(template_dir + this.path(), {encoding: 'utf8'})
mustache.parse(this.template)
}
render(vars) {
return mustache.render(this.template, vars, this.partials());
}
path() {
throw 'Subclasses must define a template path';
}
partials() {
return {};
}
}
class BaseView extends View {
path() {
return 'base.mustache';
}
}
class IndexView extends View {
path() {
return 'index.mustache';
}
partials() {
return {
log: views.log.template
}
}
}
class LogView extends View {
path() {
return 'log.mustache';
}
}
const views = {};
module.exports = function(tpl_dir) {
template_dir = tpl_dir;
views.base = new BaseView();
views.index = new IndexView();
views.log = new LogView();
return views;
}

1820
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,9 +17,9 @@
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"bufferutil": "^4.0.1", "bufferutil": "^4.0.1",
"express": "^4.17.1", "express": "^4.17.1",
"express-handlebars": "^3.1.0",
"glob": "^7.1.6", "glob": "^7.1.6",
"morgan": "^1.7.0", "morgan": "^1.7.0",
"mustache": "^3.1.0",
"sqlite": "^3.0.3", "sqlite": "^3.0.3",
"throttle-debounce": "^2.1.0", "throttle-debounce": "^2.1.0",
"utf-8-validate": "^5.0.2", "utf-8-validate": "^5.0.2",

View File

@ -2,16 +2,13 @@
const config = require('./config.json') const config = require('./config.json')
const views = require('./jukebox/views')(__dirname + '/templates/');
const library = require('./jukebox/library'); const library = require('./jukebox/library');
const MediaLibrarySqliteBackend = require('./jukebox/library/sqlite-backend')(config);
const MediaLibrary = new library.Library(config, MediaLibrarySqliteBackend);
const ScriptRunner = require('./jukebox/scripts')(config); const ScriptRunner = require('./jukebox/scripts')(config);
const express = require('express'); const express = require('express');
const morgan = require('morgan') const morgan = require('morgan');
const handlebars = require('express-handlebars');
const { createLogger, format, transports } = require('winston'); const { createLogger, format, transports } = require('winston');
@ -29,6 +26,12 @@ const MediaPlayer = require('./jukebox/media-player')(config, logger);
const TagReader = require('./jukebox/tag-reader')(config, logger); const TagReader = require('./jukebox/tag-reader')(config, logger);
const play_log = require('./jukebox/library/play-log'); const play_log = require('./jukebox/library/play-log');
const sqlite3 = require('sqlite3');
var db = new sqlite3.Database(config.db);
const MediaLibrarySqliteBackend = require('./jukebox/library/sqlite-backend')(config, db);
const MediaLibrary = new library.Library(config, MediaLibrarySqliteBackend);
const PlayLog = new play_log.PlayLog(); const PlayLog = new play_log.PlayLog();
var app = express(); var app = express();
@ -86,37 +89,53 @@ MediaPlayer.on('command', tag => {
PlayLog.updateLog(tag); PlayLog.updateLog(tag);
}); });
var hbs = handlebars.create({
extname: ".hbs",
});
MediaLibrary.on('action', MediaPlayer.handleTag.bind(MediaPlayer));
app.engine(".hbs", hbs.engine);
app.set('view engine', '.hbs');
app.set('views', __dirname + '/views');
app.use(morgan('dev'))
app.use(express.static(__dirname + '/static'))
PlayLog.on('update', tag => { PlayLog.on('update', tag => {
app.render("log", { layout: false, play_log: PlayLog.getLog(), }, (err, html) => {
var data = { var data = {
html: views.log.render({ play_log: PlayLog.getLog() }), html: html,
tag: tag.tag tag: tag.tag
}; };
wss.broadcast(JSON.stringify(data)); wss.broadcast(JSON.stringify(data));
}); });
});
MediaLibrary.on('action', MediaPlayer.handleTag.bind(MediaPlayer));
app.use(morgan('dev'))
app.use(express.static(__dirname + '/static'))
app.get('/', function (req, res, next) { app.get('/', function (req, res, next) {
try { res.render('index', {
var index = views.index.render({ last_tag: PlayLog.getLastTag(),
last_tag: PlayLog.getLastTag() config: config,
, config: config play_log: PlayLog.getLog(),
, play_log: PlayLog.getLog() });
});
app.get('/library', function (req, res, next) {
db.all("SELECT * FROM tags", (err, rows) => {
var content = views.library.render({
rows: rows,
config: config,
}); });
var html = views.base.render({ var html = views.base.render({
title: 'Home' title: 'Library',
, content: index content: content,
}) });
res.send(html) res.send(html);
} catch (e) { });
next(e)
}
}); });
wss.broadcast = function broadcast(data) { wss.broadcast = function broadcast(data) {

View File

@ -1,56 +1,23 @@
@import url("https://fonts.googleapis.com/css?family=Source+Code+Pro:400,200,700");
* { * {
margin: 0; margin: 0;
padding: 0; padding: 0;
box-sizing: border-box; box-sizing: border-box;
} }
body { body {
font-family: 'Source Code Pro', monospace; font-family: Arial;
margin: 1em;
} }
.header { h1 {
background: url("https://images.unsplash.com/photo-1451337516015-6b6e9a44a8a3?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925"); margin: 0.5em 0;
background-position: center; color: hsl(182.8, 70.2%, 42.2%)
} }
.header .page-title { li {
font-weight: 200; margin-left: 1em;
font-size: 80px;
text-align: center;
padding: 100px;
text-transform: uppercase;
color: #fff;
} }
.nav { p {
background: #000; margin: 1em 0;
text-align: center;
} }
.nav li { footer {
display: inline-block; margin-top: 2em;
} font-size: 0.75rem;
.nav a {
display: inline-block;
padding: 20px;
color: #fff;
text-decoration: none;
}
.nav a:hover {
background-color: #3ab795;
text-decoration: underline;
}
.main-content {
margin: 50px auto;
max-width: 600px;
}
.main-content p {
line-height: 1.6;
margin-bottom: 1.7em;
}
.footer {
margin: 50px auto;
max-width: 600px;
border-top: 1px solid #444;
padding: 20px 0;
}
.footer p {
color: #444;
font-size: 12px;
} }

View File

@ -4,6 +4,7 @@
<head> <head>
<title>jukebox.stop.wtf</title> <title>jukebox.stop.wtf</title>
<link rel=stylesheet href=/css/index.css> <link rel=stylesheet href=/css/index.css>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head> </head>
<body> <body>
@ -11,11 +12,11 @@
<h1 class=page-title>jukebox.stop.wtf</h1> <h1 class=page-title>jukebox.stop.wtf</h1>
</div> </div>
<div class="main-content"> <div class="main-content">
{{{ content }}} {{{body}}}
</div> </div>
<div class="footer"> <footer>
<p>made with love by annika</p> <p>made with love by annika</p>
</div> </footer>
<script src="/jukebox.js"></script> <script src="/jukebox.js"></script>
</body> </body>

1
views/library.hbs Normal file
View File

@ -0,0 +1 @@
hello

2
views/log.hbs Normal file
View File

@ -0,0 +1,2 @@
{{! Used for rending *just* the log line partial. }}
{{> log}}