From 5b5dca7e4b201e41cb0c7551b0723a5a74bf93f5 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Wed, 20 Nov 2024 19:51:35 +0200 Subject: [PATCH 1/3] refactor: Change structure for web.go --- cmd/app/main.go | 3 ++- internal/web/handlers.go | 1 + internal/web/web.go | 42 +++++++++++++++++++++++++--------------- 3 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 internal/web/handlers.go diff --git a/cmd/app/main.go b/cmd/app/main.go index 39c496e..ac4f9ce 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -5,5 +5,6 @@ import ( ) func main() { - web.Start() + webServer := web.NewWebServer() + webServer.Start() } diff --git a/internal/web/handlers.go b/internal/web/handlers.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/internal/web/handlers.go @@ -0,0 +1 @@ +package web diff --git a/internal/web/web.go b/internal/web/web.go index 65a14f3..342ad1e 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -15,6 +15,18 @@ import ( "github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view" ) +type WebServder struct { + mux *http.ServeMux +} + +func NewWebServer() *WebServder { + mux := http.NewServeMux() + + return &WebServder{ + mux: mux, + } +} + func handleRenderError(err error) { if err != nil { fmt.Println("Render error: ", err) @@ -119,13 +131,11 @@ func deleteNoteFunc(w http.ResponseWriter, r *http.Request) { //go:embed static/* var staticFiles embed.FS -func Start() { - mux := http.NewServeMux() - - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { +func (ws WebServder) Start() { + ws.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Check if the requested URL is one of the defined handlers // If not, redirect to the custom 404 page - _, pattern := mux.Handler(r) + _, pattern := ws.mux.Handler(r) if r.URL.Path != pattern { http.Redirect(w, r, "/404", http.StatusSeeOther) return @@ -135,27 +145,27 @@ func Start() { notesFunc(w, r) }) - mux.Handle("/404", templ.Handler(page.Index(view.NotFoundComponent()), + ws.mux.Handle("/404", templ.Handler(page.Index(view.NotFoundComponent()), templ.WithStatus(http.StatusNotFound))) - mux.HandleFunc("/notes", notesFunc) - mux.HandleFunc("/notes/load-more", moreNotesFunc) + ws.mux.HandleFunc("/notes", notesFunc) + ws.mux.HandleFunc("/notes/load-more", moreNotesFunc) - mux.HandleFunc("/add", addNoteModalFunc) - mux.HandleFunc("POST /notes", addNoteFunc) + ws.mux.HandleFunc("/add", addNoteModalFunc) + ws.mux.HandleFunc("POST /notes", addNoteFunc) - mux.HandleFunc("/edit/{id}", editNoteModalFunc) - mux.HandleFunc("PUT /note/{id}", editNoteFunc) - mux.HandleFunc("DELETE /note/{id}", deleteNoteFunc) + ws.mux.HandleFunc("/edit/{id}", editNoteModalFunc) + ws.mux.HandleFunc("PUT /note/{id}", editNoteFunc) + ws.mux.HandleFunc("DELETE /note/{id}", deleteNoteFunc) // Use embed.FS to create a file system from the embedded files - mux.Handle("/static/", http.FileServerFS(staticFiles)) + ws.mux.Handle("/static/", http.FileServerFS(staticFiles)) fileSystem, err := fs.Sub(staticFiles, "static") if err != nil { panic(err) } - mux.Handle("/favicon.ico", http.StripPrefix("/", http.FileServerFS(fileSystem))) + ws.mux.Handle("/favicon.ico", http.StripPrefix("/", http.FileServerFS(fileSystem))) fmt.Println("Starting web interface on port: 8089") @@ -167,7 +177,7 @@ func Start() { server := &http.Server{ Addr: ":8089", - Handler: middlewares(mux), + Handler: middlewares(ws.mux), ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 10 * time.Second, From f41cb2f0716f188f6502cd85bb03d5411c6534f8 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Wed, 20 Nov 2024 22:03:21 +0200 Subject: [PATCH 2/3] fix: Fix lint --- cmd/app/main.go | 2 +- internal/web/web.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index ac4f9ce..45cf651 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -5,6 +5,6 @@ import ( ) func main() { - webServer := web.NewWebServer() + webServer := web.NewServer() webServer.Start() } diff --git a/internal/web/web.go b/internal/web/web.go index 342ad1e..955227b 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -19,7 +19,7 @@ type WebServder struct { mux *http.ServeMux } -func NewWebServer() *WebServder { +func NewServer() *WebServder { mux := http.NewServeMux() return &WebServder{ From 5de8990e244503f56e3540a1d5868395826163b8 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Wed, 20 Nov 2024 22:11:55 +0200 Subject: [PATCH 3/3] fix: Fix lint --- internal/web/web.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/web/web.go b/internal/web/web.go index 955227b..07fca3f 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -15,14 +15,14 @@ import ( "github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view" ) -type WebServder struct { +type Server struct { mux *http.ServeMux } -func NewServer() *WebServder { +func NewServer() *Server { mux := http.NewServeMux() - return &WebServder{ + return &Server{ mux: mux, } } @@ -131,7 +131,7 @@ func deleteNoteFunc(w http.ResponseWriter, r *http.Request) { //go:embed static/* var staticFiles embed.FS -func (ws WebServder) Start() { +func (ws Server) Start() { ws.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Check if the requested URL is one of the defined handlers // If not, redirect to the custom 404 page