golang iris使用
安裝iris
go get github.com/kataras/iris
實例
註冊一個route到服務的API
app := iris.New() app.Handle("GET", "/ping", func(ctx iris.Context) { ctx.JSON(iris.Map{"message": "pong"}) }) app.Run(iris.Addr(":8080"))
幾行代碼就可以實現,通過瀏覽器訪問 http://localhost :8080/ping會返回{"message":"pong"}
使用Handle函數可以註冊方法,路徑和對應的處理函數
添加middleware
如果我們希望記錄下所有的請求的log信息還希望在調用相應的route時確認請求的UA是否是我們允許的可以通過Use函數添加相應的middleware
package main import ( "github.com/kataras/iris" "github.com/kataras/iris/middleware/logger" ) func main() { app := iris.New() app.Use(logger.New()) app.Use(checkAgentMiddleware) app.Handle("GET", "/ping", func(ctx iris.Context) { ctx.JSON(iris.Map{"message": "pong"}) }) app.Run(iris.Addr(":8080")) } func checkAgentMiddleware(ctx iris.Context) { ctx.Application().Logger().Infof("Runs before %s", ctx.Path()) user_agent := ctx.GetHeader("User-Agent") if user_agent != "pingAuthorized" { ctx.JSON("No authorized for ping") return } ctx.Next() }
使用postman訪問在Header中添加User-Agent訪問/ping可以正常返回結果,如果去掉User-Agent則會返回我們設定的"No authorized for ping"。因爲我們添加了iris的log middleware所以在訪問時會在終端顯示相應的log信息
取得請求參數,展示到html
bookinfo.html
<html> <head>Book information</head> <body> <h2>{{ .bookName }}</h2> <h1>{{ .bookID }}</h1> <h1>{{ .author }}</h1> <h1>{{ .chapterCount }}</h1> </body> </html>
main.go
package main import "github.com/kataras/iris" func main() { app := iris.New() app.RegisterView(iris.HTML("./views", ".html")) app.Handle("GET", "/bookinfo/{bookid:string}", func(ctx iris.Context) { bookID := ctx.Params().GetString("bookid") ctx.ViewData("bookName", "Master iris") ctx.ViewData("bookID", bookID) ctx.ViewData("author", "Iris expert") ctx.ViewData("chapterCount", "40") ctx.View("bookinfo.html") }) app.Run(iris.Addr(":8080")) }
取得請求中帶的參數
ctx.Params().GetString("bookid")
設置html中變量的值
ctx.ViewData(key, value)
route允許和禁止外部訪問
實際使用中有時會有些route只能內部使用,對外訪問不到。
可以通過使用 XXX_route.Method = iris.MethodNone設定爲offline
內部調用通過使用函數 Context.Exec("NONE", "/XXX_yourroute")
main.go
package main import "github.com/kataras/iris" import "strings" func main() { app := iris.New() magicAPI := app.Handle("NONE", "/magicapi", func(ctx iris.Context) { if ctx.GetCurrentRoute().IsOnline() { ctx.Writef("I'm back!") } else { ctx.Writef("I'll be back") } }) app.Handle("GET", "/onoffhandler/{method:string}/{state:string}", func(ctx iris.Context) { changeMethod := ctx.Params().GetString("method") state := ctx.Params().GetString("state") if changeMethod == "" || state == "" { return } if strings.Index(magicAPI.Path, changeMethod) == 1 { settingState := strings.ToLower(state) if settingState == "on" || settingState == "off" { if strings.ToLower(state) == "on" && !magicAPI.IsOnline() { magicAPI.Method = iris.MethodGet } else if strings.ToLower(state) == "off" && magicAPI.IsOnline() { magicAPI.Method = iris.MethodNone } app.RefreshRouter() ctx.Writef("\n Changed magicapi to %s\n", state) } else { ctx.Writef("\n Setting state incorrect(\"on\" or \"off\") \n") } } }) app.Handle("GET", "/execmagicapi", func(ctx iris.Context) { ctx.Values().Set("from", "/execmagicapi") if !magicAPI.IsOnline() { ctx.Exec("NONE", "/magicapi") } else { ctx.Exec("GET", "/magicapi") } }) app.Run(iris.Addr(":8080")) }
測試:
<1>訪問 http://localhost :8080/magicapi,返回Not found。說明route magicapi對外無法訪問
<2>訪問 http://localhost :8080/execmagicapi,返回I'll be back。在execmagicapi處理函數中會執行 ctx.Exec("GET", "/magicapi")調用offline的route magicapi。在magicapi中會判斷自己是否offline,如果爲offline則返回I'll be back。
<3>訪問 http://localhost :8080/onoffhandler/magicapi/on改變magicapi爲online
<4>再次訪問 http://localhost :8080/magicapi,返回I'm back!。說明route /mabicapi已經可以對外訪問了
grouping route
在實際應用中會根據實際功能進行route的分類,例如users,books,community等。
/users/getuserdetail
/users/getusercharges
/users/getuserhistory
/books/bookinfo
/books/chapterlist
對於這類route可以把他們劃分在users的group和books的group。對該group會有共通的handler處理共同的一些處理
package main import ( "time" "github.com/kataras/iris" "github.com/kataras/iris/middleware/basicauth" ) func bookInfoHandler(ctx iris.Context) { ctx.HTML("<h1>Calling bookInfoHandler </h1>") ctx.HTML("<br/>bookID:" + ctx.Params().Get("bookID")) ctx.Next() } func chapterListHandler(ctx iris.Context) { ctx.HTML("<h1>Calling chapterListHandler </h1>") ctx.HTML("<br/>bookID:" + ctx.Params().Get("bookID")) ctx.Next() } func main() { app := iris.New() authConfig := basicauth.Config{ Users: map[string]string{"bookuser": "testabc123"}, Realm: "Authorization required", Expires: time.Duration(30) * time.Minute, } authentication := basicauth.New(authConfig) books := app.Party("/books", authentication) books.Get("/{bookID:string}/bookinfo", bookInfoHandler) books.Get("/chapterlist/{bookID:string}", chapterListHandler) app.Run(iris.Addr(":8080")) }
上例中使用了basicauth。對所有訪問books group的routes都會先做auth認證。認證方式是username和password。
在postman中訪問 http://localhost :8080/books/sfsg3234/bookinfo
設定Authorization爲Basic Auth,Username和Password設定爲程序中的值,訪問會正確回覆。否則會回覆Unauthorized
有疑問加站長微信聯繫