Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
balancer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5ea6c94e
authored
Mar 22, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring
parent
4cf44483
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
54 additions
and
20 deletions
+54
-20
.env
+3
-2
.gitignore
+2
-0
core/models.py
+8
-5
envtemplate
+4
-0
main.py
+8
-4
pyproject.toml
+1
-1
sredis/models.py
+7
-1
sredis/sredis.py
+18
-2
stresstest/main.py
+3
-5
No files found.
.env
View file @
5ea6c94e
secret=ASLKDHSADKJFLKSJDFKJSDLFKSJ
secret=ASLKDHSADKJFLKSJDFKJSDLFKSJ
algorithm=HS256
algorithm=HS256
\ No newline at end of file
ADMIN_PASS=12345678
\ No newline at end of file
.gitignore
View file @
5ea6c94e
...
@@ -4,6 +4,8 @@
...
@@ -4,6 +4,8 @@
.env
.env
.ruff_cache/
.ruff_cache/
.env
# Packaging files:
# Packaging files:
*.egg*
*.egg*
...
...
core/models.py
View file @
5ea6c94e
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
envtemplate
0 → 100644
View file @
5ea6c94e
secret=
algorithm=
ADMIN_PASS=
\ No newline at end of file
main.py
View file @
5ea6c94e
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
,
UserLogin
Schema
from
core.models
import
User
,
Token
,
UserLoginSchema
,
DataCenter
Schema
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
:
DataCenter
Schema
=
None
,
username
=
Depends
(
admin_user
)):
add_datacenter
(
dc
.
name
)
add_datacenter
(
dc
.
url
)
return
Response
(
status_code
=
201
)
return
Response
(
status_code
=
201
)
...
...
pyproject.toml
View file @
5ea6c94e
...
@@ -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]
...
...
sredis/models.py
View file @
5ea6c94e
...
@@ -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
sredis/sredis.py
View file @
5ea6c94e
...
@@ -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
stresstest/main.py
View file @
5ea6c94e
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment