-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
92 lines (80 loc) · 2.6 KB
/
config.go
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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
)
const DefaultJailBaseURL = "https://omsweb.public-safety-cloud.com"
type JailConfig struct {
// Used in API URLs; the jail's unique identifier in JailTracker
Slug string
// Whether or not we can currently pull data from the jail.
// Jail might require search, consistently times out or errors, etc
Usable bool
/* Optional fields */
// The Title field is currently ignored. This should gradually be broken out to Facility and State.
// It's a relic of the initial data scrape for list of jails.
// Name and state of facility
Facility string
State string
// URL for the jail. Usually "https://omsweb.public-safety-cloud.com", but not always!
BaseURL string
// The main public website of the facility itself
FacilityURL string
// JailTracker web page for viewing the roster. Helpful for including in logs for convenient debugging.
IndexURL string
// Some jails allow advance search; JTT isn't using this right now, but it's helpful to know.
// For example, searching by release status
HasAdvancedSearch bool
// Notes on the entry; e.g. why it isn't usable
Notes string
}
type AppConfig struct {
Jails []JailConfig
// Directory to cache jail data
Cache string
}
// Marshal data from filename into provided config
func (config *AppConfig) LoadConfig(filename string) error {
file, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read config file: %w", err)
}
err = json.Unmarshal(file, config)
if err != nil {
return fmt.Errorf("failed to unmarshal config file: %w", err)
}
for i := range config.Jails {
jailConfig := &config.Jails[i]
// Currently, most jails don't have a domain name in the config since I haven't looked them up yet.
if jailConfig.BaseURL == "" {
jailConfig.BaseURL = DefaultJailBaseURL
}
}
return nil
}
type AppEnv struct {
OpenAIAPIKey string // "JTT_OPENAI_API_KEY"
// Directory to cache jail data
ConfigPath string // "JTT_CONFIG_PATH"
}
// Load sets default values for empty optional environment variables
func (a *AppEnv) Load() {
a.OpenAIAPIKey = os.Getenv("JTT_OPENAI_API_KEY")
a.ConfigPath = os.Getenv("JTT_CONFIG_PATH")
if a.ConfigPath == "" {
a.ConfigPath = "config.json"
} else {
log.Printf("Using config file from environment (JTT_CONFIG_PATH=\"%s\"", a.ConfigPath)
}
}
// Validate checks that required environment variables are set.
// This is a separate step from Load, since it shouldn't be run on init() during tests.
func (a *AppEnv) ValidateRequired() error {
if a.OpenAIAPIKey == "" {
return errors.New("JTT_OPENAI_API_KEY must be set")
}
return nil
}