Resolve public_base_url at SignURL time so admin settings apply without restart. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -72,10 +72,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
return "http://" + a
|
return "http://" + a
|
||||||
}
|
}
|
||||||
imgStore := imagestore.New(db.DB(), imageDir, publicURL(), rt.EncryptionKey(), rt.SignedURLTTL())
|
imgStore := imagestore.New(db.DB(), imageDir, publicURL, rt.EncryptionKey(), rt.SignedURLTTL())
|
||||||
_ = imgStore.EnsureDir()
|
_ = imgStore.EnsureDir()
|
||||||
|
|
||||||
predSvc := prediction.NewService(db.DB(), memCache, imgStore, publicURL(), rt.PredictionWaitSeconds())
|
predSvc := prediction.NewService(db.DB(), memCache, imgStore, rt.PredictionWaitSeconds())
|
||||||
|
|
||||||
gw := gateway.New(memCache, rt)
|
gw := gateway.New(memCache, rt)
|
||||||
mon := monitor.New(gw, memCache, db, rt)
|
mon := monitor.New(gw, memCache, db, rt)
|
||||||
|
|||||||
@@ -25,19 +25,18 @@ type Service struct {
|
|||||||
cache *cache.Store
|
cache *cache.Store
|
||||||
images *imagestore.Store
|
images *imagestore.Store
|
||||||
hub *Hub
|
hub *Hub
|
||||||
publicURL string
|
|
||||||
waitSec int64
|
waitSec int64
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
running map[string]context.CancelFunc
|
running map[string]context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(db *gorm.DB, c *cache.Store, images *imagestore.Store, publicURL string, waitSec int64) *Service {
|
func NewService(db *gorm.DB, c *cache.Store, images *imagestore.Store, waitSec int64) *Service {
|
||||||
if waitSec <= 0 {
|
if waitSec <= 0 {
|
||||||
waitSec = 60
|
waitSec = 60
|
||||||
}
|
}
|
||||||
return &Service{
|
return &Service{
|
||||||
db: db, cache: c, images: images, hub: NewHub(),
|
db: db, cache: c, images: images, hub: NewHub(),
|
||||||
publicURL: strings.TrimRight(publicURL, "/"), waitSec: waitSec,
|
waitSec: waitSec,
|
||||||
running: make(map[string]context.CancelFunc),
|
running: make(map[string]context.CancelFunc),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,12 +20,19 @@ type Store struct {
|
|||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
baseDir string
|
baseDir string
|
||||||
signKey string
|
signKey string
|
||||||
publicURL string
|
publicURL func() string
|
||||||
ttl time.Duration
|
ttl time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db *gorm.DB, baseDir, publicURL, signKey string, ttl time.Duration) *Store {
|
func New(db *gorm.DB, baseDir string, publicURL func() string, signKey string, ttl time.Duration) *Store {
|
||||||
return &Store{db: db, baseDir: baseDir, publicURL: strings.TrimRight(publicURL, "/"), signKey: signKey, ttl: ttl}
|
return &Store{db: db, baseDir: baseDir, publicURL: publicURL, signKey: signKey, ttl: ttl}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) baseURL() string {
|
||||||
|
if s.publicURL == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.TrimRight(s.publicURL(), "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) EnsureDir() error {
|
func (s *Store) EnsureDir() error {
|
||||||
@@ -76,7 +83,7 @@ func (s *Store) ListByPrediction(predictionID string) ([]model.StoredImage, erro
|
|||||||
func (s *Store) SignURL(imageID string) string {
|
func (s *Store) SignURL(imageID string) string {
|
||||||
exp := time.Now().Add(s.ttl).Unix()
|
exp := time.Now().Add(s.ttl).Unix()
|
||||||
sig := s.sign(imageID, exp)
|
sig := s.sign(imageID, exp)
|
||||||
return fmt.Sprintf("%s/files/%s?exp=%d&sig=%s", s.publicURL, imageID, exp, sig)
|
return fmt.Sprintf("%s/files/%s?exp=%d&sig=%s", s.baseURL(), imageID, exp, sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Verify(imageID string, exp int64, sig string) bool {
|
func (s *Store) Verify(imageID string, exp int64, sig string) bool {
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package imagestore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSignURLUsesDynamicPublicBase(t *testing.T) {
|
||||||
|
current := "http://localhost:8293"
|
||||||
|
store := New(nil, t.TempDir(), func() string { return current }, "test-key", time.Hour)
|
||||||
|
|
||||||
|
if got := store.SignURL("img-1"); !hasPrefix(got, "http://localhost:8293/files/img-1") {
|
||||||
|
t.Fatalf("unexpected url: %s", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
current = "https://gateway.example.com"
|
||||||
|
if got := store.SignURL("img-2"); !hasPrefix(got, "https://gateway.example.com/files/img-2") {
|
||||||
|
t.Fatalf("expected updated base url, got: %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasPrefix(s, prefix string) bool {
|
||||||
|
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user