Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel, conint
|
| 3 |
import threading
|
|
@@ -7,12 +63,13 @@ app = FastAPI()
|
|
| 7 |
live_names = {}
|
| 8 |
expiration_times = {}
|
| 9 |
|
| 10 |
-
class LiveNameRequest(BaseModel):
|
| 11 |
name: str
|
| 12 |
url: str
|
| 13 |
timeout: conint(ge=60, le=3600)
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
time.sleep(expiration_times[name] - time.time())
|
| 17 |
if name in live_names:
|
| 18 |
del live_names[name]
|
|
@@ -20,8 +77,8 @@ def remove_expired_name(name: str):
|
|
| 20 |
print(f"{name} has expired and is available again!")
|
| 21 |
|
| 22 |
@app.post("/set_live_name")
|
| 23 |
-
def set_live_name(data: LiveNameRequest):
|
| 24 |
-
if data.name in live_names:
|
| 25 |
remaining_time = max(0, int((expiration_times[data.name] - time.time()) / 60))
|
| 26 |
return {"message": f"Name taken. Available in {remaining_time} min(s)."}
|
| 27 |
|
|
@@ -34,8 +91,7 @@ def set_live_name(data: LiveNameRequest):
|
|
| 34 |
"access_url": f"/lookup/{data.name}"}
|
| 35 |
|
| 36 |
@app.get("/lookup/name")
|
| 37 |
-
def lookup_name(c: str):
|
| 38 |
-
if c in live_names:
|
| 39 |
return {"url": live_names[c]}
|
| 40 |
-
|
| 41 |
-
raise HTTPException(status_code=404, detail="Live name not found")
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
+
from functools import wraps
|
| 3 |
+
from typing import get_type_hints
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def strict_types(func):
|
| 7 |
+
sig = inspect.signature(func)
|
| 8 |
+
type_hints = get_type_hints(func)
|
| 9 |
+
|
| 10 |
+
@wraps(func)
|
| 11 |
+
def wrapper(*args, **kwargs):
|
| 12 |
+
bound_args = sig.bind(*args, **kwargs)
|
| 13 |
+
bound_args.apply_defaults()
|
| 14 |
+
|
| 15 |
+
# Check argument types
|
| 16 |
+
for name, value in bound_args.arguments.items():
|
| 17 |
+
expected_type = type_hints.get(name)
|
| 18 |
+
if expected_type and not isinstance(value, expected_type):
|
| 19 |
+
raise TypeError(
|
| 20 |
+
f"Argument '{name}' expected {
|
| 21 |
+
expected_type.__name__}, got {
|
| 22 |
+
type(value).__name__}")
|
| 23 |
+
|
| 24 |
+
# Call the function
|
| 25 |
+
result = func(*args, **kwargs)
|
| 26 |
+
|
| 27 |
+
# Check return type
|
| 28 |
+
expected_return = type_hints.get('return')
|
| 29 |
+
if expected_return and not isinstance(result, expected_return):
|
| 30 |
+
raise TypeError(
|
| 31 |
+
f"Return value expected {
|
| 32 |
+
expected_return.__name__}, got {
|
| 33 |
+
type(result).__name__}")
|
| 34 |
+
|
| 35 |
+
return result
|
| 36 |
+
|
| 37 |
+
return wrapper
|
| 38 |
+
# main.repy
|
| 39 |
+
|
| 40 |
+
#
|
| 41 |
+
# eshare - Share sites easily
|
| 42 |
+
# Copyright (C) 2025 Rihaan Meher
|
| 43 |
+
# This program is free software: you can redistribute it and/or modify
|
| 44 |
+
# it under the terms of the GNU General Public License as published by
|
| 45 |
+
# the Free Software Foundation, either version 3 of the License, or
|
| 46 |
+
# (at your option) any later version.
|
| 47 |
+
#
|
| 48 |
+
# This program is distributed in the hope that it will be useful,
|
| 49 |
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 50 |
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 51 |
+
# GNU General Public License for more details.
|
| 52 |
+
#
|
| 53 |
+
# You should have received a copy of the GNU General Public License
|
| 54 |
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
| 55 |
+
#
|
| 56 |
+
|
| 57 |
from fastapi import FastAPI, HTTPException
|
| 58 |
from pydantic import BaseModel, conint
|
| 59 |
import threading
|
|
|
|
| 63 |
live_names = {}
|
| 64 |
expiration_times = {}
|
| 65 |
|
| 66 |
+
class LiveNameRequest(BaseModel) :
|
| 67 |
name: str
|
| 68 |
url: str
|
| 69 |
timeout: conint(ge=60, le=3600)
|
| 70 |
|
| 71 |
+
@strict_types
|
| 72 |
+
def remove_expired_name(name: str) :
|
| 73 |
time.sleep(expiration_times[name] - time.time())
|
| 74 |
if name in live_names:
|
| 75 |
del live_names[name]
|
|
|
|
| 77 |
print(f"{name} has expired and is available again!")
|
| 78 |
|
| 79 |
@app.post("/set_live_name")
|
| 80 |
+
def set_live_name(data: LiveNameRequest) :
|
| 81 |
+
if data.name in live_names :
|
| 82 |
remaining_time = max(0, int((expiration_times[data.name] - time.time()) / 60))
|
| 83 |
return {"message": f"Name taken. Available in {remaining_time} min(s)."}
|
| 84 |
|
|
|
|
| 91 |
"access_url": f"/lookup/{data.name}"}
|
| 92 |
|
| 93 |
@app.get("/lookup/name")
|
| 94 |
+
def lookup_name(c: str) :
|
| 95 |
+
if c in live_names :
|
| 96 |
return {"url": live_names[c]}
|
| 97 |
+
raise HTTPException(status_code=404, detail="Live name not found")
|
|
|