-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
97 lines (79 loc) · 2.5 KB
/
Copy pathconfig.go
File metadata and controls
97 lines (79 loc) · 2.5 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package redeye
import (
"errors"
"fmt"
"encoding/json"
"net/http"
"os"
"path/filepath"
)
type Configuration struct {
HTTPAddr string `json:"addr"` // http address and port
HTMLPath string `json:"basepath"` // html basepath
MQTTBroker string `json:"broker"` // MQTT broker URL
MQTTTopicPrefix string `json:"topic-prefix"` // MQTT topic namespace
VideoDevice string `json:"video-device"` // Capture device: index, name, or path
Image string `json:"image"` // Single image
Video string `json:"video"`
RTSPUrl string `json:"rtsp-url"`
CascadeFile string `json:"cascade-file"`
Pipeline string `json:"pipeline"`
WaitTime int `json:"wait-time"`
PluginDir string `json:"plugin-dir"` // directory of .so filter plugins to load at startup
ListFilters bool `json:"list-filters"` // List filters
ListCameras bool `json:"list-cameras"` // Print available cameras and exit
LogFile string `json:"log-file"` // log destination: stderr, stdout, or a file path
LogLevel string `json:"log-level"` // debug | info | warn | error
ID string `json:"id"`
Thumb string `json:"thumb"`
Debug bool `json:"debug"`
}
var (
Config *Configuration = &Configuration{}
)
func GetConfig() *Configuration {
return Config
}
func (c *Configuration) Save(path string) (err error) {
buf, err := json.Marshal(c)
if err != nil {
return fmt.Errorf("Config Save [%s] failed json.Marshal config [%w]", path, err)
}
err = os.WriteFile(path, buf, 0644)
if err != nil {
return fmt.Errorf("Config Save [%s] failed to save file: [%w]", path, err)
}
return err
}
func (c *Configuration) Load(path string) error {
buf, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("Config Load [%s] failed to read file: [%w]", path, err)
}
if err := json.Unmarshal(buf, c); err != nil {
return fmt.Errorf("Config Load [%s] failed json.Unmarshal config [%w]", path, err)
}
return nil
}
func (c *Configuration) LoadDefault() error {
paths := []string{"redeye.json"}
if homeDir, err := os.UserHomeDir(); err == nil {
paths = append(paths, filepath.Join(homeDir, ".redeye.json"))
}
for _, path := range paths {
err := c.Load(path)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return err
}
return nil
}
return nil
}
// ServeHTTP provides the Web service for the configuration module
func (c Configuration) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
writeJSON(w, c)
}