From 00bd0a8af4f1365d114484ee2ed1c47417c8d450 Mon Sep 17 00:00:00 2001 From: Tyler Compton Date: Fri, 16 Aug 2019 11:02:00 -0700 Subject: [PATCH] Load assets for dashboard/admin panel on demand The client and server services now only attempt to load assets if the dashboard or admin panel are enabled. This change makes it possible to use FRP as a library without having to manage assets. If a library user wants to start a server with the dashboard enabled, they will need to set the DashboardPort and AssetsDir fields of ServerCommonConf. --- client/service.go | 15 +++++++-------- server/service.go | 14 +++++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/client/service.go b/client/service.go index f21be4f..f14edf9 100644 --- a/client/service.go +++ b/client/service.go @@ -52,13 +52,6 @@ type Service struct { } func NewService(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.VisitorConf) (svr *Service, err error) { - // Init assets - err = assets.Load("") - if err != nil { - err = fmt.Errorf("Load assets error: %v", err) - return - } - svr = &Service{ pxyCfgs: pxyCfgs, visitorCfgs: visitorCfgs, @@ -102,7 +95,13 @@ func (svr *Service) Run() error { go svr.keepControllerWorking() if g.GlbClientCfg.AdminPort != 0 { - err := svr.RunAdminServer(g.GlbClientCfg.AdminAddr, g.GlbClientCfg.AdminPort) + // Init admin server assets + err := assets.Load("") + if err != nil { + return fmt.Errorf("Load assets error: %v", err) + } + + err = svr.RunAdminServer(g.GlbClientCfg.AdminAddr, g.GlbClientCfg.AdminPort) if err != nil { log.Warn("run admin server error: %v", err) } diff --git a/server/service.go b/server/service.go index e3a1d11..b1523c9 100644 --- a/server/service.go +++ b/server/service.go @@ -108,13 +108,6 @@ func NewService() (svr *Service, err error) { // Init HTTP group controller svr.rc.HTTPGroupCtl = group.NewHTTPGroupController(svr.httpVhostRouter) - // Init assets - err = assets.Load(cfg.AssetsDir) - if err != nil { - err = fmt.Errorf("Load assets error: %v", err) - return - } - // Init 404 not found page vhost.NotFoundPagePath = cfg.Custom404Page @@ -231,6 +224,13 @@ func NewService() (svr *Service, err error) { var statsEnable bool // Create dashboard web server. if cfg.DashboardPort > 0 { + // Init dashboard assets + err = assets.Load(cfg.AssetsDir) + if err != nil { + err = fmt.Errorf("Load assets error: %v", err) + return + } + err = svr.RunDashboardServer(cfg.DashboardAddr, cfg.DashboardPort) if err != nil { err = fmt.Errorf("Create dashboard web server error, %v", err)