refactor: seperate concerns via handler, service, storage #2
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
lucas/grugchat#2
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
ideally, all business logic should be in a service (file chat.go) and use purely our defined go structs.
a handler would be responsible from translating incoming HTTP API request containing json into the correct Go function calls + structs.
the storage part would be responsible for translating our Go structs + functions into SQL queries.
refactor done - pushed as 739e1fe
what changed:
main.go- startup + wiring only: open db, register routes, run serverhandlers.go- handler layer: translates http/json <-> service callschat.go- service layer: business logic lives here. it's a thin pass-through right now, but the sse notification for postMessage (per pseudocode/backend.s) belongs here when it landsstorage.go- storage layer: all sql lives heretypes.go- the shared structs (Room, Message, NewMessage)the temporary global
dbis gone - the handle is passed as a plain function argument (same style as the sample in docs/dependency-explanations.md). requests now flow handler -> service -> storage. kept it as simple as possible: plain functions only, no interfaces, no generics.small fixes that came with the move:
http.ListenAndServeerror is no longer ignored (port already in use now logs a proper error instead of exiting silently - this bit me while setting the dev server up)500on db errors instead oflog.Fatal-ing the whole serverPOST /messagesresponds201with{"id": N}(the frontend ignores the response, so nothing breaks)AGENTS.md was updated to match the new layout.
tested:
go build+go vetclean, server restarted, verified GET /rooms, GET /messages, POST /messages (including 400 on invalid json and the Remote-User header). leaving this issue open for review.