-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (28 loc) · 1.2 KB
/
main.py
File metadata and controls
34 lines (28 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from fastapi import FastAPI
from routers import news, users, favorite, history
app = FastAPI()
from utils.exception_handlers import register_exception_handlers
#注册异常处理函数
register_exception_handlers(app)
#需要添加CORS中间件,因为客户端和服务器在不同的域名下,需要允许跨域请求
#CORS是(Cross-Origin Resource Sharing)就是:浏览器用来“限制不同源之间请求”的安全机制。
#需要保证协议,域名,端口一致,才能跨域请求
from fastapi.middleware.cors import CORSMiddleware
origins = ["*"] #默认所有,后续要指定
allow_credentials = True
allow_methods = ["*"]
allow_headers = ["*"]
app.add_middleware(
CORSMiddleware, #中间件
allow_origins=origins, #允许的源,网址,所有
allow_credentials=allow_credentials, #允许携带cookie
allow_methods=allow_methods, #允许的方法,比如get,post,put,delete等
allow_headers=allow_headers #允许的请求头,token是在请求头里的
)
@app.get("/")
async def root():
return {"msg": "Hello World"}
app.include_router(news.router)
app.include_router(users.router)
app.include_router(favorite.router)
app.include_router(history.router)