31476802-go-microservice/front-end/cmd/web/main.go

47 lines
1,022 B
Go
Raw Normal View History

2025-05-16 14:39:17 -05:00
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
render(w, "test.page.gohtml")
})
2025-05-16 15:17:41 -05:00
fmt.Println("Starting front end service on port 8088")
err := http.ListenAndServe(":8088", nil)
2025-05-16 14:39:17 -05:00
if err != nil {
log.Panic(err)
}
}
func render(w http.ResponseWriter, t string) {
partials := []string{
"./front-end/cmd/web/templates/base.layout.gohtml",
"./front-end/cmd/web/templates/header.partial.gohtml",
"./front-end/cmd/web/templates/footer.partial.gohtml",
}
var templateSlice []string
templateSlice = append(templateSlice, fmt.Sprintf("./front-end/cmd/web/templates/%s", t))
for _, x := range partials {
templateSlice = append(templateSlice, x)
}
tmpl, err := template.ParseFiles(templateSlice...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}