diff options
author | GrailFinder <wohilas@gmail.com> | 2024-03-19 07:39:31 +0300 |
---|---|---|
committer | GrailFinder <wohilas@gmail.com> | 2024-03-19 07:39:31 +0300 |
commit | 5f2eb541052f6f6f02d5bc4320f9c19e21c6f59f (patch) | |
tree | 985ae1ac1db4f19d132c16cdbf84e300edce6b6d /internal | |
parent | bfb7942343bc08a7424f144f9799a9b547d6f607 (diff) |
Feat: template use examples
Diffstat (limited to 'internal')
-rw-r--r-- | internal/handlers/main.go | 16 | ||||
-rw-r--r-- | internal/server/router.go | 7 |
2 files changed, 21 insertions, 2 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 3b551f1..74a66c7 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -1,10 +1,11 @@ package handlers import ( + "apjournal/config" + "html/template" "log/slog" "net/http" "os" - "apjournal/config" ) // Handlers structure @@ -34,3 +35,16 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { h.log.Info("got ping request") w.Write([]byte("pong")) } + +func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { + h.log.Info("got mainpage request") + tmpl := template.Must(template. + ParseFiles("components/index.html")) + hardcodedName := "Guest" + tmpl.Execute(w, hardcodedName) +} + +func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + h.log.Info("got postform request", "payload", r.PostForm) +} diff --git a/internal/server/router.go b/internal/server/router.go index ed33c19..9b03371 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -16,7 +16,12 @@ func (srv *server) ListenToRequests() { WriteTimeout: time.Second * 5, } - mux.HandleFunc("/ping", h.Ping) + fs := http.FileServer(http.Dir("assets/")) + mux.Handle("GET /assets/", http.StripPrefix("/assets/", fs)) + + mux.HandleFunc("GET /ping", h.Ping) + mux.HandleFunc("GET /", h.MainPage) + mux.HandleFunc("POST /", h.HandleForm) fmt.Printf("Listening on %v\n", server.Addr) server.ListenAndServe() |