2021-02-10 01:01:50 +04:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
const Koa = require("koa");
|
|
|
|
const koaPug = require("koa-pug");
|
|
|
|
const koaRouter = require("koa-router");
|
2021-02-10 02:02:28 +04:00
|
|
|
// const koaServe = require("koa-static");
|
2021-02-10 01:01:50 +04:00
|
|
|
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.",
|
2021-02-11 02:57:18 +04:00
|
|
|
main_site: util.getServiceByHost(ctx.header.host)
|
2021-02-10 01:01:50 +04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.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);
|
|
|
|
|
2021-02-10 03:24:14 +04:00
|
|
|
app.proxy = true;
|
2021-02-10 01:01:50 +04:00
|
|
|
app
|
2021-02-10 02:02:28 +04:00
|
|
|
// .use(koaServe(path.join("static", "shared")))
|
|
|
|
// .use(koaServe(path.join("static", "radio")))
|
2021-02-10 01:01:50 +04:00
|
|
|
.use(radio_router.routes())
|
|
|
|
.listen(config.radio.port, config.radio.host);
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|