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.
This commit is contained in:
Tyler Compton 2019-08-16 11:02:00 -07:00
parent a415573e45
commit 00bd0a8af4
2 changed files with 14 additions and 15 deletions

View File

@ -52,13 +52,6 @@ type Service struct {
} }
func NewService(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.VisitorConf) (svr *Service, err error) { 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{ svr = &Service{
pxyCfgs: pxyCfgs, pxyCfgs: pxyCfgs,
visitorCfgs: visitorCfgs, visitorCfgs: visitorCfgs,
@ -102,7 +95,13 @@ func (svr *Service) Run() error {
go svr.keepControllerWorking() go svr.keepControllerWorking()
if g.GlbClientCfg.AdminPort != 0 { 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 { if err != nil {
log.Warn("run admin server error: %v", err) log.Warn("run admin server error: %v", err)
} }

View File

@ -108,13 +108,6 @@ func NewService() (svr *Service, err error) {
// Init HTTP group controller // Init HTTP group controller
svr.rc.HTTPGroupCtl = group.NewHTTPGroupController(svr.httpVhostRouter) 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 // Init 404 not found page
vhost.NotFoundPagePath = cfg.Custom404Page vhost.NotFoundPagePath = cfg.Custom404Page
@ -231,6 +224,13 @@ func NewService() (svr *Service, err error) {
var statsEnable bool var statsEnable bool
// Create dashboard web server. // Create dashboard web server.
if cfg.DashboardPort > 0 { 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) err = svr.RunDashboardServer(cfg.DashboardAddr, cfg.DashboardPort)
if err != nil { if err != nil {
err = fmt.Errorf("Create dashboard web server error, %v", err) err = fmt.Errorf("Create dashboard web server error, %v", err)