- [refactor] using Go embed to load lal.html - [refactor] using path /lal.html instead of path / to load web ui

pull/263/head
q191201771 2 years ago
parent 3302008b74
commit feb6ed992f

@ -18,7 +18,7 @@ GitCommitLog=${GitCommitLog//\'/\"}
GitStatus=`git status -s`
BuildTime=`date +'%Y.%m.%d.%H%M%S'`
BuildGoVersion=`go version`
WebUITpl=`cat lal.html`
#WebUITpl=`cat lal.html`
LDFlags=" \
-X 'github.com/q191201771/naza/pkg/bininfo.GitTag=${GitTag}' \
@ -26,9 +26,10 @@ LDFlags=" \
-X 'github.com/q191201771/naza/pkg/bininfo.GitStatus=${GitStatus}' \
-X 'github.com/q191201771/naza/pkg/bininfo.BuildTime=${BuildTime}' \
-X 'github.com/q191201771/naza/pkg/bininfo.BuildGoVersion=${BuildGoVersion}' \
-X 'github.com/q191201771/lal/pkg/logic.webUITpl=${WebUITpl}' \
"
#-X 'github.com/q191201771/lal/pkg/logic.webUITpl=${WebUITpl}' \
echo "build" ${ROOT_DIR}/app/lalserver "..."
cd ${ROOT_DIR}/app/lalserver && go build -ldflags "$LDFlags" -o ${ROOT_DIR}/${OUT_DIR}/lalserver
#cd ${ROOT_DIR}/app/lalserver && go build -race -ldflags "$LDFlags" -o ${ROOT_DIR}/${OUT_DIR}/lalserver.debug

@ -1,5 +1,5 @@
module github.com/q191201771/lal
go 1.14
go 1.16
require github.com/q191201771/naza v0.30.8

@ -52,6 +52,9 @@ const (
ErrorCodeSucc = 0
DespSucc = "succ"
ErrorCodePageNotFound = 404
DespPageNotFound = "page not found, check this document out: https://pengrl.com/lal/#/HTTPAPI"
ErrorCodeGroupNotFound = 1001
DespGroupNotFound = "group not found"
ErrorCodeParamMissing = 1002
@ -63,12 +66,16 @@ const (
ErrorCodeListenUdpPortFail = 2002
)
// HttpResponseBasic
type ApiRespBasic struct {
ErrorCode int `json:"error_code"`
Desp string `json:"desp"`
}
var ApiNotFoundResp = ApiRespBasic{
ErrorCode: ErrorCodePageNotFound,
Desp: DespPageNotFound,
}
type ApiStatLalInfoResp struct {
ApiRespBasic
Data LalInfo `json:"data"`

@ -250,7 +250,7 @@
</section>
<footer>
<hr>lal
<hr><a href="https://github.com/q191201771/lal">lal github</a> | <a href="https://pengrl.com/lal">document</a>
</footer>
<script>

@ -9,6 +9,7 @@
package logic
import (
_ "embed"
"encoding/json"
"html/template"
"io/ioutil"
@ -23,6 +24,9 @@ import (
"github.com/q191201771/lal/pkg/base"
)
//go:embed http_an__lal.html
var webUITpl string
type HttpApiServer struct {
addr string
sm *ServerManager
@ -30,8 +34,6 @@ type HttpApiServer struct {
ln net.Listener
}
var webUITpl string
func NewHttpApiServer(addr string, sm *ServerManager) *HttpApiServer {
return &HttpApiServer{
addr: addr,
@ -50,6 +52,8 @@ func (h *HttpApiServer) Listen() (err error) {
func (h *HttpApiServer) RunLoop() error {
mux := http.NewServeMux()
mux.HandleFunc("/lal.html", h.webUIHandler)
mux.HandleFunc("/api/stat/group", h.statGroupHandler)
mux.HandleFunc("/api/stat/all_group", h.statAllGroupHandler)
mux.HandleFunc("/api/stat/lal_info", h.statLalInfoHandler)
@ -58,7 +62,8 @@ func (h *HttpApiServer) RunLoop() error {
mux.HandleFunc("/api/ctrl/stop_relay_pull", h.ctrlStopRelayPullHandler)
mux.HandleFunc("/api/ctrl/kick_session", h.ctrlKickSessionHandler)
mux.HandleFunc("/api/ctrl/start_rtp_pub", h.ctrlStartRtpPubHandler)
mux.HandleFunc("/", h.webUIHandler)
// 所有没有注册路由的走下面这个处理函数
mux.HandleFunc("/", h.notFoundHandler)
var srv http.Server
srv.Handler = mux
@ -108,7 +113,6 @@ func (h *HttpApiServer) statGroupHandler(w http.ResponseWriter, req *http.Reques
v.ErrorCode = base.ErrorCodeSucc
v.Desp = base.DespSucc
feedback(v, w)
return
}
// ---------------------------------------------------------------------------------------------------------------------
@ -143,7 +147,6 @@ func (h *HttpApiServer) ctrlStartRelayPullHandler(w http.ResponseWriter, req *ht
resp := h.sm.CtrlStartRelayPull(info)
feedback(resp, w)
return
}
func (h *HttpApiServer) ctrlStopRelayPullHandler(w http.ResponseWriter, req *http.Request) {
@ -162,7 +165,6 @@ func (h *HttpApiServer) ctrlStopRelayPullHandler(w http.ResponseWriter, req *htt
resp := h.sm.CtrlStopRelayPull(streamName)
feedback(resp, w)
return
}
func (h *HttpApiServer) ctrlKickSessionHandler(w http.ResponseWriter, req *http.Request) {
@ -182,7 +184,6 @@ func (h *HttpApiServer) ctrlKickSessionHandler(w http.ResponseWriter, req *http.
resp := h.sm.CtrlKickSession(info)
feedback(resp, w)
return
}
func (h *HttpApiServer) ctrlStartRtpPubHandler(w http.ResponseWriter, req *http.Request) {
@ -213,17 +214,9 @@ func (h *HttpApiServer) ctrlStartRtpPubHandler(w http.ResponseWriter, req *http.
resp := h.sm.CtrlStartRtpPub(info)
feedback(resp, w)
return
}
// ---------------------------------------------------------------------------------------------------------------------
func (h *HttpApiServer) webUIHandler(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
h.notFoundHandler(w, req)
return
}
t, err := template.New("webUI").Parse(webUITpl)
if err != nil {
Log.Errorf("invaild html template: %v", err)
@ -250,8 +243,12 @@ func (h *HttpApiServer) webUIHandler(w http.ResponseWriter, req *http.Request) {
func (h *HttpApiServer) notFoundHandler(w http.ResponseWriter, req *http.Request) {
Log.Warnf("invalid http-api request. uri=%s, raddr=%s", req.RequestURI, req.RemoteAddr)
//w.WriteHeader(http.StatusNotFound)
feedback(base.ApiNotFoundResp, w)
}
// ---------------------------------------------------------------------------------------------------------------------
func feedback(v interface{}, w http.ResponseWriter) {
resp, _ := json.Marshal(v)
w.Header().Add("Server", base.LalHttpApiServer)

Loading…
Cancel
Save