-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (35 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"net/http"
"chftm.dev/nuclear-hack-2024/views"
"github.com/a-h/templ"
)
func main() {
mux := http.NewServeMux()
index := views.Index()
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
serveHtmxPage(r, w, index)
})
mux.HandleFunc("POST /upload", func(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
panic(err) // TODO
}
defer file.Close()
result, id := analyze(file)
w.Header().Add("HX-Push-Url", fmt.Sprint("/result/", id))
serveHtmxPage(r, w, views.Result(result.Emotion))
})
mux.HandleFunc("GET /result/{id}", func(w http.ResponseWriter, r *http.Request) {
result := fetch(r.PathValue("id"))
serveHtmxPage(r, w, views.Result(result.Emotion))
})
http.ListenAndServe(":8080", mux)
}
func serveHtmxPage(r *http.Request, w http.ResponseWriter, component templ.Component) {
if r.Header.Get("HX-Request") != "true" {
component = views.Page(component)
}
component.Render(r.Context(), w)
}