diff --git a/build/sysroot/usr/lib/systemd/system/casaos-gateway.service b/build/sysroot/usr/lib/systemd/system/casaos-gateway.service index 5c878e7..5a51913 100644 --- a/build/sysroot/usr/lib/systemd/system/casaos-gateway.service +++ b/build/sysroot/usr/lib/systemd/system/casaos-gateway.service @@ -1,6 +1,5 @@ [Unit] After=network.target -ConditionFileNotEmpty=/etc/casaos/gateway.ini Description=CasaOS Gateway [Service] diff --git a/cmd/migration-tool/main.go b/cmd/migration-tool/main.go index 28ad90c..03e8baa 100644 --- a/cmd/migration-tool/main.go +++ b/cmd/migration-tool/main.go @@ -12,8 +12,6 @@ import ( ) const ( - gatewayConfigDirPath = "/etc/casaos" - gatewayConfigFilePath = "/etc/casaos/gateway.ini" gatewayServiceName = "casaos-gateway.service" gatewayServiceNameShort = "gateway" ) diff --git a/common/config.go b/common/config.go index c46c6de..42cde71 100644 --- a/common/config.go +++ b/common/config.go @@ -6,6 +6,8 @@ import ( "path/filepath" "github.com/spf13/viper" + + "github.com/IceWhaleTech/CasaOS-Common/utils/constants" ) const ( @@ -15,20 +17,23 @@ const ( ConfigKeyGatewayPort = "gateway.Port" ConfigKeyWWWPath = "gateway.WWWPath" ConfigKeyRuntimePath = "common.RuntimePath" + + GatewayName = "gateway" + GatewayConfigType = "ini" ) func LoadConfig() (*viper.Viper, error) { config := viper.New() - config.SetDefault(ConfigKeyLogPath, "/var/log/casaos") - config.SetDefault(ConfigKeyLogSaveName, "gateway") + config.SetDefault(ConfigKeyLogPath, constants.DefaultLogPath) + config.SetDefault(ConfigKeyLogSaveName, GatewayName) config.SetDefault(ConfigKeyLogFileExt, "log") - config.SetDefault(ConfigKeyWWWPath, "/var/lib/casaos/www") - config.SetDefault(ConfigKeyRuntimePath, "/var/run/casaos") // See https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s13.html + config.SetDefault(ConfigKeyWWWPath, filepath.Join(constants.DefaultDataPath, "www")) + config.SetDefault(ConfigKeyRuntimePath, constants.DefaultRuntimePath) // See https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s13.html - config.SetConfigName("gateway") - config.SetConfigType("ini") + config.SetConfigName(GatewayName) + config.SetConfigType(GatewayConfigType) if currentDirectory, err := os.Getwd(); err != nil { log.Println(err) @@ -41,7 +46,7 @@ func LoadConfig() (*viper.Viper, error) { config.AddConfigPath(configPath) } - config.AddConfigPath(filepath.Join("/", "etc", "casaos")) + config.AddConfigPath(constants.DefaultConfigPath) if err := config.ReadInConfig(); err != nil { return nil, err diff --git a/main.go b/main.go index a553876..57af927 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + _ "embed" "errors" "flag" "fmt" @@ -15,6 +16,7 @@ import ( "github.com/IceWhaleTech/CasaOS-Common/external" "github.com/IceWhaleTech/CasaOS-Common/model" + "github.com/IceWhaleTech/CasaOS-Common/utils/constants" http2 "github.com/IceWhaleTech/CasaOS-Common/utils/http" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" "github.com/coreos/go-systemd/daemon" @@ -39,6 +41,9 @@ var ( _gatewayServiceReady = make(chan struct{}) ErrCheckURLNotOK = errors.New("check url did not return 200 OK") + + //go:embed build/sysroot/etc/casaos/gateway.ini.sample + _confSample string ) func init() { @@ -55,6 +60,24 @@ func init() { _state = service.NewState() + // create default config file if not exist + ConfigFilePath := filepath.Join(constants.DefaultConfigPath, common.GatewayName+"."+common.GatewayConfigType) + if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { + fmt.Println("config file not exist, create it") + // create config file + file, err := os.Create(ConfigFilePath) + if err != nil { + panic(err) + } + defer file.Close() + + // write default config + _, err = file.WriteString(_confSample) + if err != nil { + panic(err) + } + } + config, err := common.LoadConfig() if err != nil { panic(err)