feat: spaces redesign, model selector, auth fixes
Spaces: - Perplexity-like UI with collaboration features - Space detail page with threads/members tabs - Invite members via email, role management - New space creation with icon/color picker Model selector: - Added Ollama client for free Auto model - GooSeek 1.0 via Timeweb (tariff-based) - Frontend model dropdown in ChatInput Auth & Infrastructure: - Fixed auth-svc missing from Dockerfile.all - Removed duplicate ratelimit_tiered.go (conflict) - Added Redis to api-gateway for rate limiting - Fixed Next.js proxy for local development UI improvements: - Redesigned login button in sidebar (gradient) - Settings page with tabs (account/billing/prefs) - Auth pages visual refresh Made-with: Cursor
This commit is contained in:
@@ -70,6 +70,10 @@ type Config struct {
|
||||
TimewebAPIKey string
|
||||
TimewebProxySource string
|
||||
|
||||
// Ollama (local LLM)
|
||||
OllamaBaseURL string
|
||||
OllamaModelKey string
|
||||
|
||||
// Timeouts
|
||||
HTTPTimeout time.Duration
|
||||
LLMTimeout time.Duration
|
||||
@@ -141,6 +145,9 @@ func Load() (*Config, error) {
|
||||
TimewebAPIKey: getEnv("TIMEWEB_API_KEY", ""),
|
||||
TimewebProxySource: getEnv("TIMEWEB_X_PROXY_SOURCE", "gooseek"),
|
||||
|
||||
OllamaBaseURL: getEnv("OLLAMA_BASE_URL", "http://ollama:11434"),
|
||||
OllamaModelKey: getEnv("OLLAMA_MODEL", "llama3.2"),
|
||||
|
||||
HTTPTimeout: time.Duration(getEnvInt("HTTP_TIMEOUT_MS", 60000)) * time.Millisecond,
|
||||
LLMTimeout: time.Duration(getEnvInt("LLM_TIMEOUT_MS", 120000)) * time.Millisecond,
|
||||
ScrapeTimeout: time.Duration(getEnvInt("SCRAPE_TIMEOUT_MS", 25000)) * time.Millisecond,
|
||||
|
||||
@@ -144,6 +144,7 @@ type TieredRateLimitConfig struct {
|
||||
RedisClient *redis.Client
|
||||
KeyPrefix string
|
||||
Tiers map[string]TierConfig
|
||||
DefaultTier string
|
||||
GetTierFunc func(*fiber.Ctx) string
|
||||
KeyFunc func(*fiber.Ctx) string
|
||||
}
|
||||
@@ -157,16 +158,31 @@ func TieredRateLimit(cfg TieredRateLimitConfig) fiber.Handler {
|
||||
if cfg.KeyPrefix == "" {
|
||||
cfg.KeyPrefix = "ratelimit:tiered"
|
||||
}
|
||||
if cfg.DefaultTier == "" {
|
||||
cfg.DefaultTier = "free"
|
||||
}
|
||||
if cfg.GetTierFunc == nil {
|
||||
cfg.GetTierFunc = func(c *fiber.Ctx) string { return "default" }
|
||||
cfg.GetTierFunc = func(c *fiber.Ctx) string {
|
||||
tier := GetUserTier(c)
|
||||
if tier == "" {
|
||||
return cfg.DefaultTier
|
||||
}
|
||||
return tier
|
||||
}
|
||||
}
|
||||
if cfg.KeyFunc == nil {
|
||||
cfg.KeyFunc = func(c *fiber.Ctx) string { return c.IP() }
|
||||
cfg.KeyFunc = func(c *fiber.Ctx) string {
|
||||
userID := GetUserID(c)
|
||||
if userID != "" {
|
||||
return "user:" + userID
|
||||
}
|
||||
return "ip:" + c.IP()
|
||||
}
|
||||
}
|
||||
|
||||
defaultTier := TierConfig{Max: 60, Window: time.Minute}
|
||||
if _, ok := cfg.Tiers["default"]; !ok {
|
||||
cfg.Tiers["default"] = defaultTier
|
||||
defaultTierCfg := TierConfig{Max: 60, Window: time.Minute}
|
||||
if _, ok := cfg.Tiers[cfg.DefaultTier]; !ok {
|
||||
cfg.Tiers[cfg.DefaultTier] = defaultTierCfg
|
||||
}
|
||||
|
||||
return func(c *fiber.Ctx) error {
|
||||
@@ -174,7 +190,7 @@ func TieredRateLimit(cfg TieredRateLimitConfig) fiber.Handler {
|
||||
tier := cfg.GetTierFunc(c)
|
||||
tierCfg, ok := cfg.Tiers[tier]
|
||||
if !ok {
|
||||
tierCfg = cfg.Tiers["default"]
|
||||
tierCfg = cfg.Tiers[cfg.DefaultTier]
|
||||
}
|
||||
|
||||
key := fmt.Sprintf("%s:%s:%s", cfg.KeyPrefix, tier, cfg.KeyFunc(c))
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type TierConfig struct {
|
||||
Max int
|
||||
Window time.Duration
|
||||
}
|
||||
|
||||
type TieredRateLimitConfig struct {
|
||||
Tiers map[string]TierConfig
|
||||
DefaultTier string
|
||||
KeyFunc func(*fiber.Ctx) string
|
||||
GetTierFunc func(*fiber.Ctx) string
|
||||
}
|
||||
|
||||
type tieredRateLimiter struct {
|
||||
requests map[string][]time.Time
|
||||
mu sync.RWMutex
|
||||
tiers map[string]TierConfig
|
||||
}
|
||||
|
||||
func newTieredRateLimiter(tiers map[string]TierConfig) *tieredRateLimiter {
|
||||
rl := &tieredRateLimiter{
|
||||
requests: make(map[string][]time.Time),
|
||||
tiers: tiers,
|
||||
}
|
||||
go rl.cleanup()
|
||||
return rl
|
||||
}
|
||||
|
||||
func (rl *tieredRateLimiter) cleanup() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
for range ticker.C {
|
||||
rl.mu.Lock()
|
||||
now := time.Now()
|
||||
for key, times := range rl.requests {
|
||||
var valid []time.Time
|
||||
for _, t := range times {
|
||||
if now.Sub(t) < 5*time.Minute {
|
||||
valid = append(valid, t)
|
||||
}
|
||||
}
|
||||
if len(valid) == 0 {
|
||||
delete(rl.requests, key)
|
||||
} else {
|
||||
rl.requests[key] = valid
|
||||
}
|
||||
}
|
||||
rl.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (rl *tieredRateLimiter) allow(key string, tier string) (bool, int, int) {
|
||||
rl.mu.Lock()
|
||||
defer rl.mu.Unlock()
|
||||
|
||||
cfg, ok := rl.tiers[tier]
|
||||
if !ok {
|
||||
cfg = rl.tiers["free"]
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
windowStart := now.Add(-cfg.Window)
|
||||
|
||||
times := rl.requests[key]
|
||||
var valid []time.Time
|
||||
for _, t := range times {
|
||||
if t.After(windowStart) {
|
||||
valid = append(valid, t)
|
||||
}
|
||||
}
|
||||
|
||||
remaining := cfg.Max - len(valid)
|
||||
if remaining <= 0 {
|
||||
rl.requests[key] = valid
|
||||
return false, 0, cfg.Max
|
||||
}
|
||||
|
||||
rl.requests[key] = append(valid, now)
|
||||
return true, remaining - 1, cfg.Max
|
||||
}
|
||||
|
||||
func TieredRateLimit(config TieredRateLimitConfig) fiber.Handler {
|
||||
if config.Tiers == nil {
|
||||
config.Tiers = map[string]TierConfig{
|
||||
"free": {Max: 60, Window: time.Minute},
|
||||
"pro": {Max: 300, Window: time.Minute},
|
||||
"business": {Max: 1000, Window: time.Minute},
|
||||
}
|
||||
}
|
||||
if config.DefaultTier == "" {
|
||||
config.DefaultTier = "free"
|
||||
}
|
||||
if config.KeyFunc == nil {
|
||||
config.KeyFunc = func(c *fiber.Ctx) string {
|
||||
userID := GetUserID(c)
|
||||
if userID != "" {
|
||||
return "user:" + userID
|
||||
}
|
||||
return "ip:" + c.IP()
|
||||
}
|
||||
}
|
||||
if config.GetTierFunc == nil {
|
||||
config.GetTierFunc = func(c *fiber.Ctx) string {
|
||||
tier := GetUserTier(c)
|
||||
if tier == "" {
|
||||
return config.DefaultTier
|
||||
}
|
||||
return tier
|
||||
}
|
||||
}
|
||||
|
||||
limiter := newTieredRateLimiter(config.Tiers)
|
||||
|
||||
return func(c *fiber.Ctx) error {
|
||||
key := config.KeyFunc(c)
|
||||
tier := config.GetTierFunc(c)
|
||||
|
||||
allowed, remaining, limit := limiter.allow(key, tier)
|
||||
|
||||
c.Set("X-RateLimit-Limit", formatInt(limit))
|
||||
c.Set("X-RateLimit-Remaining", formatInt(remaining))
|
||||
c.Set("X-RateLimit-Tier", tier)
|
||||
|
||||
if !allowed {
|
||||
c.Set("Retry-After", "60")
|
||||
return c.Status(429).JSON(fiber.Map{
|
||||
"error": "Rate limit exceeded",
|
||||
"tier": tier,
|
||||
"limit": limit,
|
||||
"retryAfter": 60,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func formatInt(n int) string {
|
||||
if n < 0 {
|
||||
n = 0
|
||||
}
|
||||
s := ""
|
||||
if n == 0 {
|
||||
return "0"
|
||||
}
|
||||
for n > 0 {
|
||||
s = string(rune('0'+n%10)) + s
|
||||
n /= 10
|
||||
}
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user