Code Monkey home page Code Monkey logo

Comments (2)

sarulabs avatar sarulabs commented on June 2, 2024

Hi,

di.Get(c.Request, "nsq") is a shortcut for di.C(c.Request).Get("nsq").

But in your case di.C(c.Request) fails because it can not retrieve the container from the given request. You have to inject the context in the request first, with a middleware for example.

The middleware for the go handlers is available here: https://github.com/sarulabs/di/blob/master/http.go#L23

You have to write something similar for gin. I'm not a gin user but I think something like this should work:

func DI() gin.HandlerFunc {
    return func(c *gin.Context) {
        ctn, err := app.SubContainer()
        if err != nil {
            // TODO: handle error
            return
        }
        defer func() {
            if err := ctn.Delete(); err != nil {
                // TODO: handle error
            }
        }()

        c.Set("di-container", ctn)

        c.Next()
    }
}

r := gin.New()
r.Use(DI())

Here I inject the container in the gin Context instead of the request. So the di.C function must be rewritten:

di.C = func(i interface{}) di.Container {
    if ctn, ok := i.(di.Container); ok {
        return ctn
    }

    c, ok := i.(c *gin.Context)
    if !ok {
        panic("could not get the container with C(): not a *gin.Context")
    }

    ctnI, ok := c.Get("di-container")
    if !ok {
        panic("could not get the container from the given *gin.Context")
    }

    ctn, ok := ctnI.(Container)
    if !ok {
        panic("could not cast the container from the given *gin.Context")
    }

    return ctn
}

With this, you should be able to write:

func IntegralByLogin(c *gin.Context) {
    fmt.Print(di.Get(c, "nsq").(*producer.Producer).Test())
}

If you have only one scope, the middleware may be too much for you and you can make the code work with just:

di.C = func(i interface{}) di.Container {
    return app
}

from di.

CyberLife-Markus avatar CyberLife-Markus commented on June 2, 2024

Thank you for your reply

from di.

Related Issues (15)

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.