msg.kwolek.io/web.go

132 lines
2.1 KiB
Go

package main
import (
"html/template"
"net/http"
)
type Web struct {
Messages *MessageList
SiteName string
SSHAddr string
}
var siteTemplate string = `
<!DOCTYPE html>
<title>{{ .Name }}</title>
<meta charset="utf-8">
<style>
@import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono&display=swap');
body {
background: #333;
color: white;
font-family: 'Ubuntu Mono', Monaco, monospace;
max-width: 1024px;
margin: 0 auto;
padding: 1em;
}
h1, .info {
text-align: center;
}
h2 {
padding-top: 2em;
font-size: 2em;
}
pre {
font-size: 1.5em;
}
h1 {
font-size: 4em;
}
.message {
display: flex;
}
.message .meta {
display: flex;
}
.ts {
color: #aaa;
}
.message .meta,
.message .ts,
.message .username {
flex-shrink: 0;
}
.message .username {
text-align: right;
min-width: 12em;
padding: 0 .5em;
color: cyan;
}
.message .meta .username::before {
content: '<';
}
.message .meta .username::after {
content: '>';
}
@media (max-width: 1024px) {
.message, .meta {
flex-direction: column;
}
.message .username {
text-align: center;
padding: 0 0;
padding-top: 0.2em;
}
.message .content {
padding-left: 1em;
padding-top: 0.3em;
}
.message {
text-align: center;
}
h2 {
text-align: center;
font-size: 1.5em;
}
h1 {
font-size: 3em;
}
}
.message {
padding-bottom: 1em;
}
</style>
<h1>{{ .Name }}</h1>
<p class="info">Hello! To add your own message, run:</p>
<pre class="info"><code>ssh {{ .Addr }}</code></pre>
<h2>Latest messages</h2>
<p>
{{range .Msgs}}
<div class="message">
<div class="meta"><div class="ts">{{ .Timestamp.Format "2006-01-02 15:04:05 MST" }}</div> <div class="username">{{ .Username }} ({{ .PKShort }})</div></div> <div class="content">{{ .Content }}</div><br>
</div>
{{end}}
</p>
`
func (w *Web) ListenAndServe(addr string) {
tmpl := template.Must(template.New("tmpl").Parse(siteTemplate))
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
tmpl.Execute(rw, map[string]interface{}{
"Msgs": w.Messages.Reverse(),
"Name": w.SiteName,
"Addr": w.SSHAddr,
})
})
http.ListenAndServe(addr, nil)
}