web frontend
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
Wojciech Kwolek 2021-10-10 23:33:40 +02:00
parent 0c75f9fd36
commit c6387c6ab7
3 changed files with 151 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist/

17
main.go
View File

@ -41,6 +41,15 @@ func (ml *MessageList) List() []Message {
return ml.list
}
func (ml *MessageList) Reverse() []Message {
r := make([]Message, len(ml.list))
l := len(ml.list) - 1
for i, m := range ml.list {
r[l-i] = m
}
return r
}
func CheckString(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] < 32 || s[i] > 126 {
@ -82,7 +91,7 @@ func main() {
term.Escape.Blue, pkHexShort,
term.Escape.Reset)
fmt.Fprintf(s, "%sRun %s`w`%s to write a message, %s`p`%s to print existing messages and %s`q`%s to quit.%s\n\n",
fmt.Fprintf(s, "%sRun %s`w`%s to write a message, %s`p`%s to print existing messages and %s`q`%s to quit.%s\n",
term.Escape.Blue,
term.Escape.Cyan, term.Escape.Blue,
term.Escape.Cyan, term.Escape.Blue,
@ -90,7 +99,7 @@ func main() {
term.Escape.Reset)
for {
term.SetPrompt(fmt.Sprintf("%s>%s ", term.Escape.Blue, term.Escape.Reset))
term.SetPrompt(fmt.Sprintf("\n%s>%s ", term.Escape.Blue, term.Escape.Reset))
cmd, err := term.ReadLine()
if err == io.EOF {
cmd = "q"
@ -151,5 +160,9 @@ func main() {
}
})
web := Web{
Messages: m,
}
go web.ListenAndServe(":8080")
log.Fatal(ssh.ListenAndServe(":2222", nil, pkAuth))
}

135
web.go Normal file
View File

@ -0,0 +1,135 @@
package main
import (
"html/template"
"net/http"
)
type Web struct {
Messages *MessageList
}
var siteTemplate string = `
<!DOCTYPE html>
<title>messages.sh</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>messages.sh</h1>
<p class="info">Hello! To add your own message, run:</p>
<pre class="info"><code>ssh messages.sh</code></pre>
<h2>Latest messages</h2>
<p>
{{range .}}
<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) {
w.Messages.Add(Message{
Content: "2137 papiez",
Username: "weeeoefka",
PKShort: "23daa6f",
})
w.Messages.Add(Message{
Content: "2137 papiez lorem ipsum dolan sit amet saldkjaksjdf sdkjf slkdj flskjdf lkjsdlkfj ",
Username: "weeeoe",
PKShort: "23daa6f",
})
tmpl := template.Must(template.New("tmpl").Parse(siteTemplate))
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
tmpl.Execute(rw, w.Messages.Reverse())
})
http.ListenAndServe(addr, nil)
}