Commit 5ea6c94e by Karsa Zoltán István

refactoring

parent 4cf44483
secret=ASLKDHSADKJFLKSJDFKJSDLFKSJ secret=ASLKDHSADKJFLKSJDFKJSDLFKSJ
algorithm=HS256 algorithm=HS256
\ No newline at end of file ADMIN_PASS=12345678
\ No newline at end of file
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
.env .env
.ruff_cache/ .ruff_cache/
.env
# Packaging files: # Packaging files:
*.egg* *.egg*
......
from pydantic import BaseModel, EmailStr from pydantic import BaseModel, EmailStr, Field
class User(BaseModel): class User(BaseModel):
...@@ -7,10 +7,6 @@ class User(BaseModel): ...@@ -7,10 +7,6 @@ class User(BaseModel):
password: str password: str
class DataCenter(BaseModel):
name: str
class Token(BaseModel): class Token(BaseModel):
datacenter: str datacenter: str
token: str token: str
...@@ -22,3 +18,9 @@ class UserLoginSchema(BaseModel): ...@@ -22,3 +18,9 @@ class UserLoginSchema(BaseModel):
class Config: class Config:
schema_extra = {"example": {"username": "user", "password": "weakpassword"}} schema_extra = {"example": {"username": "user", "password": "weakpassword"}}
class DataCenterSchema(BaseModel):
name: str = Field(min_length=3, max_length=50)
url: str
active: bool = False
weight: float = 1.0
\ No newline at end of file
secret=
algorithm=
ADMIN_PASS=
\ No newline at end of file
from fastapi import FastAPI, Response, Body, Depends from fastapi import FastAPI, Response, Body, Depends
import os
from typing import List from typing import List
from balancer.util import proxy_datacenters from balancer.util import proxy_datacenters
from sredis.sredis import check_user, create_puser, add_datacenter, set_token from sredis.sredis import check_user, create_puser, add_datacenter, set_token, create_superpuser
import logging import logging
import requests import requests
from core.models import User, DataCenter, Token, UserLoginSchema from core.models import User, Token, UserLoginSchema, DataCenterSchema
from core.auth import signJWT from core.auth import signJWT
from core.bearer import get_current_user, admin_user from core.bearer import get_current_user, admin_user
from redis_om import Migrator from redis_om import Migrator
...@@ -20,6 +21,9 @@ app = FastAPI() ...@@ -20,6 +21,9 @@ app = FastAPI()
add_datacenter("https://kappa1.fured.cloud.bme.hu") add_datacenter("https://kappa1.fured.cloud.bme.hu")
add_datacenter("https://kappa2.fured.cloud.bme.hu") add_datacenter("https://kappa2.fured.cloud.bme.hu")
add_datacenter("https://kappa3.fured.cloud.bme.hu") add_datacenter("https://kappa3.fured.cloud.bme.hu")
create_superpuser(
User(username='admin', email='example@domain.com', password=os.getenv('ADMIN_PASS'))
)
@app.post("/user/signup", tags=["user"]) @app.post("/user/signup", tags=["user"])
...@@ -46,8 +50,8 @@ def proxy_post(server_path: str = "/", username=Depends(get_current_user)): ...@@ -46,8 +50,8 @@ def proxy_post(server_path: str = "/", username=Depends(get_current_user)):
@app.post("/add_datacenter/") @app.post("/add_datacenter/")
def create_datacenter(dc: DataCenter = None, username=Depends(admin_user)): def create_datacenter(dc: DataCenterSchema = None, username=Depends(admin_user)):
add_datacenter(dc.name) add_datacenter(dc.url)
return Response(status_code=201) return Response(status_code=201)
......
...@@ -26,7 +26,7 @@ shell = "poetry run uvicorn main:app --reload --port 6973 --host 0.0.0.0" ...@@ -26,7 +26,7 @@ shell = "poetry run uvicorn main:app --reload --port 6973 --host 0.0.0.0"
help = "Start the microservice on port 6973" help = "Start the microservice on port 6973"
[tool.poe.tasks.prod] [tool.poe.tasks.prod]
shell = "poetry run uvicorn main:app --host 0.0.0.0 --port 8080 --workers 4 --log-level critical" shell = "poetry run uvicorn main:app --host 0.0.0.0 --port 8080 --workers 8 --log-level critical"
help = "Start the prod deployment" help = "Start the prod deployment"
[tool.poe.tasks.lint] [tool.poe.tasks.lint]
......
...@@ -6,4 +6,9 @@ class PUser(HashModel): ...@@ -6,4 +6,9 @@ class PUser(HashModel):
username: str = Field(index=True) username: str = Field(index=True)
email: EmailStr email: EmailStr
password: str password: str
admin: bool = False admin: int = 0
class DataCenter(HashModel):
name: str = Field(index=True)
url: str = Field(index=True)
active: bool = Field(index=True)
\ No newline at end of file
...@@ -2,9 +2,10 @@ import redis ...@@ -2,9 +2,10 @@ import redis
from core.models import UserLoginSchema from core.models import UserLoginSchema
from .models import PUser from .models import PUser
from passlib.hash import pbkdf2_sha256 from passlib.hash import pbkdf2_sha256
from core.models import User from core.models import User, DataCenterSchema
from core.auth import hash_pass from core.auth import hash_pass
from fastapi import HTTPException from fastapi import HTTPException
import random
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True) r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
r.set("datacenters_cnt", 1) r.set("datacenters_cnt", 1)
...@@ -19,7 +20,6 @@ def add_datacenter(datacenter: str): ...@@ -19,7 +20,6 @@ def add_datacenter(datacenter: str):
r.incr("datacenters_cnt") r.incr("datacenters_cnt")
r.set("roundrobin_cnt", 1) r.set("roundrobin_cnt", 1)
def rr_get(): def rr_get():
cnt = int(r.get("datacenters_cnt")) cnt = int(r.get("datacenters_cnt"))
rr = int(r.get("roundrobin_cnt")) rr = int(r.get("roundrobin_cnt"))
...@@ -29,6 +29,10 @@ def rr_get(): ...@@ -29,6 +29,10 @@ def rr_get():
r.incr("roundrobin_cnt") r.incr("roundrobin_cnt")
return str(r.hget("datacenters_hash", rr)) return str(r.hget("datacenters_hash", rr))
def wr_get(centers: DataCenterSchema):
cnt = int(r.get("datacenters_cnt"))
indexis = range(1, cnt+1)
return random.choice(indexis, weights=())[0]
def set_token(username: str, datacenter: str, token: str): def set_token(username: str, datacenter: str, token: str):
print(f"tokens:{username}" + datacenter) print(f"tokens:{username}" + datacenter)
...@@ -54,3 +58,14 @@ def create_puser(user: User): ...@@ -54,3 +58,14 @@ def create_puser(user: User):
username=user.username, email=user.email, password=hash_pass(user.password) username=user.username, email=user.email, password=hash_pass(user.password)
) )
user.save() user.save()
def create_superpuser(user: User):
s = PUser.find(PUser.username == user.username).all()
if not s:
puser = PUser(
username=user.username, email=user.email, password=hash_pass(user.password), admin=1
)
puser.save()
return True
return False
\ No newline at end of file
import json import json
from locust import TaskSet, task, between, HttpUser from locust import TaskSet, task, HttpUser
class PerformanceTests(TaskSet): class PerformanceTests(TaskSet):
def on_start(self): def on_start(self):
...@@ -15,6 +15,4 @@ class PerformanceTests(TaskSet): ...@@ -15,6 +15,4 @@ class PerformanceTests(TaskSet):
class WebsiteUser(HttpUser): class WebsiteUser(HttpUser):
tasks = [PerformanceTests] tasks = [PerformanceTests]
min_wait = 5000 \ No newline at end of file
max_wait = 9000
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment