Skip to content

Accessing gin Context from api2go resource handler #317

@dataBaseError

Description

@dataBaseError

Hello,

Is there anyway to copy the context from a routing framework? Specifically the gin framework has Context which you can get or set Keys. While api2go supports Context, context set in Gin does not transfer to api2go.

Is there existing way to support the context store in gin to be transferred to the api2go's context?

An example of this would be using gin middleware login which sets a Key userID on login.

main.go

// ...
r := gin.Default()
api := api2go.NewAPIWithRouting(
  "api",
  api2go.NewStaticResolver("/"),
  routing.Gin(r),
)

// If login is successful will set `userID`.
r.Use(login_handler.AuthMiddleware.MiddlewareFunc())

r.Get("/test", func(c *gin.Context) {
	userId, ok := c.Get("userID")
	// Value will be present
	fmt.Println("User ", userId, " ok ", ok)
}

// ...
api.AddResource(model.User{}, userResource)
// ...

user_resource.go

func (s *UserResource) FindAll(r api2go.Request) (api2go.Responder, error) {
	v, ok := r.Context.Get("userID")
	// Value will not be present
	fmt.Println("Account ID ", v, " ok ", ok)

	// ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions