Feature: add custom ui support in API

This commit is contained in:
Dreamacro
2018-12-20 01:29:13 +08:00
parent afc4644dd1
commit a6bbc67afb
3 changed files with 33 additions and 2 deletions

View File

@ -17,6 +17,8 @@ import (
var (
serverSecret = ""
serverAddr = ""
uiPath = ""
)
type Traffic struct {
@ -24,6 +26,10 @@ type Traffic struct {
Down int64 `json:"down"`
}
func SetUIPath(path string) {
uiPath = path
}
func Start(addr string, secret string) {
if serverAddr != "" {
return
@ -49,6 +55,14 @@ func Start(addr string, secret string) {
r.Mount("/proxies", proxyRouter())
r.Mount("/rules", ruleRouter())
if uiPath != "" {
fs := http.StripPrefix("/ui", http.FileServer(http.Dir(uiPath)))
r.Get("/ui", http.RedirectHandler("/ui/", 301).ServeHTTP)
r.Get("/ui/*", func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
})
}
log.Infoln("RESTful API listening at: %s", addr)
err := http.ListenAndServe(addr, r)
if err != nil {