RoadSign/pkg/navi/config.go

59 lines
1.1 KiB
Go
Raw Normal View History

2024-01-17 06:34:08 +00:00
package navi
2023-11-16 15:06:59 +00:00
import (
"io"
"os"
"path/filepath"
2024-01-25 16:11:44 +00:00
"strings"
"time"
2024-01-25 16:11:44 +00:00
2024-01-24 16:09:39 +00:00
"github.com/pelletier/go-toml/v2"
2023-11-16 15:06:59 +00:00
)
2024-01-24 16:09:39 +00:00
var R *RoadApp
2023-11-16 15:06:59 +00:00
func ReadInConfig(root string) error {
2023-12-12 13:07:05 +00:00
instance := &RoadApp{
2024-01-24 16:09:39 +00:00
Regions: make([]*Region, 0),
2024-01-31 05:17:52 +00:00
Metrics: &RoadMetrics{
Traces: make([]RoadTrace, 0),
Traffic: make(map[string]int64),
TrafficFrom: make(map[string]int64),
TotalTraffic: 0,
StartupAt: time.Now(),
2024-01-31 05:17:52 +00:00
},
2023-11-16 15:06:59 +00:00
}
2024-01-24 16:09:39 +00:00
if err := filepath.Walk(root, func(fp string, info os.FileInfo, _ error) error {
var region Region
2023-11-16 15:06:59 +00:00
if info.IsDir() {
return nil
2024-01-25 16:11:44 +00:00
} else if !strings.HasSuffix(info.Name(), ".toml") {
return nil
2023-11-16 15:06:59 +00:00
} else if file, err := os.OpenFile(fp, os.O_RDONLY, 0755); err != nil {
return err
} else if data, err := io.ReadAll(file); err != nil {
return err
2024-01-24 16:09:39 +00:00
} else if err := toml.Unmarshal(data, &region); err != nil {
2023-11-16 15:06:59 +00:00
return err
} else {
2023-12-10 10:55:13 +00:00
defer file.Close()
2024-01-24 16:09:39 +00:00
if region.Disabled {
return nil
}
2024-01-25 16:11:44 +00:00
2024-01-24 16:09:39 +00:00
instance.Regions = append(instance.Regions, &region)
2023-11-16 15:06:59 +00:00
}
return nil
}); err != nil {
return err
}
2024-01-31 05:46:12 +00:00
// Hot swap
2024-01-24 16:09:39 +00:00
R = instance
2023-11-16 15:06:59 +00:00
return nil
}