http root serves public/root.html
This commit is contained in:
20
http.go
20
http.go
@@ -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
1
public/root.html
Normal file
@@ -0,0 +1 @@
|
||||
hi
|
||||
Reference in New Issue
Block a user