diff --git a/pkg/internal/server/api/destinations_api.go b/pkg/internal/server/api/destinations_api.go index 3250b47..a90a3f8 100644 --- a/pkg/internal/server/api/destinations_api.go +++ b/pkg/internal/server/api/destinations_api.go @@ -2,14 +2,14 @@ package api import ( "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" - "github.com/spf13/viper" ) func listDestination(c *fiber.Ctx) error { var destinations []models.BaseDestination - for _, value := range viper.GetStringSlice("destinations") { + for _, value := range services.DestinationsByIndex { var parsed models.BaseDestination raw, _ := jsoniter.Marshal(value) _ = jsoniter.Unmarshal(raw, &parsed) diff --git a/pkg/internal/services/boost.go b/pkg/internal/services/boost.go index 90e9210..f94f997 100644 --- a/pkg/internal/services/boost.go +++ b/pkg/internal/services/boost.go @@ -64,7 +64,7 @@ func CreateBoost(user *sec.UserInfo, source models.Attachment, destination int) AccountID: user.ID, } - if des, ok := destinationsByIndex[destination]; !ok { + if des, ok := DestinationsByIndex[destination]; !ok { return boost, fmt.Errorf("invalid destination: %d", destination) } else { var destBase models.BaseDestination diff --git a/pkg/internal/services/destinations.go b/pkg/internal/services/destinations.go index ed54289..62dc0be 100644 --- a/pkg/internal/services/destinations.go +++ b/pkg/internal/services/destinations.go @@ -1,6 +1,8 @@ package services import ( + "fmt" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" jsoniter "github.com/json-iterator/go" "github.com/rs/zerolog/log" @@ -13,15 +15,16 @@ type destinationMapping struct { } var ( - destinationsByIndex = make(map[int]destinationMapping) - destinationsByRegion = make(map[string]destinationMapping) + DestinationsByIndex = make(map[int]destinationMapping) + DestinationsByRegion = make(map[string]destinationMapping) ) func BuildDestinationMapping() { - count := 0 - for idx, value := range viper.GetStringSlice("destinations") { + count := len(viper.GetStringSlice("destinations")) + for idx := 0; idx < count; idx++ { + destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", idx)) var parsed models.BaseDestination - raw, _ := jsoniter.Marshal(value) + raw, _ := jsoniter.Marshal(destMap) _ = jsoniter.Unmarshal(raw, &parsed) mapping := destinationMapping{ @@ -30,8 +33,8 @@ func BuildDestinationMapping() { } if len(parsed.Region) > 0 { - destinationsByIndex[idx] = mapping - destinationsByRegion[parsed.Region] = mapping + DestinationsByIndex[idx] = mapping + DestinationsByRegion[parsed.Region] = mapping } count++ diff --git a/pkg/internal/services/opener.go b/pkg/internal/services/opener.go index 569db7f..1509ad9 100644 --- a/pkg/internal/services/opener.go +++ b/pkg/internal/services/opener.go @@ -77,7 +77,7 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str var rawDest []byte if len(region) > 0 { - if des, ok := destinationsByRegion[region[0]]; ok { + if des, ok := DestinationsByRegion[region[0]]; ok { for _, boost := range result.Boosts { if boost.Destination == des.Index { rawDest = des.Raw @@ -89,12 +89,12 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str if rawDest == nil { if len(result.Boosts) > 0 { randomIdx := rand.IntN(len(result.Boosts)) - if des, ok := destinationsByIndex[randomIdx]; ok { + if des, ok := DestinationsByIndex[randomIdx]; ok { rawDest = des.Raw json.Unmarshal(rawDest, &dest) } } else { - if des, ok := destinationsByIndex[result.Attachment.Destination]; ok { + if des, ok := DestinationsByIndex[result.Attachment.Destination]; ok { rawDest = des.Raw json.Unmarshal(rawDest, &dest) }