2023-06-25 13:52:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-04 17:29:25 +00:00
|
|
|
"bytes"
|
2023-06-25 13:52:00 +00:00
|
|
|
"embed"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2023-07-04 17:29:25 +00:00
|
|
|
"text/template"
|
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
|
2023-07-04 17:29:25 +00:00
|
|
|
//
|
|
|
|
|
// 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
|
2023-07-04 08:49:44 +00:00
|
|
|
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 17:29:25 +00:00
|
|
|
tmpl := template.Must(template.ParseFiles("partials/helloworld.html"))
|
2023-09-19 11:56:50 +00:00
|
|
|
buff := bytes.NewBufferString("")
|
2023-07-04 17:29:25 +00:00
|
|
|
err := tmpl.Execute(buff, map[string]string{"Name": name})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ise(err, w)
|
2023-06-25 13:52:00 +00:00
|
|
|
return
|
|
|
|
|
}
|
2023-07-04 17:29:25 +00:00
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
w.Write(buff.Bytes())
|
2023-06-25 13:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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")
|
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)
|
|
|
|
|
}
|