diff options
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 48 |
1 files changed, 16 insertions, 32 deletions
@@ -3,16 +3,14 @@ package main import ( "bufio" "os" - "reflect" "sync" - "go.lindenii.runxiyu.org/lindenii-common/misc" "go.lindenii.runxiyu.org/lindenii-common/scfg" ) var config struct { - Server_name string `scfg:"server_name"` - Inbox_path string `scfg:"inbox_path"` + Server_name string `scfg:"server_name"` + Routes map[string]string `scfg:"routes"` } var config_mutex sync.RWMutex @@ -36,34 +34,20 @@ func load_config(path string) error { return nil } -var directories struct { - inbox misc.Dir_t +// config_fetch_one fetches one value from the configuration. +// +// Consequtive calls do not guarantee a consistent snapshot of the +// configuration. Use config_consistent_run in these cases. +func config_fetch_one[T any](x *T) T { + config_mutex.RLock() + defer config_mutex.RUnlock() + return *x } -var directories_mutex sync.RWMutex -func prepare_dirs() error { - directories_mutex.Lock() - defer directories_mutex.Unlock() - var err error - directories.inbox, err = misc.Open_directory_readonly(config.Inbox_path) - if err != nil { - return err - } - return nil -} - -// clean_up_dirs closes all directories. It should only be called before -// program exit. -func clean_up_dirs() { - directories_mutex.Lock() - defer directories_mutex.Unlock() - directories_rv := reflect.ValueOf(directories) - directories_rt := directories_rv.Type() - for i := range directories_rt.NumField() { - dir, ok := directories_rv.Field(i).Interface().(misc.Dir_t) - if !ok { - panic("directories contains a field that's not misc.Dir_t") - } - dir.Close() - } +// config_consistent_run runs the supplied function with a consistent snapshot +// of the configuration. +func config_consistent_run(f func()) { + config_mutex.RLock() + defer config_mutex.RUnlock() + f() } |