Skip to content

Commit

Permalink
refactor: Rework handlers (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonjek authored Nov 23, 2024
1 parent 2d95027 commit 50be581
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 128 deletions.
4 changes: 3 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/handlers"
)

func main() {
webServer := web.NewServer()
hs := handlers.NewHandler()
webServer := web.NewServer(hs)
webServer.Start()
}
1 change: 0 additions & 1 deletion internal/web/handlers.go

This file was deleted.

36 changes: 36 additions & 0 deletions internal/web/handlers/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handlers

import (
"fmt"
"net/http"

"github.com/a-h/templ"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/components"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/page"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view"
)

type Handlers struct{}

func NewHandler() *Handlers {
return &Handlers{}
}

func handleRenderError(err error) {
if err != nil {
fmt.Println("Render error: ", err)
}
}

// Set header and render error message
func sendErrorMsg(w http.ResponseWriter, r *http.Request, errorMsg string) {
w.WriteHeader(http.StatusBadRequest)
handleRenderError(components.ErrorMsg(errorMsg).Render(r.Context(), w))
}

func (h *Handlers) Page404(w http.ResponseWriter, r *http.Request) {
templ.Handler(
page.Index(view.NotFoundComponent()),
templ.WithStatus(http.StatusNotFound),
).ServeHTTP(w, r)
}
101 changes: 101 additions & 0 deletions internal/web/handlers/notes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package handlers

import (
"net/http"
"strconv"
"time"

"github.com/sonjek/go-templ-htmx-picocss-example/internal/notes"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/components"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/page"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view"
)

func (h *Handlers) Notes(w http.ResponseWriter, r *http.Request) {
handleRenderError(page.Index(view.NotesView(notes.GetLatestNotes())).Render(r.Context(), w))
}

func (h *Handlers) MoreNotes(w http.ResponseWriter, r *http.Request) {
noteID := -1
if p := r.URL.Query().Get("note"); p != "" {
if parsedNoteID, err := strconv.Atoi(p); err == nil {
noteID = parsedNoteID
}
}

if noteID == -1 {
sendErrorMsg(w, r, "Note is empty")
return
}

notesOnPage := notes.GetNextNotes(noteID)

time.Sleep(250 * time.Millisecond)
handleRenderError(components.NotesList(notesOnPage).Render(r.Context(), w))
}

func (h *Handlers) AddNoteModal(w http.ResponseWriter, r *http.Request) {
handleRenderError(components.ModalAddNote().Render(r.Context(), w))
}

func (h *Handlers) AddNote(w http.ResponseWriter, r *http.Request) {
if r.FormValue("title") == "" {
sendErrorMsg(w, r, "Title is empty")
return
}

if r.FormValue("body") == "" {
sendErrorMsg(w, r, "Body is empty")
return
}

note := notes.Add(notes.CreateNote{
Title: r.FormValue("title"),
Body: r.FormValue("body"),
})

time.Sleep(250 * time.Millisecond)

handleRenderError(components.NoteItem(note).Render(r.Context(), w))
}

func (h *Handlers) EditNoteModal(w http.ResponseWriter, r *http.Request) {
note, err := notes.GetNoteByID(r.PathValue("id"))
if err != nil {
sendErrorMsg(w, r, err.Error())
return
}

handleRenderError(components.ModalEditNote(note).Render(r.Context(), w))
}

func (h *Handlers) EditNote(w http.ResponseWriter, r *http.Request) {
if r.FormValue("title") == "" {
sendErrorMsg(w, r, "Title is empty")
return
}

if r.FormValue("body") == "" {
sendErrorMsg(w, r, "Body is empty")
return
}

note, err := notes.GetNoteByID(r.PathValue("id"))
if err != nil {
sendErrorMsg(w, r, err.Error())
return
}

note.Title = r.FormValue("title")
note.Body = r.FormValue("body")
notes.Update(note)

time.Sleep(250 * time.Millisecond)
handleRenderError(components.NoteItem(note).Render(r.Context(), w))
}

func (h *Handlers) DeleteNote(w http.ResponseWriter, r *http.Request) {
if err := notes.Delete(r.PathValue("id")); err != nil {
sendErrorMsg(w, r, err.Error())
}
}
142 changes: 16 additions & 126 deletions internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,127 +5,23 @@ import (
"fmt"
"io/fs"
"net/http"
"strconv"
"time"

"github.com/a-h/templ"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/notes"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/handlers"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/middleware"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/components"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/page"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view"
)

type Server struct {
mux *http.ServeMux
mux *http.ServeMux
handlers *handlers.Handlers
}

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

return &Server{
mux: mux,
}
}

func handleRenderError(err error) {
if err != nil {
fmt.Println("Render error: ", err)
}
}

// Set header and render error message
func sendErrorMsg(w http.ResponseWriter, r *http.Request, errorMsg string) {
w.WriteHeader(http.StatusBadRequest)
handleRenderError(components.ErrorMsg(errorMsg).Render(r.Context(), w))
}

func notesFunc(w http.ResponseWriter, r *http.Request) {
handleRenderError(page.Index(view.NotesView(notes.GetLatestNotes())).Render(r.Context(), w))
}

func moreNotesFunc(w http.ResponseWriter, r *http.Request) {
noteID := -1
if p := r.URL.Query().Get("note"); p != "" {
if parsedNoteID, err := strconv.Atoi(p); err == nil {
noteID = parsedNoteID
}
}

if noteID == -1 {
sendErrorMsg(w, r, "Note is empty")
return
}

notesOnPage := notes.GetNextNotes(noteID)

time.Sleep(250 * time.Millisecond)
handleRenderError(components.NotesList(notesOnPage).Render(r.Context(), w))
}

func addNoteModalFunc(w http.ResponseWriter, r *http.Request) {
handleRenderError(components.ModalAddNote().Render(r.Context(), w))
}

func addNoteFunc(w http.ResponseWriter, r *http.Request) {
if r.FormValue("title") == "" {
sendErrorMsg(w, r, "Title is empty")
return
}

if r.FormValue("body") == "" {
sendErrorMsg(w, r, "Body is empty")
return
}

note := notes.Add(notes.CreateNote{
Title: r.FormValue("title"),
Body: r.FormValue("body"),
})

time.Sleep(250 * time.Millisecond)

handleRenderError(components.NoteItem(note).Render(r.Context(), w))
}

func editNoteModalFunc(w http.ResponseWriter, r *http.Request) {
note, err := notes.GetNoteByID(r.PathValue("id"))
if err != nil {
sendErrorMsg(w, r, err.Error())
return
}

handleRenderError(components.ModalEditNote(note).Render(r.Context(), w))
}

func editNoteFunc(w http.ResponseWriter, r *http.Request) {
if r.FormValue("title") == "" {
sendErrorMsg(w, r, "Title is empty")
return
}

if r.FormValue("body") == "" {
sendErrorMsg(w, r, "Body is empty")
return
}

note, err := notes.GetNoteByID(r.PathValue("id"))
if err != nil {
sendErrorMsg(w, r, err.Error())
return
}

note.Title = r.FormValue("title")
note.Body = r.FormValue("body")
notes.Update(note)

time.Sleep(250 * time.Millisecond)
handleRenderError(components.NoteItem(note).Render(r.Context(), w))
}

func deleteNoteFunc(w http.ResponseWriter, r *http.Request) {
if err := notes.Delete(r.PathValue("id")); err != nil {
sendErrorMsg(w, r, err.Error())
mux: mux,
handlers: h,
}
}

Expand All @@ -138,26 +34,20 @@ func (ws Server) Start() {
// If not, redirect to the custom 404 page
_, pattern := ws.mux.Handler(r)
if r.URL.Path != pattern {
http.Redirect(w, r, "/404", http.StatusSeeOther)
return
ws.handlers.Page404(w, r)
}

// Serve the index handler
notesFunc(w, r)
// Redirect from site index to notes page
http.Redirect(w, r, "/notes", http.StatusSeeOther)
})

ws.mux.Handle("/404", templ.Handler(page.Index(view.NotFoundComponent()),
templ.WithStatus(http.StatusNotFound)))

ws.mux.HandleFunc("/notes", notesFunc)
ws.mux.HandleFunc("/notes/load-more", moreNotesFunc)

ws.mux.HandleFunc("/add", addNoteModalFunc)
ws.mux.HandleFunc("POST /notes", addNoteFunc)

ws.mux.HandleFunc("/edit/{id}", editNoteModalFunc)
ws.mux.HandleFunc("PUT /note/{id}", editNoteFunc)
ws.mux.HandleFunc("DELETE /note/{id}", deleteNoteFunc)
ws.mux.HandleFunc("/notes", ws.handlers.Notes)
ws.mux.HandleFunc("/notes/load-more", ws.handlers.MoreNotes)
ws.mux.HandleFunc("/add", ws.handlers.AddNoteModal)
ws.mux.HandleFunc("POST /notes", ws.handlers.AddNote)
ws.mux.HandleFunc("/edit/{id}", ws.handlers.EditNoteModal)
ws.mux.HandleFunc("PUT /note/{id}", ws.handlers.EditNote)
ws.mux.HandleFunc("DELETE /note/{id}", ws.handlers.DeleteNote)

// Use embed.FS to create a file system from the embedded files
ws.mux.Handle("/static/", http.FileServerFS(staticFiles))
Expand Down

0 comments on commit 50be581

Please sign in to comment.