plugins: Add reverse proxy pluginsHandler for serving plugins

This commit is contained in:
Chaitanya Bankanhal 2020-07-28 00:02:45 +05:30 committed by Nick Craig-Wood
parent f9ee0dc3f2
commit 22674d1146

View File

@ -61,12 +61,14 @@ func Start(opt *rc.Options) (*Server, error) {
// Server contains everything to run the rc server
type Server struct {
*httplib.Server
files http.Handler
opt *rc.Options
files http.Handler
pluginsHandler http.Handler
opt *rc.Options
}
func newServer(opt *rc.Options, mux *http.ServeMux) *Server {
fileHandler := http.Handler(nil)
pluginsHandler := http.Handler(nil)
// Add some more mime types which are often missing
_ = mime.AddExtensionType(".wasm", "application/wasm")
_ = mime.AddExtensionType(".js", "application/javascript")
@ -104,12 +106,15 @@ func newServer(opt *rc.Options, mux *http.ServeMux) *Server {
fs.Logf(nil, "Serving Web GUI")
fileHandler = http.FileServer(http.Dir(extractPath))
pluginsHandler = http.FileServer(http.Dir(webgui.PluginsPath))
}
s := &Server{
Server: httplib.NewServer(mux, &opt.HTTPOptions),
opt: opt,
files: fileHandler,
Server: httplib.NewServer(mux, &opt.HTTPOptions),
opt: opt,
files: fileHandler,
pluginsHandler: pluginsHandler,
}
mux.HandleFunc("/", s.handler)
@ -366,11 +371,13 @@ var fsMatch = regexp.MustCompile(`^\[(.*?)\](.*)$`)
func (s *Server) handleGet(w http.ResponseWriter, r *http.Request, path string) {
// Look to see if this has an fs in the path
match := fsMatch.FindStringSubmatch(path)
fsMatchResult := fsMatch.FindStringSubmatch(path)
pluginsMatchResult := webgui.PluginsMatch.FindStringSubmatch(path)
switch {
case match != nil && s.opt.Serve:
case fsMatchResult != nil && s.opt.Serve:
// Serve /[fs]/remote files
s.serveRemote(w, r, match[2], match[1])
s.serveRemote(w, r, fsMatchResult[2], fsMatchResult[1])
return
case path == "metrics" && s.opt.EnableMetrics:
promHandler.ServeHTTP(w, r)
@ -380,6 +387,15 @@ func (s *Server) handleGet(w http.ResponseWriter, r *http.Request, path string)
s.serveRoot(w, r)
return
case s.files != nil:
if s.opt.WebUI && pluginsMatchResult != nil {
ok := webgui.ServePluginOK(w, r, pluginsMatchResult)
if !ok {
r.URL.Path = fmt.Sprintf("/%s/%s/app/build/%s", pluginsMatchResult[1], pluginsMatchResult[2], pluginsMatchResult[3])
s.pluginsHandler.ServeHTTP(w, r)
return
}
return
}
// Serve the files
r.URL.Path = "/" + path
s.files.ServeHTTP(w, r)