dashboard: add the ability to use a remote dashboard, serve only local files: assets and eventualy public.json (to override given ones)

This commit is contained in:
nemunaire 2019-01-21 11:36:52 +01:00
parent 8c754fe265
commit b205409679
3 changed files with 154 additions and 20 deletions

50
dashboard/fwd.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"io"
"net/http"
"net/url"
"os"
"path"
)
func fwd_request(w http.ResponseWriter, r *http.Request, fwd string) {
if u, err := url.Parse(fwd); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
var user, pass string
if u.User != nil {
user = u.User.Username()
pass, _ = u.User.Password()
u.User = nil
}
if v, exists := os.LookupEnv("FICCLOUD_USER"); exists {
user = v
} else if v, exists := os.LookupEnv("FICCLOUD_PASS"); exists {
pass = v
}
u.Path = path.Join(u.Path, r.URL.Path)
if r, err := http.NewRequest(r.Method, u.String(), r.Body); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
if len(user) != 0 || len(pass) != 0 {
r.SetBasicAuth(user, pass)
}
if resp, err := http.DefaultClient.Do(r); err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
} else {
defer resp.Body.Close()
for key := range resp.Header {
w.Header().Add(key, resp.Header.Get(key))
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
}
}
}

View File

@ -72,6 +72,8 @@ func main() {
flag.StringVar(&DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where load and save settings")
var fwdr = flag.String("forwarder", "", "URL of another dashboard where send traffic to, except static assets")
flag.BoolVar(&fwdPublicJson, "fwdpublicjson", fwdPublicJson, "Also forward public.json files to forwarder")
flag.Parse()
log.SetPrefix("[public] ")
@ -95,6 +97,9 @@ func main() {
tmp := ""
baseURL = &tmp
}
if fwdr != nil && len(*fwdr) > 0 {
forwarder = fwdr
}
// Prepare graceful shutdown
interrupt := make(chan os.Signal, 1)

View File

@ -14,6 +14,9 @@ import (
"github.com/julienschmidt/httprouter"
)
var forwarder *string = nil
var fwdPublicJson = false
func init() {
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
@ -36,80 +39,156 @@ func init() {
})
api.Router().GET("/files/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(fic.FilesDir, strings.TrimPrefix(r.URL.Path, "/files")))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(fic.FilesDir, strings.TrimPrefix(r.URL.Path, "/files")))
}
})
api.Router().GET("/events.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(TeamsDir, "events.json"))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "events.json"))
}
})
api.Router().GET("/my.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(TeamsDir, "public", "my.json"))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "public", "my.json"))
}
})
api.Router().GET("/stats.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(TeamsDir, "public", "stats.json"))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "public", "stats.json"))
}
})
api.Router().GET("/api/teams/:tid/stats.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.Redirect(w, r, "http://127.0.0.1:8081/api/teams/" + string(ps.ByName("tid")) + "/stats.json", 302)
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
fwd_request(w, r, "http://127.0.0.1:8081/")
}
})
api.Router().GET("/settings.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.SettingsFile))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.SettingsFile))
}
})
api.Router().GET("/teams.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(TeamsDir, "teams.json"))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "teams.json"))
}
})
api.Router().GET("/themes.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(TeamsDir, "themes.json"))
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "themes.json"))
}
})
api.Router().GET("/public.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public.json"))
}
})
api.Router().GET("/public0.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public0.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public0.json"))
}
})
api.Router().GET("/public1.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public1.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public1.json"))
}
})
api.Router().GET("/public2.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public2.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public2.json"))
}
})
api.Router().GET("/public3.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public3.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public3.json"))
}
})
api.Router().GET("/public4.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public4.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public4.json"))
}
})
api.Router().GET("/public5.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public5.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public5.json"))
}
})
api.Router().GET("/public6.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public6.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public6.json"))
}
})
api.Router().GET("/public7.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public7.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public7.json"))
}
})
api.Router().GET("/public8.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public8.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public8.json"))
}
})
api.Router().GET("/public9.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path.Join(DashboardDir, "public9.json"))
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public9.json"))
}
})
}