From 71ab9b7f14118f02dd18cd733bd4e0ad19ece590 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 5 Apr 2025 20:21:32 +0800 Subject: config shall no longer be a global variable --- config.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'config.go') diff --git a/config.go b/config.go index 6220d6f..1bbc3a1 100644 --- a/config.go +++ b/config.go @@ -16,7 +16,7 @@ import ( // config holds the global configuration used by this instance. There is // currently no synchronization mechanism, so it must not be modified after // request handlers are spawned. -var config struct { +type Config struct { HTTP struct { Net string `scfg:"net"` Addr string `scfg:"addr"` @@ -76,7 +76,7 @@ var config struct { // configuration patterns, but silently ignores fields in the [config] struct // that is not present in the user's configuration file. We would prefer the // exact opposite behavior. -func loadConfig(path string) (err error) { +func (s *server) loadConfig(path string) (err error) { var configFile *os.File if configFile, err = os.Open(path); err != nil { return err @@ -84,19 +84,19 @@ func loadConfig(path string) (err error) { defer configFile.Close() decoder := scfg.NewDecoder(bufio.NewReader(configFile)) - if err = decoder.Decode(&config); err != nil { + if err = decoder.Decode(&s.config); err != nil { return err } - if config.DB.Type != "postgres" { + if s.config.DB.Type != "postgres" { return errors.New("unsupported database type") } - if database, err = pgxpool.New(context.Background(), config.DB.Conn); err != nil { + if database, err = pgxpool.New(context.Background(), s.config.DB.Conn); err != nil { return err } - globalData["forge_title"] = config.General.Title + globalData["forge_title"] = s.config.General.Title return nil } -- cgit v1.2.3