allow customizing the addresses on the site
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Wojciech Kwolek 2021-10-18 16:59:31 +02:00
parent f84b0a16e1
commit c5bd1c5707
4 changed files with 29 additions and 7 deletions

View File

@ -1,3 +1,5 @@
SSH_KEY=./id_ed25519 SSH_KEY=./id_ed25519
SSH_ADDR=:2222 SSH_ADDR=:2222
HTTP_ADDR=:8080 HTTP_ADDR=:8080
SITE_NAME=localhost:8080
SSH_REMOTE_ADDR=localhost:2222

View File

@ -4,8 +4,16 @@
[[ ! -f $SSH_KEY ]] && ssh-keygen -t ed25519 -f "$SSH_KEY" -q -N "" [[ ! -f $SSH_KEY ]] && ssh-keygen -t ed25519 -f "$SSH_KEY" -q -N ""
[[ -z $SSH_ADDR ]] && SSH_ADDR=:2222 [[ -z $SSH_ADDR ]] && SSH_ADDR=:2222
[[ -z $HTTP_ADDR ]] && HTTP_ADDR=:8080 [[ -z $HTTP_ADDR ]] && HTTP_ADDR=:8080
[[ -z $SSH_REMOTE_ADDR ]] && {
if [[ ! -z $SITE_NAME ]]; then
SSH_REMOTE_ADDR=$SITE_NAME
else
SSH_REMOTE_ADDR=localhost:2222
fi
}
[[ -z $SITE_NAME ]] && SITE_NAME=localhost:8080
export SSH_KEY SSH_ADDR HTTP_ADDR export SSH_KEY SSH_ADDR HTTP_ADDR SSH_REMOTE_ADDR SITE_NAME
echo "Listening on $SSH_ADDR (ssh), $HTTP_ADDR (http)" echo "Listening on $SSH_ADDR (ssh), $HTTP_ADDR (http)"
messagessh messagessh

View File

@ -67,6 +67,8 @@ func main() {
sshKey := ssh.HostKeyFile(os.Getenv("SSH_KEY")) sshKey := ssh.HostKeyFile(os.Getenv("SSH_KEY"))
sshAddr := os.Getenv("SSH_ADDR") sshAddr := os.Getenv("SSH_ADDR")
httpAddr := os.Getenv("HTTP_ADDR") httpAddr := os.Getenv("HTTP_ADDR")
siteName := os.Getenv("SITE_NAME")
sshRemoteAddr := os.Getenv("SSH_REMOTE_ADDR")
m := NewMessageList(10) m := NewMessageList(10)
pkAuth := ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool { pkAuth := ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
@ -170,6 +172,8 @@ func main() {
web := Web{ web := Web{
Messages: m, Messages: m,
SiteName: siteName,
SSHAddr: sshRemoteAddr,
} }
go web.ListenAndServe(httpAddr) go web.ListenAndServe(httpAddr)
log.Fatal(ssh.ListenAndServe(sshAddr, nil, pkAuth, sshKey)) log.Fatal(ssh.ListenAndServe(sshAddr, nil, pkAuth, sshKey))

18
web.go
View File

@ -1,17 +1,20 @@
package main package main
import ( import (
"fmt"
"html/template" "html/template"
"net/http" "net/http"
) )
type Web struct { type Web struct {
Messages *MessageList Messages *MessageList
SiteName string
SSHAddr string
} }
var siteTemplate string = ` var siteTemplate string = `
<!DOCTYPE html> <!DOCTYPE html>
<title>msg.kwolek.io</title> <title>{{ .Name }}</title>
<meta charset="utf-8"> <meta charset="utf-8">
<style> <style>
@import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono&display=swap');
@ -103,12 +106,12 @@ h1 {
padding-bottom: 1em; padding-bottom: 1em;
} }
</style> </style>
<h1>msg.kwolek.io</h1> <h1>{{ .Name }}</h1>
<p class="info">Hello! To add your own message, run:</p> <p class="info">Hello! To add your own message, run:</p>
<pre class="info"><code>ssh msg.kwolek.io</code></pre> <pre class="info"><code>ssh {{ .Addr }}</code></pre>
<h2>Latest messages</h2> <h2>Latest messages</h2>
<p> <p>
{{range .}} {{range .Msgs}}
<div class="message"> <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 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> </div>
@ -129,7 +132,12 @@ func (w *Web) ListenAndServe(addr string) {
}) })
tmpl := template.Must(template.New("tmpl").Parse(siteTemplate)) tmpl := template.Must(template.New("tmpl").Parse(siteTemplate))
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
tmpl.Execute(rw, w.Messages.Reverse()) tmpl.Execute(rw, map[string]interface{}{
"Msgs": w.Messages.Reverse(),
"Name": w.SiteName,
"Addr": w.SSHAddr,
})
fmt.Println(w.SSHAddr)
}) })
http.ListenAndServe(addr, nil) http.ListenAndServe(addr, nil)
} }