mirror of
https://github.com/lucheng0127/virtuallan.git
synced 2025-09-26 12:41:10 +08:00
Make index.html configurable
This commit is contained in:
@@ -11,4 +11,5 @@ routes:
|
||||
nexthop: wj # route nexthop user name
|
||||
web:
|
||||
enable: true
|
||||
port: 8000
|
||||
#port: 8000
|
||||
index: "/opt/virtuallan/static/index.html" # web index html file, must name as index.html
|
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/creasty/defaults"
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -16,8 +17,9 @@ const (
|
||||
)
|
||||
|
||||
type WebConfig struct {
|
||||
Enable bool `yaml:"enable"`
|
||||
Port int `yaml:"port"`
|
||||
Enable bool `yaml:"enable"`
|
||||
Port int `yaml:"port" default:"8000"`
|
||||
Index string `yaml:"index" validate:"required,validateIndex" default:"./static/index.html"`
|
||||
}
|
||||
|
||||
type RoutesConfig struct {
|
||||
@@ -26,14 +28,14 @@ type RoutesConfig struct {
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port int `yaml:"port" default:"6123"`
|
||||
IP string `yaml:"ip" validate:"required"`
|
||||
Bridge string `yaml:"bridge" validate:"required"`
|
||||
LogLevel string `yaml:"log-level" default:"info"`
|
||||
Key string `yaml:"key" validate:"required,validateKeyLen"`
|
||||
DHCPRange string `yaml:"dhcp-range" validate:"required"`
|
||||
WebConfig `yaml:"web"`
|
||||
Routes []RoutesConfig `yaml:"routes"`
|
||||
Port int `yaml:"port" default:"6123"`
|
||||
IP string `yaml:"ip" validate:"required"`
|
||||
Bridge string `yaml:"bridge" validate:"required"`
|
||||
LogLevel string `yaml:"log-level" default:"info"`
|
||||
Key string `yaml:"key" validate:"required,validateKeyLen"`
|
||||
DHCPRange string `yaml:"dhcp-range" validate:"required"`
|
||||
*WebConfig `yaml:"web"`
|
||||
Routes []RoutesConfig `yaml:"routes"`
|
||||
}
|
||||
|
||||
func GetCfgPath(dir string) string {
|
||||
@@ -51,14 +53,21 @@ func LoadConfigFile(path string) (*ServerConfig, error) {
|
||||
}
|
||||
|
||||
cfg := new(ServerConfig)
|
||||
cfg.WebConfig = new(WebConfig)
|
||||
|
||||
err = yaml.Unmarshal(f, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := defaults.Set(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfgValidator := validator.New()
|
||||
cfgValidator.RegisterValidation("validateKeyLen", ValidateKeyLength)
|
||||
cfgValidator.RegisterValidation("validateCidr", ValidateCIDR)
|
||||
cfgValidator.RegisterValidation("validateIndex", ValidateIndex)
|
||||
|
||||
if err := cfgValidator.Struct(cfg); err != nil {
|
||||
return nil, err
|
||||
@@ -70,13 +79,19 @@ func LoadConfigFile(path string) (*ServerConfig, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := defaults.Set(cfg); err != nil {
|
||||
return nil, err
|
||||
if cfg.WebConfig != nil && cfg.WebConfig.Enable {
|
||||
if err := cfgValidator.Struct(cfg.WebConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func ValidateIndex(fl validator.FieldLevel) bool {
|
||||
return strings.HasSuffix(fl.Field().String(), "index.html")
|
||||
}
|
||||
|
||||
func ValidateCIDR(fl validator.FieldLevel) bool {
|
||||
if _, _, err := net.ParseCIDR(fl.Field().String()); err != nil {
|
||||
return false
|
||||
|
@@ -150,7 +150,8 @@ func Run(cCtx *cli.Context) error {
|
||||
// Run web server
|
||||
if svc.ServerConfig.WebConfig.Enable {
|
||||
webSvc := &webServe{
|
||||
port: svc.ServerConfig.WebConfig.Port,
|
||||
port: svc.ServerConfig.WebConfig.Port,
|
||||
index: svc.ServerConfig.WebConfig.Index,
|
||||
}
|
||||
go webSvc.Serve()
|
||||
log.Info("run web server on port ", webSvc.port)
|
||||
|
@@ -11,7 +11,8 @@ import (
|
||||
)
|
||||
|
||||
type webServe struct {
|
||||
port int
|
||||
port int
|
||||
index string
|
||||
}
|
||||
|
||||
type EpEntry struct {
|
||||
@@ -67,7 +68,7 @@ func (svc *webServe) Serve() {
|
||||
gin.DefaultWriter = io.Discard
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLFiles("./static/index.html")
|
||||
router.LoadHTMLFiles(svc.index)
|
||||
|
||||
router.GET("/endpoints", listEpEntries)
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
|
Reference in New Issue
Block a user