GIN-BOOT
介紹
這是一個go的web項目框架(腳手架),可以讓你3分鐘內建立自己可用的Go Web項目窿冯。
功能描述
http請求入?yún)⒖梢宰詣犹畛涞浇Y構體骗奖,如果是POST,則將http body數(shù)據(jù)填充到結構體醒串; 如果是GET执桌,則將URL query參數(shù)自動填充到結構體;
返回數(shù)據(jù)厦凤,可以是任意數(shù)據(jù)類型鼻吮。如果數(shù)據(jù)不是boot.ApiResp,則返回數(shù)據(jù)會被包裝為boot.ApiResp的json數(shù)據(jù)较鼓;
統(tǒng)一全局異常,請求返回會被包裝為系統(tǒng)異常
返回json格式
{
? ? "code":"100200",
? ? "msg":"成功",?
? ? "data":null?
}
使用demo
package main
import (
? ? "errors"
? ? "github.com/gin-gonic/gin"
? ? "github.com/xiaojun207/gin-boot/boot"
? ? "log"
? ? "net/http"
)
type Foo struct {
? ? Username string `json:"username" form:"username" binding:"required"`
? ? Password string `json:"password" form:"password"`
}
type Page struct {
? ? boot.BindQuery? ? // 繼承boot.BindQuery的結構體违柏,指定綁定到url參數(shù)
? ? PageNum? ? ? ? int `json:"page_num" form:"page_num"`? // url 中的參數(shù)博烂,需要用tag 'form' 標識,才能自動綁定
? ? PageSize? ? ? int `json:"page_size" form:"page_size"` // url 中的參數(shù)漱竖,需要用tag 'form' 標識禽篱,才能自動綁定
}
type QueryHeader struct {
? ? boot.BindHeader
? ? Authorization string `header:"authorization"`
}
// /testPost
// 入?yún)⒖梢宰詣犹畛涞浇Y構體,如果是POST,則將http body數(shù)據(jù)填充到結構體馍惹;
// 返回數(shù)據(jù)躺率,可以是任意數(shù)據(jù)類型。如果數(shù)據(jù)不是boot.ApiResp万矾,則返回數(shù)據(jù)會被包裝為boot.ApiResp的json數(shù)據(jù)悼吱;<br>
// 如果handler執(zhí)行異常,請求返回會被包裝為系統(tǒng)異常
// 參數(shù)page繼承boot.BindQuery的結構體良狈,綁定url參數(shù)
func TestPost1Handler(c *gin.Context, req *Foo, page Page, header QueryHeader) boot.ApiResp {
? ? log.Println("TestPost1Handler.req:", req.Username, ",Password:", req.Password)
? ? log.Println("TestPost1Handler.page.PageNum:", page.PageNum, ",PageSize:", page.PageSize)
? ? log.Println("TestPost1Handler.Authorization:", header.Authorization)
? ? return boot.ApiResp{Code: "100200", Msg: "Success", Data: "TestData: " + req.Username}
}
// /testGetEmpty
// 空返回值包裝測試后添,返回:{"code":"100200","data":null,"msg":"成功"}
func TestGetEmptyHandler(c *gin.Context, req *Foo) {
? ? log.Println("TestGetEmptyHandler.req.username:", req.Username, ",password:", req.Password)
}
// 空返回值包裝測試,返回:{"code":"100200","data":null,"msg":"成功"}
func TestGetHandler(c *gin.Context, page Page, header QueryHeader) {
? ? log.Println("TestGetHandler.page.PageNum:", page.PageNum, ",PageSize:", page.PageSize)
? ? log.Println("TestGetHandler.Authorization:", header.Authorization)
? ? log.Println("TestGetHandler")
}
// 異常全局處理測試薪丁,返回:{"code":"100101","data":null,"msg":"TestPost2Handler.TestError"}
func TestPost2Handler(c *gin.Context, req *Foo) {
? ? log.Println("TestPost2Handler.req.username:", req.Username, ",password:", req.Password)
? ? panic(errors.New("TestPost2Handler.TestError"))
}
// /testGet?username=admin12&password=1234&page_num=1&page_size=10
// url 中的參數(shù)遇西,需要用tag 'form' 標識
// 也可以使用gin方法, GET類型的請求,query參數(shù)也可以自動裝填到結構體
func TestGet1Handler(c *gin.Context, req *Foo, page Page) interface{} {
? ? log.Println("TestGet1Handler.req.username:", req.Username, ",password:", req.Password)
? ? log.Println("TestGet1Handler.page.PageNum:", page.PageNum, ",PageSize:", page.PageSize)
? ? data := map[string]interface{}{
? ? ? ? "list": []*Foo{req},
? ? ? ? "page": page,
? ? }
? ? log.Println("TestGet1Handler.data:", data)
? ? return data
}
func AuthInterceptor(c *gin.Context, header http.Header) {
? ? authorization := c.GetHeader("authorization")
? ? log.Println("AuthInterceptor.authorization:", authorization)
? ? if authorization == "" {
? ? ? ? log.Println("AuthInterceptor authorization is null")
? ? ? ? boot.Resp(c, "105101", "賬戶未登錄", "")
? ? ? ? c.Abort()
? ? ? ? return
? ? }
? ? //TODO getUid By authorization
? ? id := 100
? ? c.Set("uid", id)
}
var webRouter = func(router *boot.WebRouter) {
? ? //router.Use(AuthInterceptor)
? ? // 靜態(tài)資源
? ? router.StaticFile("/", "./views/index.html")
? ? router.StaticFS("/static/", http.Dir("./views/static/"))
? ? // 動態(tài)API
? ? router.POST("/testPost", AuthInterceptor, TestPost1Handler)
? ? router.POST("/testPost2", TestPost2Handler)
? ? router.GET("/testGetEmpty", TestGetEmptyHandler)
? ? router.GET("/testGet", TestGetHandler)
? ? router.GET("/testGet1", AuthInterceptor, TestGet1Handler)
? ? apiRouter := router.Group("/api/")
? ? apiRouter.GET("/test", TestPost2Handler)
}
func main() {
? ? boot.Start("8088", "/", webRouter)
}