Added support for a X-Client-Timezone header to convert dates to client's local format gotten from IP using GeoIP.

This commit is contained in:
Alexander Andreev 2021-02-10 03:24:14 +04:00
parent 73795b3755
commit 60ca5a1463
Signed by: Arav
GPG Key ID: 610DF2574456329F
6 changed files with 21 additions and 10 deletions

View File

@ -65,6 +65,7 @@ function setRoutes(router) {
await ctx.render('mindflow', {
title: "/ Mindflow",
description: "Here I will post updates on my infrastructure, my very important opinions and thoughts.",
tz: util.getTimezone(ctx),
diary: await mindflow.getPosts("Diary"),
updates: await mindflow.getPosts("Update")
})
@ -101,6 +102,7 @@ function setRoutes(router) {
return await ctx.render('guestbook', {
title: "/ Guestbook",
description: "This is my guestbook. Welcome.",
tz: util.getTimezone(ctx),
owner: config.dwelling.guestbook.owner,
posts: posts,
error: posts === null,
@ -131,8 +133,8 @@ module.exports = () => {
const pug = new koaPug({
viewPath: path.join(__dirname, "views", "dwelling"),
locals: {
date_: date => util.datetime(date, util.formats.post_date),
mindflowDateToId: date => util.datetime(date, util.formats.id_date),
date_: (date, tz) => util.datetime(date, util.formats.post_date, tz),
mindflowDateToId: (date, tz) => util.datetime(date, util.formats.id_date, tz),
rssLink: util.rssLink },
app: app
});
@ -140,6 +142,7 @@ module.exports = () => {
const dwelling_router = koaRouter();
setRoutes(dwelling_router);
app.proxy = true;
app
.use(async (ctx, next) => {
try {

View File

@ -127,6 +127,7 @@ module.exports = () => {
const radio_router = koaRouter();
setRoutes(radio_router);
app.proxy = true;
app
.use(koaRange)
.use(radio_router.routes())

View File

@ -52,6 +52,7 @@ module.exports = () => {
const radio_router = koaRouter();
setRoutes(radio_router);
app.proxy = true;
app
// .use(koaServe(path.join("static", "shared")))
// .use(koaServe(path.join("static", "radio")))

View File

@ -5,6 +5,11 @@ const formats = Object.freeze({
file_date: "YYYY-MM-DD HH:mm:ss z",
id_date: "YYYYMMDD-HHmm" });
function getTimezone(ctx) {
if (ctx.header['x-client-timezone'] !== undefined)
return ctx.header['x-client-timezone'];
}
function datetime(date, format=formats.post_date, timezone="UTC") {
return moment.utc(date).tz(timezone).format(format);
}
@ -20,6 +25,7 @@ exports.getBaseHost = (host) => {
}
}
exports.getTimezone = getTimezone;
exports.formats = formats;
exports.datetime = datetime;

View File

@ -31,13 +31,13 @@ block content
each post in posts
article
header
| Posted by #[span.highlighted= post.name] #{post.email} #{post.website} on #[time(datetime=post.created)= date_(post.created)]
| Posted by #[span.highlighted= post.name] #{post.email} #{post.website} on #[time(datetime=post.created)= date_(post.created, tz)]
each line in post.message.split("\n")
p= line
if post.feedback
.feedback
header
| Feedback by #[span.highlighted #{owner}] on #[time(datetime=post.feedback_created)= date_(post.feedback_created)]
| Feedback by #[span.highlighted #{owner}] on #[time(datetime=post.feedback_created)= date_(post.feedback_created, tz)]
each line in post.feedback.split("\n")
p= line
else

View File

@ -18,27 +18,27 @@ block content
h2 Updates
if (updates)
each update in updates
article(id='update-'+mindflowDateToId(update.date))
article(id='update-'+mindflowDateToId(update.date, tz))
header
a(href='#update-'+mindflowDateToId(update.date))
a(href='#update-'+mindflowDateToId(update.date, tz))
h3= update.title
each line in update.body.split('\n')
p!= line
footer
time(datetime=update.date)= date_(update.date)
time(datetime=update.date)= date_(update.date, tz)
else
p.center No updates? There must be some... Looks like database isn't connected.
section
h2 Diary
if (diary)
each entry in diary
article(id='diary-'+mindflowDateToId(entry.date))
article(id='diary-'+mindflowDateToId(entry.date, tz))
header
a(href='#diary-'+mindflowDateToId(entry.date))
a(href='#diary-'+mindflowDateToId(entry.date, tz))
h3= entry.title
each line in entry.body.split('\n')
p!= line
footer
time(datetime=entry.date)= date_(entry.date)
time(datetime=entry.date)= date_(entry.date, tz)
else
p.center No records? Well, that's definitely something's wrong happened to the database. :(