coolair
V2EX  ›  问与答

Go 有类似 Django 的 DRF, FastAPI 的 fastapi-crudrouter 之类的东西吗?

  •  1
     
  •   coolair · Sep 8, 2021 · 1237 views
    This topic created in 1708 days ago, the information mentioned may be changed or developed.

    DRF

    fastapi-crudrouter

    我看了下别人用 go 写的各种系统,都是 crud 四个方法都得写。

    看 reddit,有个哥们的想法去实现了 DRF 的那套玩意:

    func main() {
        r := rest.New()
        userViewSet := ViewSet { Type: reflect.TypeOf(&User{}) }
        r.REST("/users", userViewSet)
    }
    
    func REST(path string, viewSet ViewSet) {
        group := e.Group(path)
        g.Use(rest.ViewSetMiddleware(viewSet))
        g.GET("/", rest.listHandler)
        g.GET("/:id", rest.GetHandler)
        // and so on
    }
    
    func ViewSetMiddleware(viewSet ViewSet) {
        return func(next echo.HandlerFunc) echo.HandlerFunc {
            return func(c echo.Context) error {
                //stuff the view set onto the context
            }
        }
    }
    
    func GetHandler(c echo.Context) error {
        myStruct := reflect.New(c.GetViewContext().Type)
        return c.JSON(db.First(&myStruct, c.Param("id")))
    }
    
    
    2 replies    2021-09-10 15:59:29 +08:00
    jenlors
        1
    jenlors  
       Sep 9, 2021
    倒是整了个跟 FastAPI 一样的 Swagger 文档生成: https://github.com/long2ice/swagin
    cyrivlclth
        2
    cyrivlclth  
       Sep 10, 2021
    你是说代码生成器?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1310 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 23:53 · PVG 07:53 · LAX 16:53 · JFK 19:53
    ♥ Do have faith in what you're doing.