feat(api): add public RESTful API with API key auth and documentation page

This commit is contained in:
2026-07-07 09:22:11 +08:00
parent 14c226d082
commit 6a49fedfbb
9 changed files with 910 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ class User(Base):
sessions = relationship("UserSession", back_populates="user", cascade="all, delete-orphan")
invite_codes = relationship("InviteCode", back_populates="creator", foreign_keys="InviteCode.created_by", cascade="all, delete-orphan")
invited_by_code = relationship("InviteCode", foreign_keys=[invited_by_code_id])
api_keys = relationship("ApiKey", back_populates="user", cascade="all, delete-orphan")
class InviteCode(Base):
@@ -74,7 +75,7 @@ class Postcard(Base):
id = Column(Integer, primary_key=True, index=True)
profile_id = Column(Integer, ForeignKey("profiles.id", ondelete="CASCADE"), nullable=False)
card_number = Column(String(64), nullable=False)
card_number = Column(String(64), nullable=False, unique=True)
status = Column(String(16), nullable=False)
# sent & delivered
recipient_name = Column(String(64))
@@ -98,3 +99,17 @@ class Postcard(Base):
@validates("country")
def _upper_country(self, key, value):
return value.strip().upper() if value else None
class ApiKey(Base):
__tablename__ = "api_keys"
id = Column(Integer, primary_key=True, index=True)
key = Column(String(64), unique=True, nullable=False, index=True)
name = Column(String(64), nullable=False, default="")
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
is_active = Column(Boolean, nullable=False, default=True)
created_at = Column(DateTime, default=_utcnow)
last_used_at = Column(DateTime, nullable=True)
user = relationship("User", back_populates="api_keys")