You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
975 B
37 lines
975 B
2 months ago
|
from math import floor
|
||
|
from random import uniform
|
||
|
from fastapi import FastAPI
|
||
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
import sqlalchemy
|
||
|
from sqlalchemy import text
|
||
|
|
||
|
app = FastAPI()
|
||
|
|
||
|
origins = ["http://localhost:5173"]
|
||
|
# noinspection PyTypeChecker
|
||
|
app.add_middleware(
|
||
|
CORSMiddleware,
|
||
|
allow_origins=origins,
|
||
|
allow_credentials=True,
|
||
|
allow_methods=["*"],
|
||
|
allow_headers=["*"],
|
||
|
)
|
||
|
|
||
|
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/BOOK_DEMO")
|
||
|
@app.get("/")
|
||
|
async def root():
|
||
|
return {"message": "Hello World"}
|
||
|
|
||
|
|
||
|
@app.get("/hello/{name}")
|
||
|
async def say_hello(name: str):
|
||
|
return {"message": f"Hello {name}"}
|
||
|
|
||
|
@app.get("/get_random_stu")
|
||
|
async def get_random_stu():
|
||
|
with engine.connect() as c:
|
||
|
stu = c.execute(text("SELECT sno, sname FROM stu_info")).fetchall()
|
||
|
size = stu.__len__()
|
||
|
return {"sno": stu[floor(uniform(0, size - 1))][0], "sname": stu[floor(uniform(0, size - 1))][1]}
|
||
|
|