Skip to content

Commit ade3cae

Browse files
committed
Embed web page into the binary
1 parent 98a6564 commit ade3cae

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ RELAY_PUBKEY="RelayPublicKey"
3636
# The path can be relative to the binary, or absolute
3737
DB_PATH="db/"
3838

39-
# Where we should store the index.html and static files
40-
# The path can be relative to the binary, or absolute
41-
WEB_PATH="web/"
42-
4339
# Interval in hours to refresh the web of trust
4440
REFRESH_INTERVAL_HOURS=24
4541

main.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"context"
5+
"embed"
56
"fmt"
67
"html/template"
78
"io"
9+
"io/fs"
810
"log"
911
"net/http"
1012
"os"
@@ -20,6 +22,12 @@ import (
2022
"github.com/nbd-wtf/go-nostr/nip10"
2123
)
2224

25+
//go:embed template/index.html
26+
var indexHTML string
27+
28+
//go:embed template/assets
29+
var assets embed.FS
30+
2331
var (
2432
version string
2533
)
@@ -150,13 +158,16 @@ func main() {
150158
wg.Wait()
151159

152160
mux := relay.Router()
153-
web := http.FileServer(http.Dir(config.WebPath))
154161

155-
mux.Handle("GET /web/", http.StripPrefix("/web/", web))
156-
mux.Handle("GET /favicon.ico", http.StripPrefix("/", web))
162+
serverRoot, fsErr := fs.Sub(assets, "template/assets")
163+
if fsErr != nil {
164+
log.Fatal(fsErr)
165+
}
166+
mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(serverRoot))))
167+
mux.Handle("/favicon.ico", http.FileServer(http.FS(serverRoot)))
157168

158169
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
159-
tmpl := template.Must(template.ParseFiles(config.WebPath + "index.html"))
170+
tmpl := template.Must(template.New("index").Parse(indexHTML))
160171
data := struct {
161172
RelayName string
162173
RelayPubkey string
File renamed without changes.
File renamed without changes.

web/index.html template/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta name="theme-color" content="#000000" />
55
<meta charset="UTF-8" />
6-
<link rel="icon" href="/web/favicon.ico" type="image/x-icon" />
6+
<link rel="icon" href="/assets/favicon.ico" type="image/x-icon" />
77
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
88
<title>{{.RelayName}}</title>
99
<script src="https://cdn.tailwindcss.com"></script>
@@ -12,7 +12,7 @@
1212
<div class="flex-grow flex flex-col justify-center items-center px-4">
1313
<!-- Container -->
1414
<div class="flex flex-col text-center max-w-2xl">
15-
<img src="/web/icon.png" class="self-center w-32" />
15+
<img src="/assets/icon.png" class="self-center w-32" />
1616

1717
<h1 class="text-5xl md:text-6xl font-bold text-rose-500 mb-6">
1818
{{.RelayName}}

0 commit comments

Comments
 (0)