2 Star 1 Fork 0

jretty-org/buddhad

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
logtail.go 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/ochinchina/supervisord/logger"
)
// Logtail tails the process log through http interface
type Logtail struct {
router *mux.Router
supervisor *Supervisor
}
// NewLogtail creates a Logtail object
func NewLogtail(supervisor *Supervisor) *Logtail {
return &Logtail{router: mux.NewRouter(), supervisor: supervisor}
}
// CreateHandler creates http handlers to process the program stdout and stderr through http interface
func (lt *Logtail) CreateHandler() http.Handler {
lt.router.HandleFunc("/logtail/{program}/stdout", lt.getStdoutLog).Methods("GET")
lt.router.HandleFunc("/logtail/{program}/stderr", lt.getStderrLog).Methods("GET")
return lt.router
}
func (lt *Logtail) getStdoutLog(w http.ResponseWriter, req *http.Request) {
lt.getLog("stdout", w, req)
}
func (lt *Logtail) getStderrLog(w http.ResponseWriter, req *http.Request) {
lt.getLog("stderr", w, req)
}
func (lt *Logtail) getLog(logType string, w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
program := vars["program"]
procMgr := lt.supervisor.GetManager()
proc := procMgr.Find(program)
if proc == nil {
w.WriteHeader(http.StatusBadRequest)
} else {
var ok bool = false
var compositeLogger *logger.CompositeLogger = nil
if logType == "stdout" {
compositeLogger, ok = proc.StdoutLog.(*logger.CompositeLogger)
} else {
compositeLogger, ok = proc.StderrLog.(*logger.CompositeLogger)
}
if ok {
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
flusher, _ := w.(http.Flusher)
ch := make(chan []byte, 100)
chanLogger := logger.NewChanLogger(ch)
compositeLogger.AddLogger(chanLogger)
for {
text, ok := <-ch
if !ok {
break
}
_, err := w.Write(text)
if err != nil {
break
}
flusher.Flush()
}
compositeLogger.RemoveLogger(chanLogger)
_ = chanLogger.Close()
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jretty-org/buddhad.git
[email protected]:jretty-org/buddhad.git
jretty-org
buddhad
buddhad
master

搜索帮助