Spaces:
Running
Running
feat: add FL-MCP licensing and credit API routes and services
Browse files- app/routes/flmcp.py +34 -6
- app/services/flmcp/auth.py +7 -1
- app/services/flmcp/db.py +25 -17
- app/services/flmcp/plans.py +8 -2
app/routes/flmcp.py
CHANGED
|
@@ -45,16 +45,32 @@ def get_admin_token() -> str:
|
|
| 45 |
return os.environ.get("FLMCP_ADMIN_TOKEN", "")
|
| 46 |
|
| 47 |
|
| 48 |
-
def
|
| 49 |
-
raw =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
pairs = (p.split("=", 1) for p in raw.split(",") if "=" in p)
|
| 51 |
-
return {k.strip(): v.strip() for k, v in pairs}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def get_ls_variants() -> dict[str, str]:
|
| 55 |
-
|
| 56 |
-
pairs = (p.split("=", 1) for p in raw.split(",") if "=" in p)
|
| 57 |
-
return {k.strip(): v.strip() for k, v in pairs}
|
| 58 |
|
| 59 |
|
| 60 |
class ActivateIn(BaseModel):
|
|
@@ -284,6 +300,18 @@ def _summary(conn, db: DB, cfg: plans.Config, aid: str) -> dict:
|
|
| 284 |
"seats_allowed": lic["seats_allowed"],
|
| 285 |
}
|
| 286 |
out["devices"] = accounts.devices(conn, lic["id"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
return out
|
| 288 |
|
| 289 |
|
|
|
|
| 45 |
return os.environ.get("FLMCP_ADMIN_TOKEN", "")
|
| 46 |
|
| 47 |
|
| 48 |
+
def _parse_kv_or_json(raw: str) -> dict[str, str]:
|
| 49 |
+
raw = (raw or "").strip()
|
| 50 |
+
if not raw:
|
| 51 |
+
return {}
|
| 52 |
+
if raw.startswith("{"):
|
| 53 |
+
try:
|
| 54 |
+
parsed = json.loads(raw)
|
| 55 |
+
if isinstance(parsed, dict):
|
| 56 |
+
return {str(k).strip(): str(v).strip() for k, v in parsed.items() if str(k).strip()}
|
| 57 |
+
except Exception:
|
| 58 |
+
return {}
|
| 59 |
pairs = (p.split("=", 1) for p in raw.split(",") if "=" in p)
|
| 60 |
+
return {k.strip(): v.strip() for k, v in pairs if k.strip() and v.strip()}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def get_webhook_secrets() -> dict[str, str]:
|
| 64 |
+
secrets_map = _parse_kv_or_json(os.environ.get("FLMCP_WEBHOOK_SECRETS", ""))
|
| 65 |
+
for provider in ("lemonsqueezy", "stripe", "generic"):
|
| 66 |
+
env_val = os.environ.get(f"FLMCP_WEBHOOK_SECRET_{provider.upper()}", "").strip()
|
| 67 |
+
if env_val and provider not in secrets_map:
|
| 68 |
+
secrets_map[provider] = env_val
|
| 69 |
+
return secrets_map
|
| 70 |
|
| 71 |
|
| 72 |
def get_ls_variants() -> dict[str, str]:
|
| 73 |
+
return _parse_kv_or_json(os.environ.get("FLMCP_LS_VARIANTS", ""))
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
class ActivateIn(BaseModel):
|
|
|
|
| 300 |
"seats_allowed": lic["seats_allowed"],
|
| 301 |
}
|
| 302 |
out["devices"] = accounts.devices(conn, lic["id"])
|
| 303 |
+
unread = conn.execute(
|
| 304 |
+
"SELECT id, payload FROM outbox WHERE account_id=? AND kind='license_key' AND sent_at IS NULL ORDER BY id DESC LIMIT 1",
|
| 305 |
+
(aid,),
|
| 306 |
+
).fetchone()
|
| 307 |
+
if unread is not None:
|
| 308 |
+
try:
|
| 309 |
+
payload = json.loads(unread["payload"])
|
| 310 |
+
if isinstance(payload, dict) and payload.get("key"):
|
| 311 |
+
out["key"] = payload["key"]
|
| 312 |
+
conn.execute("UPDATE outbox SET sent_at=? WHERE id=?", (now, unread["id"]))
|
| 313 |
+
except Exception:
|
| 314 |
+
pass
|
| 315 |
return out
|
| 316 |
|
| 317 |
|
app/services/flmcp/auth.py
CHANGED
|
@@ -23,8 +23,14 @@ class AuthUser:
|
|
| 23 |
email: str
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
def _supabase_url() -> str:
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
@lru_cache(maxsize=1)
|
|
|
|
| 23 |
email: str
|
| 24 |
|
| 25 |
|
| 26 |
+
DEFAULT_SUPABASE_URL = "https://ijtmlchjfbxgmypewvfg.supabase.co"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
def _supabase_url() -> str:
|
| 30 |
+
raw = os.environ.get("SUPABASE_URL", DEFAULT_SUPABASE_URL).strip().rstrip("/")
|
| 31 |
+
if not raw or any(tok in raw.lower() for tok in ("placeholder", "your-", "change_me")):
|
| 32 |
+
return DEFAULT_SUPABASE_URL
|
| 33 |
+
return raw
|
| 34 |
|
| 35 |
|
| 36 |
@lru_cache(maxsize=1)
|
app/services/flmcp/db.py
CHANGED
|
@@ -293,27 +293,36 @@ class DB:
|
|
| 293 |
clock: Callable[[], float] = time.time,
|
| 294 |
) -> None:
|
| 295 |
self.clock = clock
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
if self.is_postgres:
|
| 302 |
try:
|
| 303 |
import psycopg2
|
| 304 |
import psycopg2.extras
|
|
|
|
| 305 |
self._pg_module = psycopg2
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
Path(self.path).parent.mkdir(parents=True, exist_ok=True)
|
| 316 |
-
self._init_sqlite()
|
| 317 |
|
| 318 |
def _init_sqlite(self) -> None:
|
| 319 |
conn = sqlite3.connect(self.path, timeout=15)
|
|
@@ -331,8 +340,7 @@ class DB:
|
|
| 331 |
conn.close()
|
| 332 |
|
| 333 |
def _init_postgres(self) -> None:
|
| 334 |
-
|
| 335 |
-
conn = self._pg_module.connect(self.database_url)
|
| 336 |
try:
|
| 337 |
with conn.cursor() as cur:
|
| 338 |
cur.execute(PG_SCHEMA)
|
|
|
|
| 293 |
clock: Callable[[], float] = time.time,
|
| 294 |
) -> None:
|
| 295 |
self.clock = clock
|
| 296 |
+
raw_url = (os.environ.get("DATABASE_URL") or "").strip()
|
| 297 |
+
is_placeholder = any(
|
| 298 |
+
token in raw_url.upper()
|
| 299 |
+
for token in ("YOUR_", "CHANGE_ME", "PLACEHOLDER", "[PASSWORD]", "[YOUR-PASSWORD]")
|
| 300 |
+
)
|
| 301 |
+
self.database_url = "" if is_placeholder else raw_url
|
| 302 |
+
self.is_postgres = bool(
|
| 303 |
+
self.database_url
|
| 304 |
+
and (
|
| 305 |
+
self.database_url.startswith("postgres://")
|
| 306 |
+
or self.database_url.startswith("postgresql://")
|
| 307 |
+
)
|
| 308 |
+
)
|
| 309 |
+
self.path = db_target or default_db_path()
|
| 310 |
|
| 311 |
if self.is_postgres:
|
| 312 |
try:
|
| 313 |
import psycopg2
|
| 314 |
import psycopg2.extras
|
| 315 |
+
|
| 316 |
self._pg_module = psycopg2
|
| 317 |
+
self._init_postgres()
|
| 318 |
+
return
|
| 319 |
+
except Exception:
|
| 320 |
+
# Graceful fallback to SQLite so placeholder or temporarily unreachable
|
| 321 |
+
# DATABASE_URL values never crash the backend service.
|
| 322 |
+
self.is_postgres = False
|
| 323 |
+
|
| 324 |
+
Path(self.path).parent.mkdir(parents=True, exist_ok=True)
|
| 325 |
+
self._init_sqlite()
|
|
|
|
|
|
|
| 326 |
|
| 327 |
def _init_sqlite(self) -> None:
|
| 328 |
conn = sqlite3.connect(self.path, timeout=15)
|
|
|
|
| 340 |
conn.close()
|
| 341 |
|
| 342 |
def _init_postgres(self) -> None:
|
| 343 |
+
conn = self._pg_module.connect(self.database_url, connect_timeout=5)
|
|
|
|
| 344 |
try:
|
| 345 |
with conn.cursor() as cur:
|
| 346 |
cur.execute(PG_SCHEMA)
|
app/services/flmcp/plans.py
CHANGED
|
@@ -35,12 +35,18 @@ DEFAULT = {
|
|
| 35 |
"pro": {"credits": 300, "devices": 2},
|
| 36 |
"studio": {"credits": 1200, "devices": 5},
|
| 37 |
},
|
| 38 |
-
"packs": {"pack_100": 100, "pack_500": 500},
|
| 39 |
}
|
| 40 |
|
| 41 |
|
| 42 |
def load(path: str | Path | None = None) -> Config:
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
costs, packs = dict(raw["costs"]), dict(raw["packs"])
|
| 45 |
plans = {name: Plan(**p) for name, p in raw["plans"].items()}
|
| 46 |
if not costs or any(not isinstance(v, int) or v < 1 for v in costs.values()):
|
|
|
|
| 35 |
"pro": {"credits": 300, "devices": 2},
|
| 36 |
"studio": {"credits": 1200, "devices": 5},
|
| 37 |
},
|
| 38 |
+
"packs": {"pack_100": 100, "pack100": 100, "pack_500": 500, "pack500": 500},
|
| 39 |
}
|
| 40 |
|
| 41 |
|
| 42 |
def load(path: str | Path | None = None) -> Config:
|
| 43 |
+
extra: dict = {}
|
| 44 |
+
if path and Path(path).is_file():
|
| 45 |
+
try:
|
| 46 |
+
extra = json.loads(Path(path).read_text("utf-8"))
|
| 47 |
+
except Exception:
|
| 48 |
+
extra = {}
|
| 49 |
+
raw = {**DEFAULT, **extra}
|
| 50 |
costs, packs = dict(raw["costs"]), dict(raw["packs"])
|
| 51 |
plans = {name: Plan(**p) for name, p in raw["plans"].items()}
|
| 52 |
if not costs or any(not isinstance(v, int) or v < 1 for v in costs.values()):
|