udemy-go-microservices/front-end/cmd/web/main.go

44 lines
957 B
Go
Raw Normal View History

2024-08-27 19:24:24 +00: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")
})
2024-08-28 07:27:28 +00:00
fmt.Println("Starting front end service on port 4000")
err := http.ListenAndServe(":4000", nil)
2024-08-27 19:24:24 +00:00
if err != nil {
log.Panic(err)
}
}
func render(w http.ResponseWriter, t string) {
partials := []string{
"./cmd/web/templates/base.layout.gohtml",
"./cmd/web/templates/header.partial.gohtml",
"./cmd/web/templates/footer.partial.gohtml",
}
var templateSlice []string
templateSlice = append(templateSlice, fmt.Sprintf("./cmd/web/templates/%s", t))
2024-08-28 07:27:28 +00:00
templateSlice = append(templateSlice, partials...)
2024-08-27 19:24:24 +00:00
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)
}
}