2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
0
|
// Copyright (C) 2024, Jakob Wakeling |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
1
|
// All rights reserved. |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
2
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
3
|
package goit |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
4
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
5
|
import ( |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
6
|
"encoding/json" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
7
|
"errors" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
8
|
"os" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
9
|
"path/filepath" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
10
|
) |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
11
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
12
|
type config struct { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
13
|
DataPath string `json:"data_path"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
14
|
LogsPath string `json:"logs_path"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
15
|
RuntimePath string `json:"runtime_path"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
16
|
HttpAddr string `json:"http_addr"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
17
|
HttpPort string `json:"http_port"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
18
|
GitPath string `json:"git_path"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
19
|
IpSessions bool `json:"ip_sessions"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
20
|
UsesHttps bool `json:"uses_https"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
21
|
IpForwarded bool `json:"ip_forwarded"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
22
|
CsrfSecret string `json:"csrf_secret"` |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
23
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
24
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
25
|
func loadConfig() (config, error) { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
26
|
conf := config{ |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
27
|
DataPath: dataPath(), |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
28
|
LogsPath: logsPath(), |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
29
|
RuntimePath: runtimePath(), |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
30
|
HttpAddr: "", |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
31
|
HttpPort: "8080", |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
32
|
GitPath: "git", |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
33
|
IpSessions: true, |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
34
|
UsesHttps: false, |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
35
|
IpForwarded: false, |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
36
|
CsrfSecret: "1234567890abcdef1234567890abcdef", |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
37
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
38
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
39
|
/* Load config file(s) */ |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
40
|
configs := []string{ |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
41
|
filepath.Join("/etc", "goit", "goit.conf"), |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
42
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
43
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
44
|
if os.Getuid() != 0 { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
45
|
configs = append(configs, filepath.Join(userConfigBase(), "goit", "goit.conf")) |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
46
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
47
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
48
|
for _, file := range configs { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
49
|
if data, err := os.ReadFile(file); err != nil { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
50
|
if !errors.Is(err, os.ErrNotExist) { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
51
|
return config{}, err |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
52
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
53
|
} else if data != nil { |
b6c2a7b |
Jakob Wakeling |
2024-03-15 21:11:22 |
54
|
if err := json.Unmarshal(data, &conf); err != nil { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
55
|
return config{}, err |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
56
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
57
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
58
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
59
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
60
|
/* Check required config values */ |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
61
|
if conf.DataPath == "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
62
|
return config{}, errors.New("data path unset") |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
63
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
64
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
65
|
return conf, nil |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
66
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
67
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
68
|
func userConfigBase() string { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
69
|
if path := os.Getenv("XDG_CONFIG_HOME"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
70
|
return path |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
71
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
72
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
73
|
if path := os.Getenv("HOME"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
74
|
return filepath.Join(path, ".config") |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
75
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
76
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
77
|
return "" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
78
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
79
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
80
|
func dataPath() string { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
81
|
if os.Getuid() == 0 { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
82
|
return "/var/lib/goit" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
83
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
84
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
85
|
if path := os.Getenv("XDG_DATA_HOME"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
86
|
return filepath.Join(path, "goit") |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
87
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
88
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
89
|
if path := os.Getenv("HOME"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
90
|
return filepath.Join(path, ".local", "share", "goit") |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
91
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
92
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
93
|
return "" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
94
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
95
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
96
|
func logsPath() string { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
97
|
if os.Getuid() == 0 { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
98
|
return "/var/log/goit" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
99
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
100
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
101
|
if path := os.Getenv("XDG_STATE_HOME"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
102
|
return filepath.Join(path, "goit") |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
103
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
104
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
105
|
if path := os.Getenv("HOME"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
106
|
return filepath.Join(path, ".local", "state", "goit") |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
107
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
108
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
109
|
return "" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
110
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
111
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
112
|
func runtimePath() string { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
113
|
if os.Getuid() == 0 { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
114
|
return "/run" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
115
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
116
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
117
|
if path := os.Getenv("XDG_RUNTIME_DIR"); path != "" { |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
118
|
return filepath.Join(path) |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
119
|
} |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
120
|
|
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
121
|
return "" |
2ac8bbb |
Jakob Wakeling |
2024-01-29 22:10:46 |
122
|
} |
|
|
|
123
|
|