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

add migration docs M0004 for isolating http handlers #214

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
69 changes: 69 additions & 0 deletions migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,75 @@
This file contains a list of tasks that are either required or at least
strongly recommended to align projects using this SDK.

## M0004 2024-09-13 Isolate HTTP handlers

### Reasoning

Our previous approach to have a single `Server` struct and to put all logic there gets messy pretty fast. Worse than
that it is hard to refactor this to separate this into multiple structs or packages. Dependency injection will help us
to separate those things in the very beginning of a project without manual wiring of dependencies. One step towards this
to split up all HTTP handlers into separate files.

### Hints

* The handlers should be moved into the `pkg/app/handlers` package.
* The handler struct should have a `New...` constructor with all required dependencies as parameters.
* The handler struct should implement `interface { Register(chi.Router) }`, which gets called once to set up the routes.
This will later be used for dependency injection integration.

### Example

```go
package handlers

import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/rebuy-de/platform-inventory/pkg/bll/webutilext"
"github.com/rebuy-de/platform-inventory/pkg/dal/sqlc"
"github.com/rebuy-de/rebuy-go-sdk/v8/pkg/webutil"
)

type KubeEventHandler struct {
sqlc *sqlc.Queries
viewer *webutilext.JetViewer
}

func NewKubeEventHandler(
sqlc *sqlc.Queries,
viewer *webutilext.JetViewer,
) *KubeEventHandler {
return &KubeEventHandler{
sqlc: sqlc,
viewer: viewer,
}
}

func (h *KubeEventHandler) Register(router chi.Router) {
router.Get("/kube/events", webutilext.WrapView(h.list))
router.Get("/kube/events/table-fragment", webutilext.WrapView(h.listFragment))
}

func (h *KubeEventHandler) list(r *http.Request) webutil.Response {
events, err := h.sqlc.ListKubeEvents(r.Context())
if err != nil {
return webutilext.ViewError(http.StatusInternalServerError, err)
}

return h.viewer.HTML(http.StatusOK, "kube_event_list.html", events)
}

func (h *KubeEventHandler) listFragment(r *http.Request) webutil.Response {
events, err := h.sqlc.ListKubeEvents(r.Context())
if err != nil {
return webutilext.ViewError(http.StatusInternalServerError, err)
}

return h.viewer.HTML(http.StatusOK, "frames/kube_event_table.html", events)
}
```

## M0003 2024-08-16 Change viewer interfaces of webutil

### Reasoning
Expand Down
Loading