Files
wish-serve/main.go
Yves Biener ba8e654b90
All checks were successful
Go Project Action / Spell-check and test go project (push) Successful in 1m2s
Release Go Application / Release go project (release) Successful in 34s
Go Project Action / Spell-check and test go project (release) Successful in 29s
fix(conf): fix detection of configuration file not found error
2024-10-06 11:33:29 +02:00

45 lines
1.0 KiB
Go

package main
import (
"fmt"
"github.com/spf13/viper"
"yves-biener.de/wish-serve/internal/serve"
)
func createDefaultConfig() {
viper.SetDefault("host", "127.0.0.1")
viper.SetDefault("port", "8022")
viper.SetDefault("users", map[string]string{})
viper.SetDefault("app.name", "echo")
viper.SetDefault("app.args", []string{"Hello World"})
}
// Load the configuration file from (`$HOME/.config/serve/config.yml` or
// `./config.yml`) if it exists. Otherwise use the default values from
// `createDefaultConfig()`.
func loadConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yml")
viper.AddConfigPath("$HOME/.config/serve")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
}
func main() {
createDefaultConfig()
loadConfig()
serve.Serve(
viper.GetString("host"),
viper.GetString("port"),
viper.GetStringMapString("users"),
viper.GetString("app.name"),
viper.GetStringSlice("app.args"),
)
}