代码拉取完成,页面将自动刷新
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package httpadmin
import (
"encoding/json"
"fmt"
"net/http"
"github.com/hyperledger/fabric/common/flogging"
)
//go:generate counterfeiter -o fakes/logging.go -fake-name Logging . Logging
type Logging interface {
ActivateSpec(spec string) error
Spec() string
}
type LogSpec struct {
Spec string `json:"spec,omitempty"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
func NewSpecHandler() *SpecHandler {
return &SpecHandler{
Logging: flogging.Global,
Logger: flogging.MustGetLogger("flogging.httpadmin"),
}
}
type SpecHandler struct {
Logging Logging
Logger *flogging.FabricLogger
}
func (h *SpecHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPut:
var logSpec LogSpec
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&logSpec); err != nil {
h.sendResponse(resp, http.StatusBadRequest, err)
return
}
req.Body.Close()
if err := h.Logging.ActivateSpec(logSpec.Spec); err != nil {
h.sendResponse(resp, http.StatusBadRequest, err)
return
}
resp.WriteHeader(http.StatusNoContent)
case http.MethodGet:
h.sendResponse(resp, http.StatusOK, &LogSpec{Spec: h.Logging.Spec()})
default:
err := fmt.Errorf("invalid request method: %s", req.Method)
h.sendResponse(resp, http.StatusBadRequest, err)
}
}
func (h *SpecHandler) sendResponse(resp http.ResponseWriter, code int, payload interface{}) {
encoder := json.NewEncoder(resp)
if err, ok := payload.(error); ok {
payload = &ErrorResponse{Error: err.Error()}
}
resp.WriteHeader(code)
resp.Header().Set("Content-Type", "application/json")
if err := encoder.Encode(payload); err != nil {
h.Logger.Errorw("failed to encode payload", "error", err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。