代码拉取完成,页面将自动刷新
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()
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。