-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.go
89 lines (79 loc) · 2.71 KB
/
configuration.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
// Copyright: (c) 2022, Justin Béra (@just1not2) <me@just1not2.org>
// GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"time"
)
const (
DEFAULT_HTTP_TIMEOUT = 10
DEFAULT_PORT = 9923
)
type ExporterConfiguration struct {
HTTPTimeout float64 `json:"exporter_timeout"`
Port float64 `json:"exporter_port"`
Signature string `json:"signature"`
YourlsURL string `json:"url"`
}
type YourlsConfiguration struct {
HTTPTimeout time.Duration
Port float64
Signature string
YourlsURL string
}
func NewConfiguration() *YourlsConfiguration {
// Initializes exporter configuration
configuration := &ExporterConfiguration{
HTTPTimeout: DEFAULT_HTTP_TIMEOUT,
Port: DEFAULT_PORT,
}
// Uses configuration file if it is passed as a parameter
if len(os.Args) > 1 {
file, err := os.Open(os.Args[1])
if err != nil {
fmt.Printf("warn: %s, cannot read %s (skipping)\n", err, os.Args[1])
} else {
if err := json.NewDecoder(file).Decode(configuration); err != nil {
fmt.Printf("warn: %s, cannot read %s (skipping)\n", err, os.Args[1])
}
}
file.Close()
}
// Overwrites configuration with existing environment variables
if keyString, defined := os.LookupEnv("YOURLS_URL"); defined {
configuration.YourlsURL = keyString
}
if keyString, defined := os.LookupEnv("YOURLS_SIGNATURE"); defined {
configuration.Signature = keyString
}
if keyString, defined := os.LookupEnv("YOURLS_EXPORTER_PORT"); defined {
if keyFloat, err := strconv.ParseFloat(keyString, 64); err != nil {
fmt.Printf("warn: %s, cannot use YOURLS_EXPORTER_PORT environment variable (skipping)\n", err)
} else {
configuration.Port = keyFloat
}
}
if keyString, defined := os.LookupEnv("YOURLS_EXPORTER_TIMEOUT"); defined {
if keyFloat, err := strconv.ParseFloat(keyString, 64); err != nil {
fmt.Printf("warn: %s, cannot use YOURLS_EXPORTER_TIMEOUT environment variable (skipping)\n", err)
} else {
configuration.HTTPTimeout = keyFloat
}
}
// Throws error for missing fields
if configuration.YourlsURL == "" {
log.Fatal("error: YOURLS_URL environment variable and 'url' configuration parameter were both undefined")
} else if configuration.Signature == "" {
log.Fatal("error: YOURLS_SIGNATURE environment variable and 'signature' configuration parameter were both undefined")
}
return &YourlsConfiguration{
HTTPTimeout: time.Duration(configuration.HTTPTimeout * float64(time.Second)),
YourlsURL: fmt.Sprintf("%s/yourls-api.php?format=json&signature=%s", configuration.YourlsURL, configuration.Signature),
Port: configuration.Port,
Signature: configuration.Signature,
}
}