Pārlūkot izejas kodu

Simplify setupRoutes.

Frédéric G. MARAND 11 mēneši atpakaļ
vecāks
revīzija
275b131f6a
1 mainītis faili ar 3 papildinājumiem un 15 dzēšanām
  1. 3 15
      contactsapp/app.go

+ 3 - 15
contactsapp/app.go

@@ -68,10 +68,7 @@ func MakeRouteContacts() http.HandlerFunc {
 		if search != "" {
 			contactsSet = (&Contact{}).Search(search)
 			if r.Header.Get("HX-Trigger") == "search" {
-				MakeTemplate(
-					"layout",
-					"rows",
-				).Execute(w, anyMap{"contacts": contactsSet})
+				tpl.Execute(w, anyMap{"contacts": contactsSet})
 				return
 			}
 		} else {
@@ -473,7 +470,7 @@ func flash(message string) {
 
 }
 
-func setupRoutes(mux *http.ServeMux, templates *template.Template) {
+func setupRoutes(mux *http.ServeMux) {
 	// OK
 	mux.Handle("/", RouteFront)
 	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
@@ -500,16 +497,7 @@ func setupRoutes(mux *http.ServeMux, templates *template.Template) {
 
 func main() {
 	mux := http.NewServeMux()
-	templates := template.Must(
-		template.
-			New("htmx-app").
-			Funcs(sprig.FuncMap()). // Arithmetic, etc.
-			Funcs(template.FuncMap{
-				"get_flashed_messages": func() string { return "some message" },
-			}).
-			ParseGlob("./templates/*.gohtml"),
-	)
-	setupRoutes(mux, templates)
+	setupRoutes(mux)
 	(&Contact{}).LoadDB()
 	if err := http.ListenAndServe(":8080", mux); err != nil {
 		if !errors.Is(err, http.ErrServerClosed) {