podhmo / apikit

api toolkit (WIP)
MIT License
0 stars 0 forks source link

static provider (lookup component with setup phase (not in runtime)) #202

Open podhmo opened 2 years ago

podhmo commented 2 years ago

take the component as arguments, instead of something this.

// provide component in runtime

func Hello(getProvider func(*http.Request) (*http.Request, Provider, error)) func(http.ResponseWriter, *http.Request) {
    return func(w http.ResponseWriter, req *http.Request) {
        req, provider, err := getProvider(req)
        if err != nil {
            runtime.HandleResult(w, req, nil, err)
            return
        }
        var ctx context.Context = req.Context()
        var logger *log.Logger
        {
            logger = provider.Logger()
        }
        result, err := action.Hello(ctx, logger)
        runtime.HandleResult(w, req, result, err)
    }
}

only this

// provide component in setup phase

func Hello(logger *log.Logger) func(http.ResponseWriter, *http.Request) {
    return func(w http.ResponseWriter, req *http.Request) {
        ctx := req.Context()
        result, err := action.Hello(ctx, logger)
        runtime.HandleResult(w, req, result, err)
    }
}
podhmo commented 2 years ago

marked by tracker?

tracker.AsStatic(func(logger *log.Logger) {})
// or
tracker.AsStatic(&Logger{})
podhmo commented 2 years ago

💭 not a good name, all of this issue.