Repository: uber-go/zap
File: http_handler.go (Go)

Code before:
// look like:
//
//   {"level":"info"}
//
// An example curl request could look like this:
//
//    curl -X PUT localhost:8080/log/level -H "Content-Type: application/json" -d '{"level":"debug"}'
//
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:
		current := lvl.Level()
		enc.Encode(payload{Level: &current})

	case http.MethodPut:
		requestedLvl, err := func(body io.Reader) (*zapcore.Level, error) {
			switch r.Header.Get("Content-Type") {
			case "application/x-www-form-urlencoded":
				pld, err := ioutil.ReadAll(body)
				if err != nil {
					return nil, err
				}
				values, err := url.ParseQuery(string(pld))
				if err != nil {
					return nil, err
				}
				lvl := values.Get("level")
				if lvl == "" {
					return nil, fmt.Errorf("must specify logging level")
				}
				var l zapcore.Level
				if err := l.UnmarshalText([]byte(lvl)); err != nil {
					return nil, err
				}
				return &l, nil
			default:
				var pld payload
				if err := json.NewDecoder(r.Body).Decode(&pld); err != nil {
					return nil, fmt.Errorf("malformed request body: %v", err)
				}
				if pld.Level == nil {

Review comment (refactor):
This appears to be an effort to reduce error handling duplication. That's a good call but can we perhaps move this to its own unexported method?

```
requestedLevel, err := lvl.decodePutRequest(r)
```

Additionally, since zapcore.Level is just an int8, we don't need to return its address:

```
func (lvl AtomicLevel) decodePutRequest(*http.Request) (zapcore.Level, error)
```

It's a pointer in `payload` to detect the absence of the level in the JSON payload. So we can validate it's non-nil on the JSON path and dereference it.

Code after:
//    curl -X PUT localhost:8080/log/level -d level=debug
//
// For any other content type, the payload is expected to be JSON encoded and
// look like:
//
//   {"level":"info"}
//
// An example curl request could look like this:
//
//    curl -X PUT localhost:8080/log/level -H "Content-Type: application/json" -d '{"level":"debug"}'
//
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:
		enc.Encode(payload{Level: lvl.Level()})
	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.",
		})
	}
}

// Decodes incoming PUT requests and returns the requested logging level.
func decodePutRequest(contentType string, r *http.Request) (zapcore.Level, error) {
	if contentType == "application/x-www-form-urlencoded" {
		return decodePutURL(r)
	}
	return decodePutJSON(r.Body)
}

func decodePutURL(r *http.Request) (zapcore.Level, error) {
	lvl := r.FormValue("level")