37 Star 407 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
package logserver
import (
"fmt"
"net"
"net/http"
"os"
"github.com/sirupsen/logrus"
)
var (
DefaultSocketLocation = "/tmp/log.sock"
)
// Server structure is used to the store backend information
type Server struct {
SocketLocation string
Debug bool
}
// StartServerWithDefaults starts the server with default values
func StartServerWithDefaults() {
s := Server{
SocketLocation: DefaultSocketLocation,
}
s.Start()
}
// Start the server
func (s *Server) Start() {
os.Remove(s.SocketLocation)
go s.ListenAndServe()
}
// ListenAndServe is used to setup handlers and
// start listening on the specified location
func (s *Server) ListenAndServe() error {
logrus.Infof("Listening on %s", s.SocketLocation)
server := http.Server{}
http.HandleFunc("/v1/loglevel", s.loglevel)
socketListener, err := net.Listen("unix", s.SocketLocation)
if err != nil {
return err
}
return server.Serve(socketListener)
}
func (s *Server) loglevel(rw http.ResponseWriter, req *http.Request) {
// curl -X POST -d "level=debug" localhost:12345/v1/loglevel
logrus.Debugf("Received loglevel request")
if req.Method == http.MethodGet {
level := logrus.GetLevel().String()
rw.Write([]byte(fmt.Sprintf("%s\n", level)))
}
if req.Method == http.MethodPost {
if err := req.ParseForm(); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(fmt.Sprintf("Failed to parse form: %v\n", err)))
}
level, err := logrus.ParseLevel(req.Form.Get("level"))
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(fmt.Sprintf("Failed to parse loglevel: %v\n", err)))
} else {
logrus.SetLevel(level)
rw.Write([]byte("OK\n"))
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.8-rc6

搜索帮助