czasg / pywss

一个轻量级的 Python Web 框架
https://czasg.github.io/pywss/
MIT License
96 stars 18 forks source link

怎么设置跨域? #132

Closed EaxMov closed 1 year ago

czasg commented 1 year ago
import pywss

def cors(ctx: pywss.Context):
    if ctx.method == pywss.MethodOptions:
        ctx.set_header("Access-Control-Allow-Origin", "*")
        ctx.set_header("Access-Control-Expose-Headers", "*")
        ctx.set_header("Access-Control-Allow-Credentials", "true")
        ctx.set_header("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS")
        return
    ctx.next()

if __name__ == '__main__':
    app = pywss.App()
    app.use(cors)  # 注册全局中间件
    app.options("*", lambda ctx: None)  # 注册options路由
    app.run()