Many Chanegs
parent
501e0765d3
commit
b2ce0b1828
|
|
@ -0,0 +1,3 @@
|
||||||
|
# .env file
|
||||||
|
|
||||||
|
API_KEY="^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8"
|
||||||
|
|
@ -3,7 +3,7 @@ from pydantic import BaseModel
|
||||||
class CL_Ping_Set(BaseModel):
|
class CL_Ping_Set(BaseModel):
|
||||||
## Test
|
## Test
|
||||||
name: str
|
name: str
|
||||||
ipadress: str
|
ipaddress: str
|
||||||
|
|
||||||
|
|
||||||
class CL_Database_get(BaseModel):
|
class CL_Database_get(BaseModel):
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
from fastapi.security.api_key import APIKeyHeader
|
||||||
|
from fastapi import Security, HTTPException, Depends
|
||||||
|
from starlette.status import HTTP_403_FORBIDDEN
|
||||||
|
from pydantic_settings import BaseSettings
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
api_key_header = APIKeyHeader(name="access_token", auto_error=False)
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
API_KEY: str
|
||||||
|
class Config:
|
||||||
|
env_file = ".env"
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
|
def get_settings():
|
||||||
|
return Settings()
|
||||||
|
|
||||||
|
async def get_api_key(settings: Settings = Depends(get_settings), api_key_header: str = Security(api_key_header)):
|
||||||
|
print(settings)
|
||||||
|
if api_key_header == settings.API_KEY:
|
||||||
|
return api_key_header
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTP_403_FORBIDDEN, detail="Could not validate API KEY"
|
||||||
|
)
|
||||||
10
main.py
10
main.py
|
|
@ -1,10 +1,12 @@
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI,Depends
|
||||||
from pydantic import BaseModel
|
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import Classes.main_classes as CL ## Importiert Classes
|
import Classes.main_classes as CL ## Importiert Classes
|
||||||
import uvicorn
|
import uvicorn
|
||||||
import logging
|
import logging
|
||||||
|
import auth
|
||||||
|
from fastapi.security.api_key import APIKey
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG) # add this line
|
logging.basicConfig(level=logging.DEBUG) # add this line
|
||||||
logger = logging.getLogger("foo")
|
logger = logging.getLogger("foo")
|
||||||
|
|
@ -49,8 +51,8 @@ def database(query,name,user):
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@app.post("/ping")
|
@app.post("/ping")
|
||||||
async def ping(ping: CL.CL_Ping_Set):
|
async def ping(ping: CL.CL_Ping_Set,api_key: APIKey = Depends(auth.get_api_key)):
|
||||||
database(f"INSERT INTO `Ping-Server` VALUES ('{datetime.now()}','{ping.name}','{ping.ipadress}')","Stines-GmbH","")
|
database(f"INSERT INTO `Ping-Server` VALUES ('{datetime.now()}','{ping.name}','{ping.ipaddress}')","Stines-GmbH","")
|
||||||
return f"ADD Ping from {ping.name} to Server"
|
return f"ADD Ping from {ping.name} to Server"
|
||||||
|
|
||||||
## Get Database from Server
|
## Get Database from Server
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue