-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.go
100 lines (86 loc) · 2.95 KB
/
server.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bytes"
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"text/template"
"github.com/a-h/templ"
"github.com/acaloiaro/hugo-htmx-go-template/partials"
)
//go:embed all:public
var content embed.FS
func main() {
mux := http.NewServeMux()
serverRoot, _ := fs.Sub(content, "public")
// Serve all hugo content (the 'public' directory) at the root url
mux.Handle("/", http.FileServer(http.FS(serverRoot)))
cors := func(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// in development, the Origin is the the Hugo server, i.e. http://localhost:1313
// but in production, it is the domain name where one's site is deployed
//
// CHANGE THIS: You likely do not want to allow any origin (*) in production. The value should be the base URL of
// where your static content is served
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, hx-target, hx-current-url, hx-request")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
return
}
h.ServeHTTP(w, r)
}
}
// Add any number of handlers for custom endpoints here
mux.HandleFunc("/goodbyeworld.html", cors(templ.Handler(partials.GoodbyeWorld())))
mux.HandleFunc("/hello_world", cors(http.HandlerFunc(helloWorld)))
mux.HandleFunc("/hello_world_form", cors(http.HandlerFunc(helloWorldForm)))
fmt.Printf("Starting API server on port 1314\n")
if err := http.ListenAndServe("0.0.0.0:1314", mux); err != nil {
log.Fatal(err)
}
}
// the handler accepts GET requests to /hello_world
// It checks the URL params for the "name" param and populates the html/template variable with its value
// if no "name" url parameter is present, "name" is defaulted to "World"
//
// It responds with the the HTML partial `partials/helloworld.html`
func helloWorld(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "null" || name == "" {
name = "World"
}
tmpl := template.Must(template.ParseFiles("partials/helloworld.html"))
buff := bytes.NewBufferString("")
err := tmpl.Execute(buff, map[string]string{"Name": name})
if err != nil {
ise(err, w)
return
}
w.WriteHeader(http.StatusOK)
w.Write(buff.Bytes())
}
// this handler accepts POST requests to /hello_world_form
// It checks the post request body for the form value "name" and populates the html/template
// variable with its value
//
// It responds with a simple greeting HTML partial
func helloWorldForm(w http.ResponseWriter, r *http.Request) {
name := "World"
if err := r.ParseForm(); err != nil {
ise(err, w)
return
}
name = r.FormValue("name")
if err := partials.HelloWorldGreeting(name).Render(r.Context(), w); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func ise(err error, w http.ResponseWriter) {
fmt.Fprintf(w, "error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}