Exemplo n.º 1
0
// InjectConfig 注入*config.Config
func InjectConfig(conf *config.Config) rest.Middleware {
	fn := func(h rest.HandlerFunc) rest.HandlerFunc {
		return func(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
			ctx = context.WithValue(ctx, &ctxConfigKey, conf)
			h(ctx, w, r)
		}
	}
	return rest.MiddlewareSimple(fn)
}
Exemplo n.º 2
0
// InjectLogger 注入logger.Logger
func InjectLogger(logger logger.Logger) rest.Middleware {
	fn := func(h rest.HandlerFunc) rest.HandlerFunc {
		return func(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
			ctx = context.WithValue(ctx, &ctxLoggerKey, logger)
			h(ctx, w, r)
		}
	}
	return rest.MiddlewareSimple(fn)
}
Exemplo n.º 3
0
// InjectRepo 注入model.Repo
func InjectRepo(repo model.Repo) rest.Middleware {
	fn := func(h rest.HandlerFunc) rest.HandlerFunc {
		return func(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
			ctx = context.WithValue(ctx, &ctxRepoKey, repo)
			h(ctx, w, r)
		}
	}
	return rest.MiddlewareSimple(fn)
}