From 29b8477e7a1b0199c7d2388bf99a423cd5603392 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 12 Dec 2023 04:16:18 -0600 Subject: [PATCH] Reorganize variables/funcs/type declarations, fix unnecessary Sprintf, move type def to types.go --- api.go | 28 ++++++++-------------------- debug.go | 2 +- types.go | 7 +++++++ 3 files changed, 16 insertions(+), 21 deletions(-) create mode 100644 types.go diff --git a/api.go b/api.go index 627f338..61a4812 100644 --- a/api.go +++ b/api.go @@ -13,13 +13,17 @@ import ( ) var ( - client *http.Client - requestCounter uint - lastReload int64 - parsePattern = regexp.MustCompile("\\s*(.+)\\n\\s+(.+)\\n\\s+(\\d+)\\s*") + client *http.Client + requestCounter uint + lastReload int64 + parsePattern = regexp.MustCompile(`\s*(.+)\n\s+(.+)\n\s+(\d+)\s*`) + cachedLocations []Location + cachedLocationsMap map[uint]Location + cacheExpiry time.Time ) func init() { + cacheExpiry = time.Now().Add(-time.Second) // Set the cache as expired initially cookies, err := cookiejar.New(nil) if err != nil { log.Fatal(err) @@ -90,22 +94,6 @@ func register(location uint, code string, make string, model string, plate strin } -type Location struct { - id uint // Used for registration internally - name string // Used for autocomplete & location selection - address string // Not used in this application so far -} - -var ( - cachedLocations []Location - cachedLocationsMap map[uint]Location - cacheExpiry time.Time -) - -func init() { - cacheExpiry = time.Now().Add(-time.Second) // Set the cache as expired initially -} - func GetLocations() []Location { if time.Now().Before(cacheExpiry) { return cachedLocations diff --git a/debug.go b/debug.go index 6030144..2dfc606 100644 --- a/debug.go +++ b/debug.go @@ -24,7 +24,7 @@ func DebugRequest(req *http.Request) string { str += fmt.Sprintf("\n\n {error while reading request body buffer: %s}", err) } else { if len(body) == 0 { - str += fmt.Sprintf("\n\n{empty body}") + str += "\n\n{empty body}" } else { str += fmt.Sprintf("\n\n%s", body) } diff --git a/types.go b/types.go new file mode 100644 index 0000000..1b48f3d --- /dev/null +++ b/types.go @@ -0,0 +1,7 @@ +package main + +type Location struct { + id uint // Used for registration internally + name string // Used for autocomplete & location selection + address string // Not used in this application so far +}