Dwelling/radio.js

60 lines
1.7 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const Koa = require("koa");
const koaPug = require("koa-pug");
const koaRouter = require("koa-router");
const fetch = require("node-fetch");
const config = require("./config");
const util = require("./util");
function setRoutes(router) {
router.get('/', async ctx => {
await ctx.render('index', {
title: "/ Radio",
description: "Internet-radio broadcasting from under my desk.",
main_site: util.getServiceByHost(ctx.header.host)
});
})
.get('/filelist', async ctx => {
ctx.type = 'text/html';
ctx.body = fs.readFileSync(
path.join(__dirname, '/static/radio/assets/files/radio_filelist.html'));
})
.get('/playlist', async ctx => {
ctx.attachment('radio.arav.top.m3u');
ctx.body = fs.readFileSync(
path.join(__dirname, '/static/radio/assets/files/radio.arav.top.m3u'));
})
.get('/stats', async ctx => {
let stats = await fetch('http://radio.home/status-json.xsl').then(r => r.json());
ctx.body = {
server_start_iso8601: stats.icestats.server_start_iso8601,
server_start_date: util.datetime(stats.icestats.server_start_iso8601, util.post_date_format),
song: `${stats.icestats.source.artist} - ${stats.icestats.source.title}`,
listener_peak: stats.icestats.source.listener_peak,
listeners: stats.icestats.source.listeners,
};
});
}
module.exports = () => {
const app = new Koa();
const pug = new koaPug({
viewPath: path.join(__dirname, "views", "radio"),
locals: {
moment: util.datetime },
app: app
});
const radio_router = koaRouter();
setRoutes(radio_router);
app.proxy = true;
app
.use(radio_router.routes())
.listen(config.radio.port, config.radio.host);
return app;
}