Skip to content

Commit

Permalink
refactor: move tilerequest and image out of pkg
Browse files Browse the repository at this point in the history
I had originally just put them there as a quick fix for circular dependency issues but that's long-irrelevant. Made no sense to have just those public. Down the road might rethink making things public and move them and other key interfaces into pkg.
  • Loading branch information
Michad committed Jun 7, 2024
1 parent 655c355 commit 0f5f33c
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 68 deletions.
14 changes: 7 additions & 7 deletions cmd/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"sync"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/layers"
"github.com/Michad/tilegroxy/pkg"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,13 +63,13 @@ Example:
os.Exit(1)
}

b := pkg.Bounds{MinLat: float64(minLat), MinLong: float64(minLon), MaxLat: float64(maxLat), MaxLong: float64(maxLon)}
b := internal.Bounds{MinLat: float64(minLat), MinLong: float64(minLon), MaxLat: float64(maxLat), MaxLong: float64(maxLon)}

tileRequests := make([]pkg.TileRequest, 0)
tileRequests := make([]internal.TileRequest, 0)

for _, z := range zoom {
if z > pkg.MaxZoom {
fmt.Printf("Error: zoom must be less than %v\n", pkg.MaxZoom)
if z > internal.MaxZoom {
fmt.Printf("Error: zoom must be less than %v\n", internal.MaxZoom)
os.Exit(1)
}
newTiles, err := b.FindTiles(layerName, uint(z), force)
Expand Down Expand Up @@ -100,7 +100,7 @@ Example:

chunkSize := int(math.Floor(float64(numReq) / float64(numThread)))

var reqSplit [][]pkg.TileRequest
var reqSplit [][]internal.TileRequest

for i := 0; i < int(numThread); i++ {
chunkStart := i * chunkSize
Expand All @@ -118,7 +118,7 @@ Example:

for t := int(0); t < len(reqSplit); t++ {
wg.Add(1)
go func(t int, myReqs []pkg.TileRequest) {
go func(t int, myReqs []internal.TileRequest) {
if verbose {
fmt.Printf("Created thread %v with %v tiles\n", t, len(myReqs))
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"sync"
"text/tabwriter"

"sync/atomic"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/layers"
"github.com/Michad/tilegroxy/pkg"
"github.com/spf13/cobra"
"sync/atomic"
)

var testCmd = &cobra.Command{
Expand Down Expand Up @@ -57,10 +58,10 @@ Example:
}

//Generate the full list of requests to process
tileRequests := make([]pkg.TileRequest, 0)
tileRequests := make([]internal.TileRequest, 0)

for _, layerName := range layerNames {
req := pkg.TileRequest{LayerName: layerName, Z: int(z), X: int(x), Y: int(y)}
req := internal.TileRequest{LayerName: layerName, Z: int(z), X: int(x), Y: int(y)}
_, err := req.GetBounds()

if err != nil {
Expand All @@ -87,7 +88,7 @@ Example:

//Split up all the requests for N threads
numReqPerThread := int(math.Floor(float64(numReq) / float64(numThread)))
var reqSplit [][]pkg.TileRequest
var reqSplit [][]internal.TileRequest

for i := 0; i < int(numThread); i++ {
chunkStart := i * numReqPerThread
Expand All @@ -110,7 +111,7 @@ Example:

for t := int(0); t < len(reqSplit); t++ {
wg.Add(1)
go func(t int, myReqs []pkg.TileRequest) {
go func(t int, myReqs []internal.TileRequest) {

for _, req := range myReqs {
layer := layerMap[req.LayerName]
Expand Down
6 changes: 3 additions & 3 deletions internal/caches/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ package caches
import (
"fmt"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/pkg"
"github.com/mitchellh/mapstructure"
)

type Cache interface {
Lookup(t pkg.TileRequest) (*pkg.Image, error)
Save(t pkg.TileRequest, img *pkg.Image) error
Lookup(t internal.TileRequest) (*internal.Image, error)
Save(t internal.TileRequest, img *internal.Image) error
}

func ConstructCache(rawConfig map[string]interface{}, errorMessages *config.ErrorMessages) (Cache, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/caches/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"path/filepath"
"strconv"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/pkg"
)

type DiskConfig struct {
Expand All @@ -35,7 +35,7 @@ type Disk struct {
config DiskConfig
}

func requestToFilename(t pkg.TileRequest) string {
func requestToFilename(t internal.TileRequest) string {
return t.LayerName + "_" + strconv.Itoa(t.Z) + "_" + strconv.Itoa(t.X) + "_" + strconv.Itoa(t.Y)
}

Expand All @@ -55,7 +55,7 @@ func ConstructDisk(config DiskConfig, ErrorMessages *config.ErrorMessages) (*Dis
return &Disk{config}, nil
}

func (c Disk) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c Disk) Lookup(t internal.TileRequest) (*internal.Image, error) {
filename := requestToFilename(t)

img, err := os.ReadFile(filepath.Join(c.config.Path, filename))
Expand All @@ -67,7 +67,7 @@ func (c Disk) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
return &img, err
}

func (c Disk) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c Disk) Save(t internal.TileRequest, img *internal.Image) error {
filename := requestToFilename(t)

return os.WriteFile(filepath.Join(c.config.Path, filename), *img, fs.FileMode(c.config.FileMode))
Expand Down
8 changes: 3 additions & 5 deletions internal/caches/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@

package caches

import (
"github.com/Michad/tilegroxy/pkg"
)
import "github.com/Michad/tilegroxy/internal"

type Memcache struct {
}

func (c Memcache) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c Memcache) Lookup(t internal.TileRequest) (*internal.Image, error) {
return nil, nil
}

func (c Memcache) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c Memcache) Save(t internal.TileRequest, img *internal.Image) error {
return nil
}
10 changes: 5 additions & 5 deletions internal/caches/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package caches
import (
"time"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/pkg"

"github.com/maypok86/otter"
)
Expand All @@ -42,8 +42,8 @@ func ConstructMemory(config MemoryConfig, ErrorMessages *config.ErrorMessages) (
config.Ttl = 3600
}

cache, err := otter.MustBuilder[string, pkg.Image](int(config.MaxSize)).
Cost(func(key string, value pkg.Image) uint32 {
cache, err := otter.MustBuilder[string, internal.Image](int(config.MaxSize)).
Cost(func(key string, value internal.Image) uint32 {
return uint32(len(value))
}).
WithTTL(time.Duration(config.Ttl * uint32(time.Second))).
Expand All @@ -55,10 +55,10 @@ func ConstructMemory(config MemoryConfig, ErrorMessages *config.ErrorMessages) (
return &Memory{config, cache}, nil
}

func (c Memory) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c Memory) Lookup(t internal.TileRequest) (*internal.Image, error) {
return nil, nil
}

func (c Memory) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c Memory) Save(t internal.TileRequest, img *internal.Image) error {
return nil
}
6 changes: 3 additions & 3 deletions internal/caches/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package caches
import (
"errors"

"github.com/Michad/tilegroxy/pkg"
"github.com/Michad/tilegroxy/internal"
)

type MultiConfig struct {
Expand All @@ -28,7 +28,7 @@ type Multi struct {
Tiers []Cache
}

func (c Multi) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c Multi) Lookup(t internal.TileRequest) (*internal.Image, error) {
var allErrors error

for _, cache := range c.Tiers {
Expand All @@ -45,7 +45,7 @@ func (c Multi) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
return nil, allErrors
}

func (c Multi) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c Multi) Save(t internal.TileRequest, img *internal.Image) error {
var allErrors error

for _, cache := range c.Tiers {
Expand Down
8 changes: 3 additions & 5 deletions internal/caches/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@

package caches

import (
"github.com/Michad/tilegroxy/pkg"
)
import "github.com/Michad/tilegroxy/internal"

type Noop struct {
}

func (c Noop) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c Noop) Lookup(t internal.TileRequest) (*internal.Image, error) {
return nil, nil
}

func (c Noop) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c Noop) Save(t internal.TileRequest, img *internal.Image) error {
return nil
}
12 changes: 6 additions & 6 deletions internal/caches/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"strconv"
"time"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/pkg"

"github.com/go-redis/cache/v9"
"github.com/redis/go-redis/v9"
Expand All @@ -49,7 +49,7 @@ type RedisConfig struct {
Password string //Password to use to authenticate
Mode string //Controls operating mode. One of AllModes. Defaults to standalone
Ttl uint32 //Cache expiration in seconds. Default to 1 day
Servers []RedisServer //The list of servers to use.
Servers []RedisServer //The list of servers to use.
}

type Redis struct {
Expand Down Expand Up @@ -80,7 +80,7 @@ func ConstructRedis(config *RedisConfig, errorMessages *config.ErrorMessages) (*
} else {
if config.Host != "" {
return nil, fmt.Errorf(errorMessages.ParamsMutuallyExclusive, "config.redis.host", "config.redis.servers")
}
}
}
if config.Ttl == 0 {
config.Ttl = 60 * 60 * 24
Expand Down Expand Up @@ -148,18 +148,18 @@ func ConstructRedis(config *RedisConfig, errorMessages *config.ErrorMessages) (*
return &r, nil
}

func (c Redis) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c Redis) Lookup(t internal.TileRequest) (*internal.Image, error) {
ctx := context.TODO()

key := c.KeyPrefix + t.String()
var obj pkg.Image
var obj internal.Image

err := c.cache.Get(ctx, key, &obj)

return &obj, err
}

func (c Redis) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c Redis) Save(t internal.TileRequest, img *internal.Image) error {
ctx := context.TODO()

key := c.KeyPrefix + t.String()
Expand Down
10 changes: 5 additions & 5 deletions internal/caches/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"strconv"
"strings"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/pkg"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -112,11 +112,11 @@ func ConstructS3(config *S3Config, errorMessages *config.ErrorMessages) (*S3, er
return &S3{config, downloader, uploader}, nil
}

func calcKey(config *S3, t *pkg.TileRequest) string {
func calcKey(config *S3, t *internal.TileRequest) string {
return config.Path + t.LayerName + "/" + strconv.Itoa(t.Z) + "/" + strconv.Itoa(t.X) + "/" + strconv.Itoa(t.Y)
}

func (c S3) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
func (c S3) Lookup(t internal.TileRequest) (*internal.Image, error) {
writer := aws.NewWriteAtBuffer([]byte{})

_, err := c.downloader.Download(
Expand All @@ -136,12 +136,12 @@ func (c S3) Lookup(t pkg.TileRequest) (*pkg.Image, error) {
return nil, err
}

img := pkg.Image(writer.Bytes())
img := internal.Image(writer.Bytes())

return &img, nil
}

func (c S3) Save(t pkg.TileRequest, img *pkg.Image) error {
func (c S3) Save(t internal.TileRequest, img *internal.Image) error {

uploadConfig := &s3manager.UploadInput{
Bucket: &c.Bucket,
Expand Down
10 changes: 5 additions & 5 deletions internal/layers/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"sync"
"time"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/caches"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/internal/providers"
"github.com/Michad/tilegroxy/pkg"
)

type Layer struct {
Expand Down Expand Up @@ -59,8 +59,8 @@ func (l *Layer) authWithProvider() error {
return err
}

func (l *Layer) RenderTile(tileRequest pkg.TileRequest) (*pkg.Image, error) {
var img *pkg.Image
func (l *Layer) RenderTile(tileRequest internal.TileRequest) (*internal.Image, error) {
var img *internal.Image
var err error

img, err = (*l.Cache).Lookup(tileRequest)
Expand Down Expand Up @@ -89,8 +89,8 @@ func (l *Layer) RenderTile(tileRequest pkg.TileRequest) (*pkg.Image, error) {
return img, nil
}

func (l *Layer) RenderTileNoCache(tileRequest pkg.TileRequest) (*pkg.Image, error) {
var img *pkg.Image
func (l *Layer) RenderTileNoCache(tileRequest internal.TileRequest) (*internal.Image, error) {
var img *internal.Image
var err error

if l.authContext == nil || l.authContext.Expiration.Before(time.Now()) {
Expand Down
6 changes: 3 additions & 3 deletions internal/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"slices"
"time"

"github.com/Michad/tilegroxy/internal"
"github.com/Michad/tilegroxy/internal/config"
"github.com/Michad/tilegroxy/pkg"
"github.com/mitchellh/mapstructure"
)

type Provider interface {
PreAuth(authContext *AuthContext) error
GenerateTile(authContext *AuthContext, clientConfig *config.ClientConfig, errorMessages *config.ErrorMessages, tileRequest pkg.TileRequest) (*pkg.Image, error)
GenerateTile(authContext *AuthContext, clientConfig *config.ClientConfig, errorMessages *config.ErrorMessages, tileRequest internal.TileRequest) (*internal.Image, error)
}

func ConstructProvider(rawConfig map[string]interface{}, errorMessages *config.ErrorMessages) (Provider, error) {
Expand Down Expand Up @@ -92,7 +92,7 @@ func (e *RemoteServerError) Error() string {
* Performs a GET operation against a given URL. Implementing providers should call this when possible. It has
* standard reusable logic around various config options
*/
func getTile(clientConfig *config.ClientConfig, url string, authHeaders map[string]string) (*pkg.Image, error) {
func getTile(clientConfig *config.ClientConfig, url string, authHeaders map[string]string) (*internal.Image, error) {
slog.Debug(fmt.Sprintf("Calling url %v\n", url))

req, err := http.NewRequest(http.MethodGet, url, nil)
Expand Down
Loading

0 comments on commit 0f5f33c

Please sign in to comment.