Skip to content

Commit

Permalink
create default config if it does not already exist (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Jul 31, 2023
1 parent 6ef0773 commit 442ebe0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[Unit]
After=network.target
ConditionFileNotEmpty=/etc/casaos/gateway.ini
Description=CasaOS Gateway

[Service]
Expand Down
2 changes: 0 additions & 2 deletions cmd/migration-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
)

const (
gatewayConfigDirPath = "/etc/casaos"
gatewayConfigFilePath = "/etc/casaos/gateway.ini"
gatewayServiceName = "casaos-gateway.service"
gatewayServiceNameShort = "gateway"
)
Expand Down
19 changes: 12 additions & 7 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"

"github.com/spf13/viper"

"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
)

const (
Expand All @@ -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)
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
_ "embed"
"errors"
"flag"
"fmt"
Expand All @@ -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"
Expand All @@ -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() {
Expand All @@ -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)
Expand Down

0 comments on commit 442ebe0

Please sign in to comment.