Skip to content
/ gofly Public

用go语言封装实现的 简单Web框架 <学习使用>

License

Notifications You must be signed in to change notification settings

devhg/gofly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web FrameWork

It like gin

What's support

  • 支持上下文及多格式返回 Context
  • 动态路由 /doc/:lang
  • 分组控制路由 Group
  • 静态文件 StaticFS
  • 支持中间件 middleware
  • 支持模板引擎 html/templates
  • 支持错误恢复 Panic recover

Structure

How to use

just like

r := gofly.Default()

// 添加模板渲染函数
r.SetFuncMap(template.FuncMap{
    "formatAsDate": formatAsDate,
})
// 加载模板
r.LoadHtmlGlob("templates/*")

r.GET("/", func(c *gofly.Context) {
    c.HTML(http.StatusOK, "css.tmpl", nil)
})

r.GET("/students", func(c *gofly.Context) {
    c.HTML(http.StatusOK, "arr.tmpl", gofly.H{
        "title": "gofly demo",
        "stuArr": []*Student{
            {Name: "dev", Age: 11},
            {Name: "hui", Age: 22},
        },
    })
})

v1 := r.Group("/v1")
{
    v1.GET("/", func(c *gofly.Context) {
        c.HTML(http.StatusOK, "test_func.tmpl", gofly.H{
            "title": "test dateFormat",
            "now":   time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local),
        })
    })

    v1.GET("/hello", func(c *gofly.Context) {
        c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
    })
}

v2 := r.Group("/v2")
//v2.Use(middlewareForV1())
{
    v2.GET("/hello/:name", func(c *gofly.Context) {
        // expect /hello/hui
        c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
    })
    v2.POST("/login", func(c *gofly.Context) {
        c.JSON(http.StatusOK, gofly.H{
            "username": c.PostForm("username"),
            "password": c.PostForm("password"),
        })
    })

}

r.GET("/panic", func(c *gofly.Context) {
    names := []string{"devhui"}
    c.String(http.StatusOK, names[100])
})

r.Static("/assets", "./static")

log.Fatal(r.Run(":8081"))

Just for learning. 奥利给!!!

About

用go语言封装实现的 简单Web框架 <学习使用>

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published