0
0
mirror of https://github.com/rls-moe/nyx synced 2024-11-14 22:12:24 +00:00
nyx/http/middle/config.go
2017-03-12 20:37:53 +01:00

25 lines
556 B
Go

package middle
import (
"context"
"go.rls.moe/nyx/config"
"net/http"
)
func ConfigCtx(config *config.Config) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), configKey, config))
next.ServeHTTP(w, r)
})
}
}
func GetConfig(r *http.Request) *config.Config {
val := r.Context().Value(configKey)
if val == nil {
panic("Config Middleware not configured")
}
return val.(*config.Config)
}