http root serves public/root.html

master
Bel LaPointe 2023-04-07 12:19:31 -06:00
parent a8b5942833
commit 933f87d11d
2 changed files with 21 additions and 0 deletions

20
http.go
View File

@ -2,8 +2,10 @@ package main
import (
"context"
_ "embed"
"fmt"
"net/http"
"os"
)
type Context struct {
@ -12,6 +14,12 @@ type Context struct {
func HTTP(port int, db DB) error {
foo := func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
httpRoot(w, r)
default:
http.NotFound(w, r)
}
}
foo = withAuth(foo)
return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(foo))
@ -41,3 +49,15 @@ func withAuth(foo http.HandlerFunc) http.HandlerFunc {
foo(w, r)
}
}
//go:embed public/root.html
var httpRootHTML string
func httpRoot(w http.ResponseWriter, r *http.Request) {
body := httpRootHTML
if os.Getenv("DEBUG") {
b, _ := os.ReadFile("public/root.html")
body = string(b)
}
w.Write([]byte(body))
}

1
public/root.html Normal file
View File

@ -0,0 +1 @@
hi