Reorganize variables/funcs/type declarations, fix unnecessary Sprintf, move type def to types.go

This commit is contained in:
2023-12-12 04:16:18 -06:00
parent cc2536ab99
commit 29b8477e7a
3 changed files with 16 additions and 21 deletions

28
api.go
View File

@@ -13,13 +13,17 @@ import (
) )
var ( var (
client *http.Client client *http.Client
requestCounter uint requestCounter uint
lastReload int64 lastReload int64
parsePattern = regexp.MustCompile("\\s*(.+)\\n\\s+(.+)\\n\\s+(\\d+)\\s*") parsePattern = regexp.MustCompile(`\s*(.+)\n\s+(.+)\n\s+(\d+)\s*`)
cachedLocations []Location
cachedLocationsMap map[uint]Location
cacheExpiry time.Time
) )
func init() { func init() {
cacheExpiry = time.Now().Add(-time.Second) // Set the cache as expired initially
cookies, err := cookiejar.New(nil) cookies, err := cookiejar.New(nil)
if err != nil { if err != nil {
log.Fatal(err) 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 { func GetLocations() []Location {
if time.Now().Before(cacheExpiry) { if time.Now().Before(cacheExpiry) {
return cachedLocations return cachedLocations

View File

@@ -24,7 +24,7 @@ func DebugRequest(req *http.Request) string {
str += fmt.Sprintf("\n\n {error while reading request body buffer: %s}", err) str += fmt.Sprintf("\n\n {error while reading request body buffer: %s}", err)
} else { } else {
if len(body) == 0 { if len(body) == 0 {
str += fmt.Sprintf("\n\n{empty body}") str += "\n\n{empty body}"
} else { } else {
str += fmt.Sprintf("\n\n%s", body) str += fmt.Sprintf("\n\n%s", body)
} }

7
types.go Normal file
View File

@@ -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
}