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
1 changed files with 24 additions and 8 deletions

View File

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