feat: 카드 오른쪽 위 X 버튼으로 링크 삭제
- DELETE /links/<id>: .env 의 DELETE_TOKEN 과 X-Delete-Token 헤더가 맞을 때만 삭제 - 처음 삭제할 때 관리 키를 묻고 브라우저에 기억, 틀리면 다시 묻는다 - 정적 파일에 버전 쿼리를 붙여 캐시된 옛 화면이 남지 않게 한다 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
29
app.py
29
app.py
@@ -1,3 +1,4 @@
|
||||
import hmac
|
||||
import ipaddress
|
||||
import os
|
||||
import re
|
||||
@@ -39,6 +40,8 @@ PLACEHOLDER_DATA_URI = (
|
||||
DEFAULT_PAGE_SIZE = int(os.getenv("DEFAULT_PAGE_SIZE", "30"))
|
||||
MAX_PAGE_SIZE = int(os.getenv("MAX_PAGE_SIZE", "60"))
|
||||
DEFAULT_SCHEMA = os.getenv("DB_SCHEMA", "public")
|
||||
# 카드 삭제용 관리 키(.env). 비어 있으면 삭제 기능을 막는다.
|
||||
DELETE_TOKEN = os.getenv("DELETE_TOKEN", "")
|
||||
|
||||
|
||||
def _safe_identifier(name: str) -> str:
|
||||
@@ -515,5 +518,31 @@ def add_link():
|
||||
)
|
||||
|
||||
|
||||
@app.delete("/links/<int:link_id>")
|
||||
def delete_link(link_id: int):
|
||||
# 로그인이 없는 공개 페이지라 관리 키(X-Delete-Token)가 맞을 때만 지운다
|
||||
if not DELETE_TOKEN:
|
||||
return jsonify({"error": "삭제 기능이 설정되지 않았습니다."}), 403
|
||||
token = (request.headers.get("X-Delete-Token") or "").strip()
|
||||
if not hmac.compare_digest(token.encode(), DELETE_TOKEN.encode()):
|
||||
return jsonify({"error": "관리 키가 맞지 않습니다."}), 401
|
||||
|
||||
table_ref = _table_ref(os.getenv("DB_SCHEMA", DEFAULT_SCHEMA), os.getenv("TABLE", "news_link"))
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(f"DELETE FROM {table_ref} WHERE id = %s RETURNING url", (link_id,))
|
||||
row = cur.fetchone()
|
||||
conn.commit()
|
||||
except Exception as exc:
|
||||
return jsonify({"error": "삭제 실패", "detail": str(exc)}), 500
|
||||
if not row:
|
||||
return jsonify({"error": "이미 삭제되었거나 없는 링크입니다."}), 404
|
||||
|
||||
_, ip = get_request_identity(request)
|
||||
app.logger.warning("링크 삭제 id=%s url=%s ip=%s", link_id, row[0], ip)
|
||||
return jsonify({"deleted": link_id})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="127.0.0.1", port=8021, debug=False)
|
||||
|
||||
Reference in New Issue
Block a user