This commit is contained in:
Lev 2025-11-26 17:34:49 +03:00
parent 8482aafcfa
commit 7f80451aab
8 changed files with 72 additions and 1 deletions

23
API/main.py Normal file
View file

@ -0,0 +1,23 @@
import configparser
import fastapi
from fastapi import HTTPException
import utils
config = configparser.ConfigParser()
config.read('../configs/settings.conf', encoding='utf-8')
path = config['database']['path']
DB = utils.DataBase(path=path)
app = fastapi.FastAPI()
@app.post("/web/registration/")
def reg(email: str, passwd: str) :
code = DB.register_site(email, passwd)
if code == 0 :
return 200
else :
raise HTTPException(
status_code=400,
detail="Этот email уже зарегистрирован"
)

45
API/utils.py Normal file
View file

@ -0,0 +1,45 @@
import sqlite3
import hashlib
class DataBase :
def __init__(self, path: str) :
self.connect = sqlite3.connect(path)
self.connect.row_factory = sqlite3.Row
self.cursor = self.connect.cursor()
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS users_site (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
created TEXT DEFAULT CURRENT_TIMESTAMP,
agreement_accepted TEXT DEFAULT CURRENT_TIMESTAMP,
counts_url INTEGER DEFAULT 0,
data_pay TEXT
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS users_tg (
id INTEGER PRIMARY KEY AUTOINCREMENT,
id_tg INTEGER NOT NULL UNIQUE,
agreement_accepted TEXT DEFAULT CURRENT_TIMESTAMP,
counts_url INTEGER DEFAULT 0,
data_pay TEXT
)
''')
self.connect.commit()
def register_site(self, email: str, password: str) -> int :
"""
Status code:
0 - Success
1 - Email is bussy
"""
try :
self.cursor.execute('''
INSERT INTO users_site (email, password_hash)
VALUES (?, ?)
''', (email.lower().strip(), hashlib.sha256(password.encode()).hexdigest()))
self.connect.commit()
return 0
except sqlite3.IntegrityError :
return 1