ribice / gorsk

:fire: Idiomatic Golang Restful Starter Kit

Home Page:https://www.ribice.ba/gorsk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Design question: Your various application services..

coquizen opened this issue · comments

commented

... have methods that takes in echo.Context as one of its parameters:

func (u User) Create(c echo.Context, req gorsk.User) (gorsk.User, error) {

func (a Auth) Authenticate(c echo.Context, user, pass string) (gorsk.AuthToken, error) {

func (p Password) Change(c echo.Context, userID int, oldPass, newPass string) error {

I noticed the same trend in gorsk-gin:
here

among others..

Why do you have it as a parameter if it is never used within the methods? Is its inclusion a placeholder for edge use cases?

Thanks for publishing this code. I find it immensely useful to think about how to go about structuring API in Go.

You'll notice that in Go in general, context.Context is passed as first argument nearly everywhere.

This is used for cancellations, timeouts and passing values via context.WithValue / ctx.Value. An example is storing user information inside context within middleware, and then pulling it wherever need. Or storing an instance of logger.

Unfortunately echo uses a custom context for this (and other purposes), thus it was mostly unused.

These days I write my APIs using gorilla/mux all the time.

commented

So echo.Context doesn't implement context.Context interfaces I take it?
Thanks for posting this example.. Not only helped me understand DDD but finally I grokked the purpose and use of Go's interfaces..