Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Change structure for web.go #22

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import (
)

func main() {
web.Start()
webServer := web.NewServer()
webServer.Start()
}
1 change: 1 addition & 0 deletions internal/web/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package web
42 changes: 26 additions & 16 deletions internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import (
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view"
)

type Server struct {
mux *http.ServeMux
}

func NewServer() *Server {
mux := http.NewServeMux()

return &Server{
mux: mux,
}
}

func handleRenderError(err error) {
if err != nil {
fmt.Println("Render error: ", err)
Expand Down Expand Up @@ -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 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
_, pattern := mux.Handler(r)
_, pattern := ws.mux.Handler(r)
if r.URL.Path != pattern {
http.Redirect(w, r, "/404", http.StatusSeeOther)
return
Expand All @@ -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")

Expand All @@ -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,
Expand Down