-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (69 loc) · 2.63 KB
/
main.go
File metadata and controls
73 lines (69 loc) · 2.63 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
package main
import (
"flag"
"gopkg.in/yaml.v3"
"os"
"strings"
)
var Log = Logger{}
func main() {
// Create configuration object
configuration := Configuration{}
// Initialize logging
logFolder := "/var/log/mac-api"
_, err := os.Stat(logFolder)
if os.IsNotExist(err) || os.IsPermission(err) {
logFolder = "log"
}
Log.Initialize(strings.Join([]string{logFolder, "/log.txt"}, ""))
config := ""
config1 := "/etc/mac-api/config.conf"
config2 := "config/config.conf"
// Error checking for config files
_, err1 := os.Stat(config1)
_, err2 := os.Stat(config2)
if err1 == nil {
config = config1
} else if err2 == nil {
config = config2
} else if os.IsNotExist(err1) && os.IsNotExist(err2) {
Log.Logger.Info().Msg("No configuration file found. Using Commandline parameter.")
} else if !os.IsNotExist(err1) && os.IsPermission(err1) {
Log.Logger.Warn().Str("path", config1).Msg("Unable to use configuration file. No permission to access the configuration file.")
} else if !os.IsNotExist(err2) && os.IsPermission(err2) {
Log.Logger.Warn().Str("path", config2).Msg("Unable to use configuration file. No permission to access the configuration file.")
} else if err1 != nil {
Log.Logger.Warn().Str("error", err1.Error()).Msg("Error while accessing the configuration file.")
} else if err2 != nil {
Log.Logger.Warn().Str("error", err2.Error()).Msg("Error while accessing the configuration file.")
}
// Try to parse the configuration file if exists
if config != "" {
body, err := os.ReadFile(config)
if err != nil {
Log.Logger.Warn().Str("error", err.Error()).Msg("Error while reading the configuration file.")
}
err = yaml.Unmarshal([]byte(body), &configuration)
if err != nil {
Log.Logger.Warn().Str("error", err.Error()).Msg("Error while parsing the configuration file.")
}
// Set default configuration parameter
} else {
configuration.Address = ":8080"
configuration.TimeInterval = 86400
configuration.Logging.Debug = false
}
// Commandline flags
flag.StringVar(&configuration.Address, "port", configuration.Address, "Address for the API to run on (default: ':8080')")
flag.IntVar(&configuration.TimeInterval, "timeinterval", configuration.TimeInterval, "Time interval when the data should be refreshed in seconds (default: 86400)")
flag.BoolVar(&configuration.Logging.Debug, "debug", configuration.Logging.Debug, "Option to run the API in debugging mode")
flag.Parse()
// Check if debug log should be enabled
if configuration.Logging.Debug {
Log.EnableDebug(true)
}
// Create app worker
a := App{}
a.Initialize("https://standards-oui.ieee.org/oui/oui.csv", configuration.Address, configuration.TimeInterval)
a.Run()
}