From c5bd1c5707ac600564617746cb21a42794f0143c Mon Sep 17 00:00:00 2001 From: Wojciech Kwolek Date: Mon, 18 Oct 2021 16:59:31 +0200 Subject: [PATCH] allow customizing the addresses on the site --- .env.sample | 4 +++- entrypoint.sh | 10 +++++++++- main.go | 4 ++++ web.go | 18 +++++++++++++----- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.env.sample b/.env.sample index 12c2f37..f213b78 100644 --- a/.env.sample +++ b/.env.sample @@ -1,3 +1,5 @@ SSH_KEY=./id_ed25519 SSH_ADDR=:2222 -HTTP_ADDR=:8080 \ No newline at end of file +HTTP_ADDR=:8080 +SITE_NAME=localhost:8080 +SSH_REMOTE_ADDR=localhost:2222 \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 4896eaf..2eb3efa 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -4,8 +4,16 @@ [[ ! -f $SSH_KEY ]] && ssh-keygen -t ed25519 -f "$SSH_KEY" -q -N "" [[ -z $SSH_ADDR ]] && SSH_ADDR=:2222 [[ -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)" messagessh \ No newline at end of file diff --git a/main.go b/main.go index 311d5df..90fc507 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,8 @@ func main() { sshKey := ssh.HostKeyFile(os.Getenv("SSH_KEY")) sshAddr := os.Getenv("SSH_ADDR") httpAddr := os.Getenv("HTTP_ADDR") + siteName := os.Getenv("SITE_NAME") + sshRemoteAddr := os.Getenv("SSH_REMOTE_ADDR") m := NewMessageList(10) pkAuth := ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool { @@ -170,6 +172,8 @@ func main() { web := Web{ Messages: m, + SiteName: siteName, + SSHAddr: sshRemoteAddr, } go web.ListenAndServe(httpAddr) log.Fatal(ssh.ListenAndServe(sshAddr, nil, pkAuth, sshKey)) diff --git a/web.go b/web.go index f82b76d..f69c06f 100644 --- a/web.go +++ b/web.go @@ -1,17 +1,20 @@ package main import ( + "fmt" "html/template" "net/http" ) type Web struct { Messages *MessageList + SiteName string + SSHAddr string } var siteTemplate string = ` -msg.kwolek.io +{{ .Name }} -

msg.kwolek.io

+

{{ .Name }}

Hello! To add your own message, run:

-
ssh msg.kwolek.io
+
ssh {{ .Addr }}

Latest messages

- {{range .}} + {{range .Msgs}}

{{ .Timestamp.Format "2006-01-02 15:04:05 MST" }}
{{ .Username }} ({{ .PKShort }})
{{ .Content }}

@@ -129,7 +132,12 @@ 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, 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) }