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.

65 lines
2.6 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
10 months ago
11 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
11 months ago
  1. from datetime import timedelta
  2. from fastapi.security import OAuth2PasswordRequestForm
  3. from fastapi import Depends, FastAPI, HTTPException, status
  4. from dependencies import *
  5. from internal.models import Token
  6. from fastapi.middleware.cors import CORSMiddleware
  7. from routers import usermanage,typemanage,blogmanage
  8. app=FastAPI()
  9. app.include_router(usermanage.router)
  10. app.include_router(typemanage.router)
  11. app.include_router(blogmanage.router)
  12. app.add_middleware(
  13. CORSMiddleware,
  14. allow_origins=['http://111.229.38.129'],
  15. allow_credentials=True,
  16. allow_methods=['GET', 'POST','DELETE','PUT'],
  17. allow_headers=['Authorization', 'Content-Type'],
  18. )
  19. # # 用户登录
  20. # @app.post("/token", response_model=Token)
  21. # async def login_for_access_token(
  22. # form_data: OAuth2PasswordRequestForm = Depends(),
  23. # ) -> Token:
  24. # user = authenticate_user(form_data.username, form_data.password)
  25. # if not user:
  26. # raise HTTPException(
  27. # status_code=status.HTTP_401_UNAUTHORIZED,
  28. # detail="Incorrect username or password",
  29. # headers={"WWW-Authenticate": "Bearer"},
  30. # )
  31. # access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  32. # access_token = create_access_token(
  33. # data={"sub": user.username}, expires_delta=access_token_expires
  34. # )
  35. # return {"access_token": access_token, "token_type": "bearer"}
  36. # 注册新用户
  37. @app.post("/register/")
  38. async def register_user(user: UserInDB):
  39. # 检查用户名是否已经存在
  40. existing_user = get_user(user.username)
  41. if existing_user:
  42. raise HTTPException(status_code=400, detail="Username already registered")
  43. if not user.hashed_password:
  44. raise HTTPException(status_code=400,detail="password cannot be empty")
  45. # 创建新用户并保存到数据库
  46. hashed_password = get_password_hash(user.hashed_password)
  47. insert_query = "INSERT INTO users (username, email, full_name, hashed_password, disabled) VALUES (%s, %s, %s, %s, %s)"
  48. user_data = (user.username, user.email, user.full_name, hashed_password, user.disabled)
  49. execute_query(insert_query, user_data)
  50. # 返回创建的用户信息
  51. return {"status":status.HTTP_200_OK,"message":"users create successfully!"}
  52. @app.get("/users/me/items/")
  53. async def read_own_items(current_user: User = Depends(get_current_active_user)):
  54. return [{"item_id": "Foo", "owner": current_user.username}]
  55. # @app.get("/list",response_model=list[BlogList])
  56. # def read_type_all():
  57. # select_query="SELECT blogname,blogtype,addtime,descr FROM blogs;"
  58. # result=execute_query(select_query,fetchall=True)
  59. # return result