2017-03-12 19:37:53 +00:00
|
|
|
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) {
|
2017-03-13 15:04:00 +00:00
|
|
|
if !config.IsHostNameValid(r.Host) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 19:37:53 +00:00
|
|
|
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)
|
|
|
|
}
|