#How to deal with optional params

29 messages · Page 1 of 1 (latest)

willow fable
#

*i assume you mean query params
why not just let the func that needs it to validate or use default value?

#

ex.

func handleBrand(s string) error {
  if s=="" {
    return errors.New("missing required query params `brand`")
  }
  // do something with `brand`
}

// somewhere in the handler
if err:=handleBrand(r.URL.Query().Get("brand"));err!=nil {
  // handle error
}
quick gulch
#

But I want them to be optional

#

So I don't need them

#

But I use them if sent

willow fable
#

let the funcion that "use them" decide what to do, to use it (if exists) or not.
r.URL.Query() is url.Values which is map[string][]string
RecievedParams := make(map[string]string) is not needed imo.

#

i think its just a pattern or good practice, validation is the responsibility of model/domain.
controller/handler should be only collecting data, binding request to parameter or struct then pass it to the domain/model.
domain determine, what to do or is it required? etc.

quick gulch
#

I see

#

So handler just gets parameters and sends them off

#

Also

#
func NewRouter() {
    r := chi.NewRouter()
    r.Use(middleware.RequestID)
    r.Use(middleware.RealIP)
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)

    r.Use(middleware.Timeout(60 * time.Second))
    r.Get("/", handler.HealthHandle)
    r.Post("/search", handler.SearchHandle)
    r.Mount("/admin", handler.AdminRouter())
    http.ListenAndServe(":3000", r)
}

func AdminRouter() http.Handler {
    r := chi.NewRouter()
    r.Use(AdminOnly)
    r.Get("/", AdminAccounts)
    r.Get("/accounts", AdminAccounts)
    return r
}

func AdminAccounts(w http.ResponseWriter, r *http.Request) {
    // Example router, for now ignore
    w.WriteHeader(http.StatusOK)
    fmt.Fprintf(w, "Admin Accounts")
}
func AdminOnly(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // Do stuff here
        // e.g.
        // if !isAdmin(r) {
        //     http.Error(w, http.StatusText(401), 401)
        //     return
        // }
        rand.Seed(time.Now().Unix())
        isAdmin := false
        Rand := rand.Intn(100)
        if Rand > 50 {
            isAdmin = true
        }
        if !isAdmin {
            http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
            return
        }

        next.ServeHTTP(w, r)
    })
}
#

This returns a 404 when authorized

#

( I know the auth system is stupid but I'm lazy , want to debug )

willow fable
#

what is the request looks like? the request path.

quick gulch
#

localhost:3000/admin/accounts

#

Is it a me issue

#

Am I being stupid

#

I've gotta be going to the wrong domain

#

@willow fable sirs

willow fable
#

yes ?

quick gulch
#

Am I going to the wrong path?

willow fable
#

it suppose to be correct. i'm not sure, why it return 404.

#

try recompile and restart the server.

quick gulch
#

Alright

#

Have done a few times

#

Very weird

#

Ah

#

Found issue

#

Very weird one