Code Monkey home page Code Monkey logo

Comments (4)

mahmed2000 avatar mahmed2000 commented on May 20, 2024

There's the easy way of just calling chi.URLParam to get the path parameter directly from the the request object.

Making a new middleware like pagination won't work with SendSystemMessageToConnectedClient. That already has an auth middleware that changes the function signature. It can't be chained cleanly with another one that does the same.

There's a better way to just use the Context. Store values there so it persists across an arbitrary number of middlewares. No need to introduce changed function signatures.

from owncast.

gabek avatar gabek commented on May 20, 2024

Sounds like the pagination is a different topic. I haven't looked into that. I'll file a new issue.

from owncast.

mahmed2000 avatar mahmed2000 commented on May 20, 2024

Bit of a misunderstanding. To clarify, its more to do with design than anything being broken or non-functional.


Pagination works but the middleware directly queries the params from the request object instead of taking the arguments set in the handler, for example:

func (*ServerInterfaceImpl) GetFollowers(w http.ResponseWriter, r *http.Request, params generated.GetFollowersParams) {

The params argument is set by the new generated router and contains both the limit and offset args. It is ignored, which seems incorrect to me. Currently the pagination middleware just takes it from a standard r.Query().Get() call. The equivalent for the path params would just be the chi.URLParam() I mentioned. With this the userId and clientId params would go ignored:

func (*ServerInterfaceImpl) SendSystemMessageToConnectedClient(w http.ResponseWriter, r *http.Request, clientId int) {

The generated router already surfaces this once, seems wasteful to do it again, but wouldn't exactly break anything.


The problem is more the incompatibility of chaining stuff like:

type PaginatedHandlerFunc func(int, int, http.ResponseWriter, *http.Request)

with this:

type ExternalAccessTokenHandlerFunc func(user.ExternalAPIUser, http.ResponseWriter, *http.Request)

They work as their own ends of a chain, but if they need to be chained (like a hypothetical path parameter handler that is wrapped around the externalAPIUser auth middleware), it won't be clean. One would have to know about the other since the controller function would need to surface the other's params to be called.

They can't wrap each other since neither can accept a standard HandlerFunc with only the writer and request, but do return that.

They're more "end"-wares than middle, is my problem.

RequireExternalAPIAccessToken(scope, HandlePagination(dummy_func))(w, r)

won't work since the Access expects a ExternalAccessTokenHandlerFunc instead of a HandlerFunc

or

HandlePagination(ScopedRequireExternalAPIAccessToken(dummy_func))(w, r)

which also won't work since the pagination expects a PaginatedHandlerFunc instead of a HandlerFunc


We could ignore it for this case, but what happens if there's ever an endpoint that needs both pagination and and external API access?

The controller would look like:
func (int, int, user.ExternalAPIUser, responseWriter, Request)

Using contexts, we can simply use that to pass values around instead, and middlewares don't need to create these custom types to deal with specific arguments and can all just take and return just the writer and request objects.

from owncast.

gabek avatar gabek commented on May 20, 2024

I see what you're saying. We could remove the pagination middleware, as it is there primarily for convenience, and just export a similar version as a utility function that can be used in handlers that require pagination. The result would be the same, the logic for pulling out those values would just be done in a different place.

from owncast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.