Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rosbit/mgin
a lightweight web server framework like gin. 轻量级http服务框架
https://github.com/rosbit/mgin
framework golang http-server web-server
Last synced: about 6 hours ago
JSON representation
a lightweight web server framework like gin. 轻量级http服务框架
- Host: GitHub
- URL: https://github.com/rosbit/mgin
- Owner: rosbit
- License: apache-2.0
- Created: 2021-09-13T07:40:11.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-22T23:18:50.000Z (7 months ago)
- Last Synced: 2024-04-23T00:32:46.487Z (7 months ago)
- Topics: framework, golang, http-server, web-server
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mgin
A http framework to support router and argument processing
## Sample #1: handler with http.HandlerFunc
```go
package mainimport (
"github.com/rosbit/mgin"
"net/http"
"fmt"
)func main() {
r := mgin.NewMgin()r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
c := mgin.NewHttpContext(w, r)
c.String(http.StatusOK, "hello")
})r.Get("/json/:msg", func(w http.ResponseWriter, r *http.Request) {
c := mgin.NewHttpContext(w, r)
msg := c.Param("msg")
c.JSON(http.StatusOK, map[string]interface{} {
"code": http.StatusOK,
"msg": msg,
})
})r.Post("/json", func(w http.ResponseWriter, r *http.Request) {
c := mgin.NewHttpContext(w, r)
var i interface{}
code, err := c.ReadJSON(&i)
if err != nil {
c.Error(code, err.Error())
return
}
c.JSONPretty(http.StatusOK, i, " ")
})r.Get("/jump", func(w http.ResponseWriter, r *http.Request) {
c := mgin.NewHttpContext(w, r)
url := c.QueryParam("u")
if url == "" {
c.Error(http.StatusBadRequest, "argument u expected")
return
}
c.Redirect(http.StatusFound, url)
})r.Post("/form/:name", func(w http.ResponseWriter, r *http.Request) {
c := mgin.NewHttpContext(w, r)
n := c.Param("name")
v := c.FormValue(n)
c.String(http.StatusOK, fmt.Sprintf("value of %s: %s\n", n, v))
})r.Run()
// or r.Run(":8080")
// or http.ListenAndServe(":8080", r)
}
```## Sample #2: handler with argument mgin.Context
```go
package mainimport (
"github.com/rosbit/http-mgin"
"net/http"
"fmt"
)func main() {
r := mgin.NewMgin()r.GET("/hello", func(c *mgin.Context) {
c.String(http.StatusOK, "hello")
})r.GET("/json/:msg", func(c *mgin.Context) {
msg := c.Param("msg")
c.JSON(http.StatusOK, map[string]interface{} {
"code": http.StatusOK,
"msg": msg,
})
})r.POST("/json", func(c *mgin.Context) {
var i interface{}
code, err := c.ReadJSON(&i)
if err != nil {
c.Error(code, err.Error())
return
}
c.JSONPretty(http.StatusOK, i, " ")
})r.GET("/jump", func(c *mgin.Context) {
url := c.QueryParam("u")
if url == "" {
c.Error(http.StatusBadRequest, "argument u expected")
return
}
c.Redirect(http.StatusFound, url)
})r.POST("/form/:name", func(c *mgin.Context) {
n := c.Param("name")
v := c.FormValue(n)
c.String(http.StatusOK, fmt.Sprintf("value of %s: %s\n", n, v))
})r.Run()
// or r.Run(":8080")
// or http.ListenAndServe(":8080", r)
}
```