hugo-htmx-go-template/server.go

91 lines
2.7 KiB
Go
Raw Normal View History

2023-06-25 13:52:00 +00:00
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
2023-07-04 08:49:44 +00:00
"github.com/a-h/templ"
"github.com/acaloiaro/hugo-htmx-go-template/partials"
2023-06-25 13:52:00 +00:00
)
//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)))
2023-07-04 08:49:44 +00:00
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
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)
}
}
2023-06-25 13:52:00 +00:00
// Add any number of handlers for custom endpoints here
2023-07-04 08:49:44 +00:00
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)))
2023-06-25 13:52:00 +00:00
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"
}
2023-07-04 08:49:44 +00:00
if err := partials.HelloWorld(name).Render(r.Context(), w); err != nil {
w.WriteHeader(http.StatusInternalServerError)
2023-06-25 13:52:00 +00:00
return
}
}
// 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"
// The name is not in the query param, let's see if it was submitted as a form
if err := r.ParseForm(); err != nil {
ise(err, w)
return
}
name = r.FormValue("name")
2023-07-04 08:49:44 +00:00
if err := partials.HelloWorldGreeting(name).Render(r.Context(), w); err != nil {
w.WriteHeader(http.StatusInternalServerError)
2023-06-25 13:52:00 +00:00
return
}
}
func ise(err error, w http.ResponseWriter) {
fmt.Fprintf(w, "error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}