webgo

A gin-like simple golang web framework.

A simple Web framework based on Golang, the implementation of static and dynamic route mapping, group mapping, static file transfer, template rendering.

Getting Start

Support dynamic and static routing, group routing configuration, as well as static file acquisition, template rendering page.


Static routes:

func main() {
    s := server.InitServer()
    s.Get("/level1/level2", func(c *server.Context) {
		  c.JSON(http.StatusOK, server.Content{
        "username": "yanyibin",
        "password": "yyb",
      })
    })
    s.Run("localhost:9999")
}
Copy the code

Dynamic routing:

The dynamic route name can be obtained from PathParams in the Context

Use trie tree to achieve

func main() {
    s := server.InitServer()
    s.Get("/level1/:v1", func(c *server.Context) {
      c.JSON(http.StatusOK, server.Content{
        "pathParam": c.PathParams["v1"]
        "username": "yanyibin",
        "password": "yyb",
      })
    })
    s.Run("localhost:9999")
}
Copy the code

Packet routing:

func main() {
    s := server.InitServer()
    g := s.SetGroup("/group1")
    {
        g.Get("/level1/:v1", func(c *server.Context) {
          c.JSON(http.StatusOK, server.Content{
            "pathParam": c.PathParams["v1"]
            "username": "yanyibin",
            "password": "yyb",
          })
        })
        
        g.Get("/level2/:v2", func(c *server.Context) {
          c.JSON(http.StatusOK, server.Content{
            "pathParam": c.PathParams["v2"]
            "username": "yanyibin",
            "password": "yyb",
          })
        })
    }
    s.Run("localhost:9999")
}
Copy the code

Static file access, template rendering:

To obtain the test. TMPL page, run localhost:9999/student.

type student struct {
	Name string
	Age  int
}

func main() {
    s := server.InitServer()
    s.LoadTemplate("test/templates/*")
    s.StaticResource("/static/css", "test/static")
    
    s1 := &student{Name: "yanyibin", Age: 23}
	  s2 := &student{Name: "ty", Age: 23}
    
    s.Get("/student", func(c *server.Context) {
      c.HTML(http.StatusOK, "test.tmpl", server.Content{
        "title":    "yanyibin",
        "students": [2]*student{s1, s2},
      })
    })
    s.Run("localhost:9999")
}
Copy the code

The directory structure

Directory Structure Description

.├ ── Server │ ├─.go │ ├─.go │ ├─.go │ ├─.go │ ├─.go │ ├─.go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go │.Go Server. Go / / service | ├ ─ ─ util | | ─ ─ string. Go / / string processing tools | | ─ ─ trie. Go / / implementation of dynamic routing trie tree | ├ ─ ─ for testing the static files of the test / / package | | ─ ─ The static / / js & CSS | | ─ ─ templates / / TMPL template | ├ ─ ─ test. Go / / test to start the classCopy the code

GitHub project address

View Github