diff --git a/run/server.go b/run/server.go index 35dd093..5a33ef5 100644 --- a/run/server.go +++ b/run/server.go @@ -16,8 +16,6 @@ import ( ) func Server() { - fmt.Println("Server Mode Not Implemented") - envCheck() http.HandleFunc("/traccar/", handleTraccarRequest) http.HandleFunc("/help/", handleHelpRequest) diff --git a/traccar/config.go b/traccar/config.go new file mode 100644 index 0000000..03b20ca --- /dev/null +++ b/traccar/config.go @@ -0,0 +1,81 @@ +package traccar + +import ( + "errors" + "os" + "strings" +) + +type cfgParam struct { + EnvVarName string + ConfFilePath string + DefaultValue string + FailIfAbsent bool +} + +type cfg struct { + user string + pass string + tUrl string +} + +func loadConfig() (*cfg, error) { + cfgParams := map[string]cfgParam{ + "user": { + EnvVarName: "TRACCAR_USER", + ConfFilePath: "/mapdots/cfg/traccar-user", + FailIfAbsent: true, + }, + "pass": { + EnvVarName: "TRACCAR_PASS", + ConfFilePath: "/mapdots/cfg/traccar-pass", + FailIfAbsent: true, + }, + "tUrl": { + EnvVarName: "TRACCAR_URL", + ConfFilePath: "/mapdots/cfg/traccar-url", + }, + } + + cfg := &cfg{} + + for key, param := range cfgParams { + if val, ok := os.LookupEnv(param.EnvVarName); ok { + cfg.setConfigValue(key, string(val)) + continue + } + + if data, err := os.ReadFile(param.ConfFilePath); err == nil { + cfg.setConfigValue(key, string(data)) + continue + } + + if param.DefaultValue != "" { + cfg.setConfigValue(key, param.DefaultValue) + continue + } + + if param.FailIfAbsent { + return nil, errors.New("Failed to load configuration: " + key + " is required but not set") + } + + } + + return cfg, nil +} + +func (c *cfg) setConfigValue(key, value string) { + + stripNewlines := func(s string) string { + return strings.ReplaceAll(s, "\n", "") + } + + switch key { + case "user": + c.user = stripNewlines(value) + case "pass": + c.pass = stripNewlines(value) + case "tUrl": + c.tUrl = stripNewlines(value) + } +} diff --git a/traccar/positions.go b/traccar/positions.go index 462a06c..83cb7c0 100644 --- a/traccar/positions.go +++ b/traccar/positions.go @@ -5,7 +5,6 @@ import ( "io" "net/http" "net/url" - "os" "time" "git.fjla.uk/fred.boniface/map-dots/data" @@ -13,10 +12,6 @@ import ( "go.uber.org/zap" ) -var user string = os.Getenv("TRACCAR_USER") -var pass string = os.Getenv("TRACCAR_PASS") -var tUrl string = os.Getenv("TRACCAR_URL") - func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) { var params = map[string]string{ "deviceId": id, @@ -25,24 +20,28 @@ func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) { } req, err := createRequest(params) if err != nil { + log.Msg.Error("Error creating request", zap.Error(err)) return nil, err } client := http.Client{} resp, err := client.Do(req) if err != nil { + log.Msg.Error("Error carring out request", zap.Error(err)) return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { + log.Msg.Error("Error reading response", zap.Error(err)) return nil, err } var positions []Positions err = json.Unmarshal(body, &positions) if err != nil { + log.Msg.Error("Error unmarshalling data", zap.Error(err), zap.String("body", string(body))) return nil, err } @@ -50,12 +49,18 @@ func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) { } func createRequest(params map[string]string) (*http.Request, error) { - baseURL := tUrl + "/api/positions" + cfg, err := loadConfig() + if err != nil { + log.Msg.Panic("Configuration value missing", zap.Error(err)) + } + + baseURL := cfg.tUrl + "/api/positions" log.Msg.Debug("Attemting fetch", zap.String("url", baseURL)) u, err := url.Parse(baseURL) if err != nil { + log.Msg.Error("Error building URL", zap.Error(err)) return nil, err } @@ -69,10 +74,11 @@ func createRequest(params map[string]string) (*http.Request, error) { req, err := http.NewRequest("GET", u.String(), nil) if err != nil { + log.Msg.Error("Error setting up request", zap.Error(err)) return nil, err } - req.SetBasicAuth(user, pass) + req.SetBasicAuth(cfg.user, cfg.pass) return req, nil }