aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-12 04:38:25 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-12 04:42:09 +0800
commit27cf6309af4536be615a34a647bbc54e6f8f43c6 (patch)
tree7563339698b24510f1024c4904f5a55ff7614c03 /config.go
parentUpdate README (diff)
downloadmaild-27cf6309af4536be615a34a647bbc54e6f8f43c6.tar.gz
maild-27cf6309af4536be615a34a647bbc54e6f8f43c6.tar.zst
maild-27cf6309af4536be615a34a647bbc54e6f8f43c6.zip
Add basic routing
Diffstat (limited to 'config.go')
-rw-r--r--config.go48
1 files changed, 16 insertions, 32 deletions
diff --git a/config.go b/config.go
index 8ba30ad..f187b69 100644
--- a/config.go
+++ b/config.go
@@ -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()
}