-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http.MethodGet lost setLevel #1225
Comments
@lxb325710 Can you clarify? Get should not call setLevel. That should be a read-only operation. |
I agree with @abhinav's comment. The Get operation should not call SetLevel. The Get method should only retrieve the current atomic level and return it as the response payload. The SetLevel operation should only be triggered by a Put request, where the requested level is decoded and passed to the SetLevel function. In func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type errorResponse struct {
Error string `json:"error"`
}
type payload struct {
Level zapcore.Level `json:"level"`
}
enc := json.NewEncoder(w)
switch r.Method {
case http.MethodGet:
currentLvl := lvl.Level()
enc.Encode(payload{Level: currentLvl})
case http.MethodPut:
requestedLvl, err := decodePutRequest(r.Header.Get("Content-Type"), r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
enc.Encode(errorResponse{Error: err.Error()})
return
}
lvl.SetLevel(requestedLvl)
enc.Encode(payload{Level: lvl.Level()})
default:
w.WriteHeader(http.StatusMethodNotAllowed)
enc.Encode(errorResponse{
Error: "Only GET and PUT are supported.",
})
}
}
func decodePutRequest(contentType string, r *http.Request) (zapcore.Level, error) {
if contentType != "application/json" {
return zap.InfoLevel, fmt.Errorf("Content-Type must be application/json")
}
var payload struct {
Level string `json:"level"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
return zap.InfoLevel, err
}
level, err := zapcore.ParseLevel(payload.Level)
if err != nil {
return zap.InfoLevel, err
}
return level, nil
} |
func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type errorResponse struct {
Error string
json:"error"
}
type payload struct {
Level zapcore.Level
json:"level"
}
}
The text was updated successfully, but these errors were encountered: