Initial cloud-services repo - gateway service + pkg modules

This commit is contained in:
Chris Rai
2026-01-30 23:14:52 -05:00
commit fbb820d7b3
1037 changed files with 171318 additions and 0 deletions

21
pkg/timezone/model.go Normal file
View File

@@ -0,0 +1,21 @@
package timezone
import "time"
type AzureTimezoneResult struct {
Version string `json:"Version"`
ReferenceUtcTimestamp time.Time `json:"ReferenceUtcTimestamp"`
Timezones []AzureTimezone `json:"TimeZones"`
}
type AzureTimezone struct {
ID string `json:"Id"`
ReferenceTime AzureReferenceTime `json:"ReferenceTime"`
}
type AzureReferenceTime struct {
Tag string `json:"Tag"`
StandardOffset string `json:"StandardOffset"`
DaylightSavings string `json:"DaylightSavings"`
WallTime time.Time `json:"WallTime"`
}

202
pkg/timezone/timezone.go Normal file
View File

@@ -0,0 +1,202 @@
package timezone
import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"github.com/pkg/errors"
"fiskerinc.com/modules/redis"
"fiskerinc.com/modules/utils/envtool"
)
type Timezone struct {
ID string
Name string
Offset int
DST bool
}
type TimezoneServiceInterface interface {
GetTimezoneByCoordinate(latitude float32, longitude float32) (*Timezone, error)
GetCachedTimezoneByQuadkey(conn redis.Client, quadkey string) (*Timezone, error)
SetCachedTimezoneByQuadkey(conn redis.Client, quadkey string, timezone *Timezone) error
}
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type TimezoneService struct {
apiUrl string
apiKey string
Client HTTPClient
}
var (
timezoneService TimezoneServiceInterface
timezoneOnce sync.Once
)
func GetTimezoneService() TimezoneServiceInterface {
timezoneOnce.Do(func() {
if timezoneService != nil {
return
}
timezoneService = NewTimezoneService()
})
return timezoneService
}
func SetTimezoneService(timezoneInterface TimezoneServiceInterface) {
timezoneService = timezoneInterface
}
func NewTimezoneService() TimezoneServiceInterface {
return &TimezoneService{
apiUrl: "https://atlas.microsoft.com/timezone/byCoordinates/json",
apiKey: envtool.GetEnv("AZURE_TIMEZONE_API_KEY", "REPLACE_ME"),
Client: http.DefaultClient,
}
}
func (timezoneService *TimezoneService) GetTimezoneByCoordinate(latitude float32, longitude float32) (*Timezone, error) {
timezone := &Timezone{}
err := verifyCoordinate(latitude, longitude)
if err != nil {
return nil, errors.WithStack(err)
}
request := timezoneService.createRequest(latitude, longitude)
azureTimezone, err := getAzureTimezoneByCoordinate(timezoneService.Client, request)
if err != nil {
return nil, errors.WithStack(err)
}
standardOffset, err := getOffsetInSeconds(azureTimezone.ReferenceTime.StandardOffset)
if err != nil {
return nil, errors.New("could not parse UTC")
}
dstOffset, _ := getOffsetInSeconds(azureTimezone.ReferenceTime.DaylightSavings)
if err != nil {
return nil, errors.New("could not parse DST")
}
timezone.ID = azureTimezone.ReferenceTime.Tag
timezone.Name = azureTimezone.ID
timezone.Offset = standardOffset + dstOffset
timezone.DST = dstOffset != 0
return timezone, nil
}
func getAzureTimezoneByCoordinate(client HTTPClient, url string) (*AzureTimezone, error) {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.WithStack(err)
}
request.Header.Add("accept", "application/json")
request.Header.Add("Content-Type", "application/json")
resp, err := client.Do(request)
if err != nil {
return nil, errors.WithStack(err)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithStack(err)
}
timezoneResult := &AzureTimezoneResult{}
err = json.Unmarshal(respBody, timezoneResult)
if err != nil {
return nil, err
}
if len(timezoneResult.Timezones) == 0 {
return nil, errors.WithStack(errors.New("no timezone found"))
}
return &timezoneResult.Timezones[0], nil
}
func getOffsetInSeconds(offset string) (int, error) {
var result int
parts := strings.Split(offset, ":") // "-10:30:00" --> []string{"-10", "30", "00"}
power := len(parts) - 1
for _, part := range parts {
digit, err := strconv.Atoi(part)
if err != nil {
return 0, errors.New("could not convert offset")
}
if power > 0 {
// Seconds = Hours * 60^2
// Seconds = Minutes * 60^1
// Seconds = Seconds * 60^0 (there are no timezones off by seconds)
digit = digit * int(math.Pow(60, float64(power)))
}
if result < 0 {
// Honor existing negative value
digit *= -1
}
result += digit
power -= 1
}
return result, nil
}
func (timezoneService *TimezoneService) createRequest(latitude float32, longitude float32) string {
queryParams := url.Values{
"api-version": {fmt.Sprintf("%f", 1.0)},
"query": {fmt.Sprintf("%f,%f", latitude, longitude)},
"subscription-key": {timezoneService.apiKey},
}
return fmt.Sprintf("%s?%s", timezoneService.apiUrl, queryParams.Encode())
}
func verifyCoordinate(latitude, longitude float32) error {
if latitude > 90 || latitude < -90 {
return fmt.Errorf("latitude %f is out of bounds", latitude)
}
if longitude > 180 || longitude < -180 {
return fmt.Errorf("longitude %f is out of bounds", longitude)
}
return nil
}
func (timezoneService *TimezoneService) GetCachedTimezoneByQuadkey(conn redis.Client, quadkey string) (*Timezone, error) {
timezone := &Timezone{}
key := redis.TimezoneQuadKey(quadkey, 13)
err := conn.GetObject(key, timezone)
if err != nil {
return nil, err
}
return timezone, nil
}
func (timezoneService *TimezoneService) SetCachedTimezoneByQuadkey(conn redis.Client, quadkey string, timezone *Timezone) error {
dayInSeconds := 86400
key := redis.TimezoneQuadKey(quadkey, 13)
return conn.SetObject(key, timezone, dayInSeconds*7)
}

View File

@@ -0,0 +1,315 @@
package timezone_test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"fiskerinc.com/modules/redis"
"fiskerinc.com/modules/redis/tester"
"fiskerinc.com/modules/testhelper"
"fiskerinc.com/modules/timezone"
"fiskerinc.com/modules/validator"
)
var someTime, _ = time.Parse(time.RFC3339Nano, "2023-12-27T18:09:41.0701652Z")
func mockTime(timezone string) time.Time {
loc, _ := time.LoadLocation(timezone)
return someTime.In(loc)
}
type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return m.DoFunc(req)
}
var mocks = map[string]timezone.AzureTimezoneResult{
"52.520000,13.405000": {
Version: "2023c",
ReferenceUtcTimestamp: someTime,
Timezones: []timezone.AzureTimezone{
{
ID: "Europe/Berlin",
ReferenceTime: timezone.AzureReferenceTime{
Tag: "CET",
StandardOffset: "01:00:00",
DaylightSavings: "00:00:00",
WallTime: mockTime("Europe/Berlin"),
},
},
},
},
"37.774899,122.419403": {
Version: "2023c",
ReferenceUtcTimestamp: someTime,
Timezones: []timezone.AzureTimezone{
{
ID: "Etc/GMT-8",
ReferenceTime: timezone.AzureReferenceTime{
Tag: "Etc/GMT-8",
StandardOffset: "-08:00:00",
DaylightSavings: "01:00:00",
WallTime: mockTime("America/Los_Angeles"),
},
},
},
},
"-31.546177,159.083191": {
Version: "2023d",
ReferenceUtcTimestamp: someTime,
Timezones: []timezone.AzureTimezone{
{
ID: "Australia/Lord_Howe",
ReferenceTime: timezone.AzureReferenceTime{
Tag: "+11",
StandardOffset: "10:30:00",
DaylightSavings: "00:30:00",
WallTime: mockTime("Australia/Lord_Howe"),
},
},
},
},
}
type TestTimezone struct {
Name string
Latitude float32
Longitude float32
Response timezone.Timezone
ExpectedError string
}
func TestGetTimezoneByCoordinate(t *testing.T) {
mockAzureTimezoneAPI := &MockClient{
DoFunc: func(r *http.Request) (*http.Response, error) {
query := r.URL.Query().Get("query")
coords := strings.Split(query, ",")
if payload, ok := mocks[query]; ok {
resp, err := json.Marshal(payload)
if err == nil {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(resp)),
}, nil
}
}
outOfBounds := fmt.Sprintf("latitude %s is out of bounds. longitude %s is out of bounds", coords[0], coords[1])
return &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewBufferString(outOfBounds)),
}, nil
},
}
timezone.SetTimezoneService(&timezone.TimezoneService{
Client: mockAzureTimezoneAPI,
})
var tests = []TestTimezone{
{
Name: "Berlin",
Latitude: 52.5200,
Longitude: 13.4050,
Response: timezone.Timezone{
ID: "CET",
Name: "Europe/Berlin",
Offset: 3600,
DST: false,
},
},
{
Name: "San Francisco",
Latitude: 37.7749,
Longitude: 122.4194,
Response: timezone.Timezone{
ID: "Etc/GMT-8",
Name: "Etc/GMT-8",
Offset: -25200,
DST: true,
},
},
{
Name: "Australia",
Latitude: -31.5461769,
Longitude: 159.0831909,
Response: timezone.Timezone{
ID: "+11",
Name: "Australia/Lord_Howe",
Offset: 39600,
DST: true,
},
},
{
Name: "Saturn",
Latitude: 1832.7749,
Longitude: 2378.4194,
ExpectedError: "latitude 1832.774902 is out of bounds",
},
}
for _, tt := range tests {
err := validator.ValidateStruct(tt)
if err != nil {
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Response, err)
}
actual, err := timezone.GetTimezoneService().GetTimezoneByCoordinate(tt.Latitude, tt.Longitude)
if err != nil {
if err.Error() == tt.ExpectedError {
continue
}
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Response, err)
}
compare(t, *actual, tt.Response)
}
}
type TestTimezoneIntegration struct {
Name string
Latitude float32
Longitude float32
Timezone timezone.Timezone
}
func TestGetTimezoneByCoordinate_Integration(t *testing.T) {
t.Skip()
t.Setenv("AZURE_TIMEZONE_API_KEY", "") // Temporarily paste API key (see .env or README.md)
var tests = []TestTimezoneIntegration{
{
Name: "Berlin",
Latitude: 52,
Longitude: 13,
Timezone: timezone.Timezone{
ID: "CET",
Name: "Europe/Berlin",
Offset: 1,
DST: false,
},
},
{
Name: "San Francisco",
Latitude: 37.7749,
Longitude: 122.4194,
Timezone: timezone.Timezone{
ID: "Etc/GMT-8",
Name: "Etc/GMT-8",
Offset: -8,
DST: false, // data is not historical, this could fail on time of year
},
},
}
for _, tt := range tests {
actual, err := timezone.GetTimezoneService().GetTimezoneByCoordinate(tt.Latitude, tt.Longitude)
if err != nil {
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Timezone, err)
}
compare(t, *actual, tt.Timezone)
}
}
type TestTimezoneCache struct {
Name string
QuadKey string
Timezone timezone.Timezone
}
func TestGetCachedTimezoneByQuadkey(t *testing.T) {
conn := tester.NewRedisMock()
conn.GetObjectResults = map[string]string{
"timezone:1321013300201": `{"ID":"Etc/GMT-8","Name":"Etc/GMT-8","Offset":-8,"DST":false}`,
}
tests := []TestTimezoneCache{
{
Name: "San Francisco",
QuadKey: "1321013300201123120",
Timezone: timezone.Timezone{
ID: "Etc/GMT-8",
Name: "Etc/GMT-8",
Offset: -8,
DST: false,
},
},
}
for _, tt := range tests {
actual, err := timezone.GetTimezoneService().GetCachedTimezoneByQuadkey(conn, tt.QuadKey)
if err != nil {
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Timezone, err)
}
compare(t, *actual, tt.Timezone)
}
}
func TestSetCachedTimezoneByQuadkey(t *testing.T) {
conn := tester.NewRedisMock()
tests := []TestTimezoneCache{
{
Name: "San Francisco",
QuadKey: "1321013300201123120",
Timezone: timezone.Timezone{
ID: "Etc/GMT-8",
Name: "Etc/GMT-8",
Offset: -8,
DST: false,
},
},
}
for _, tt := range tests {
err := timezone.GetTimezoneService().SetCachedTimezoneByQuadkey(conn, tt.QuadKey, &tt.Timezone)
if err != nil {
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Timezone, err)
}
// lookup key set in redis mock
key := redis.TimezoneQuadKey(tt.QuadKey, 13)
value, ok := conn.FetchCache(key)
if !ok {
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Timezone, err)
}
// convert redis value back to timezone.Timezone
var actual timezone.Timezone
temp, _ := json.Marshal(value.Value)
err = json.Unmarshal(temp, &actual)
if err != nil {
t.Errorf(testhelper.TestErrorTemplate, tt.Name, tt.Timezone, err)
}
compare(t, actual, tt.Timezone)
}
}
func compare(t *testing.T, actual, expected timezone.Timezone) {
if actual.Name != expected.Name {
t.Errorf("expected name %s, got %s", expected.Name, actual.Name)
}
if actual.ID != expected.ID {
t.Errorf("expected id %s, got %s", expected.ID, actual.ID)
}
if actual.Offset != expected.Offset {
t.Errorf("expected offset %d, got %d", expected.Offset, actual.Offset)
}
if actual.DST != expected.DST {
t.Errorf("expected dst %t, got %t", expected.DST, actual.DST)
}
}