V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
coolair
V2EX  ›  问与答

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

  •  1
     
  •   coolair · 2021-09-08 22:58:58 +08:00 · 844 次点击
    这是一个创建于 994 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2021-09-10 15:59:29 +08:00
    jenlors
        1
    jenlors  
       2021-09-09 17:56:38 +08:00
    倒是整了个跟 FastAPI 一样的 Swagger 文档生成: https://github.com/long2ice/swagin
    cyrivlclth
        2
    cyrivlclth  
       2021-09-10 15:59:29 +08:00
    你是说代码生成器?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4969 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:55 · PVG 15:55 · LAX 00:55 · JFK 03:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.