// Copyright 2020, Chef. All rights reserved. // https://github.com/q191201771/lal // // Use of this source code is governed by a MIT-style license // that can be found in the License file. // // Author: Chef (191201771@qq.com) package logic import ( "encoding/json" "net" "net/http" "time" "github.com/q191201771/naza/pkg/nazahttp" "github.com/q191201771/lal/pkg/base" "github.com/q191201771/naza/pkg/bininfo" "github.com/q191201771/naza/pkg/nazalog" ) var serverStartTime string type HTTPAPIServerObserver interface { OnStatAllGroup() []base.StatGroup OnStatGroup(streamName string) *base.StatGroup OnCtrlStartPull(info base.APICtrlStartPullReq) OnCtrlKickOutSession(info base.APICtrlKickOutSession) base.HTTPResponseBasic } type HTTPAPIServer struct { addr string observer HTTPAPIServerObserver ln net.Listener } func NewHTTPAPIServer(addr string, observer HTTPAPIServerObserver) *HTTPAPIServer { return &HTTPAPIServer{ addr: addr, observer: observer, } } func (h *HTTPAPIServer) Listen() (err error) { if h.ln, err = net.Listen("tcp", h.addr); err != nil { return } nazalog.Infof("start httpapi server listen. addr=%s", h.addr) return } func (h *HTTPAPIServer) Runloop() error { mux := http.NewServeMux() mux.HandleFunc("/api/list", h.apiListHandler) mux.HandleFunc("/api/stat/lal_info", h.statLALInfoHandler) mux.HandleFunc("/api/stat/group", h.statGroupHandler) mux.HandleFunc("/api/stat/all_group", h.statAllGroupHandler) mux.HandleFunc("/api/ctrl/start_pull", h.ctrlStartPullHandler) mux.HandleFunc("/api/ctrl/kick_out_session", h.ctrlKickOutSessionHandler) var srv http.Server srv.Handler = mux return srv.Serve(h.ln) } // TODO chef: dispose func (h *HTTPAPIServer) apiListHandler(w http.ResponseWriter, req *http.Request) { // TODO chef: 写完api list页面 b := []byte(` lal http api list

api接口列表:


其他链接:

`) w.Header().Add("Server", base.LALHTTPAPIServer) _, _ = w.Write(b) } func (h *HTTPAPIServer) statLALInfoHandler(w http.ResponseWriter, req *http.Request) { var v base.APIStatLALInfo v.ErrorCode = base.ErrorCodeSucc v.Desp = base.DespSucc v.Data.BinInfo = bininfo.StringifySingleLine() v.Data.LalVersion = base.LALVersion v.Data.APIVersion = base.HTTPAPIVersion v.Data.NotifyVersion = base.HTTPNotifyVersion v.Data.StartTime = serverStartTime v.Data.ServerID = config.ServerID feedback(v, w) } func (h *HTTPAPIServer) statAllGroupHandler(w http.ResponseWriter, req *http.Request) { gs := h.observer.OnStatAllGroup() var v base.APIStatAllGroup v.ErrorCode = base.ErrorCodeSucc v.Desp = base.DespSucc v.Data.Groups = gs feedback(v, w) } func (h *HTTPAPIServer) statGroupHandler(w http.ResponseWriter, req *http.Request) { var v base.APIStatGroup q := req.URL.Query() streamName := q.Get("stream_name") if streamName == "" { v.ErrorCode = base.ErrorCodeParamMissing v.Desp = base.DespParamMissing feedback(v, w) return } v.Data = h.observer.OnStatGroup(streamName) if v.Data == nil { v.ErrorCode = base.ErrorCodeGroupNotFound v.Desp = base.DespGroupNotFound feedback(v, w) return } v.ErrorCode = base.ErrorCodeSucc v.Desp = base.DespSucc feedback(v, w) return } func (h *HTTPAPIServer) ctrlStartPullHandler(w http.ResponseWriter, req *http.Request) { var v base.HTTPResponseBasic var info base.APICtrlStartPullReq err := nazahttp.UnmarshalRequestJsonBody(req, &info, "protocol", "addr", "app_name", "stream_name") if err != nil { nazalog.Warnf("http api start pull error. err=%+v", err) v.ErrorCode = base.ErrorCodeParamMissing v.Desp = base.DespParamMissing feedback(v, w) return } nazalog.Infof("http api start pull. req info=%+v", info) h.observer.OnCtrlStartPull(info) v.ErrorCode = base.ErrorCodeSucc v.Desp = base.DespSucc feedback(v, w) return } func (h *HTTPAPIServer) ctrlKickOutSessionHandler(w http.ResponseWriter, req *http.Request) { var v base.HTTPResponseBasic var info base.APICtrlKickOutSession err := nazahttp.UnmarshalRequestJsonBody(req, &info, "stream_name", "session_id") if err != nil { nazalog.Warnf("http api kick out session error. err=%+v", err) v.ErrorCode = base.ErrorCodeParamMissing v.Desp = base.DespParamMissing feedback(v, w) return } nazalog.Infof("http api kick out session. req info=%+v", info) resp := h.observer.OnCtrlKickOutSession(info) feedback(resp, w) return } func feedback(v interface{}, w http.ResponseWriter) { resp, _ := json.Marshal(v) w.Header().Add("Server", base.LALHTTPAPIServer) _, _ = w.Write(resp) } func init() { serverStartTime = time.Now().Format("2006-01-02 15:04:05.999") }