blob: 660526221d595efdd9efc60c458c5f7795b5d7f7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package main
import (
"bufio"
"crypto/tls"
"os"
"sync"
"go.lindenii.runxiyu.org/lindenii-common/scfg"
)
var config struct {
Server_name string `scfg:"server_name"`
Routes map[string]string `scfg:"routes"`
TLS struct {
Cert string `scfg:"cert"`
Key string `scfg:"key"`
} `scfg:"tls"`
_tls_config *tls.Config
}
var config_mutex sync.RWMutex
func load_config(path string) error {
config_file, err := os.Open(path)
if err != nil {
return err
}
decoder := scfg.NewDecoder(bufio.NewReader(config_file))
if func() error {
config_mutex.Lock()
defer config_mutex.Unlock()
err = decoder.Decode(&config)
if err != nil {
return err
}
cer, err := tls.LoadX509KeyPair(config.TLS.Cert, config.TLS.Key)
if err != nil {
return err
}
config._tls_config = &tls.Config{
Certificates: []tls.Certificate{cer},
MinVersion: tls.VersionTLS13,
}
return nil
}() != nil {
return err
}
return nil
}
// 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
}
// 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()
}
|