欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

golangiris的使用方法

這篇文章主要講解了“golang iris的使用方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“golang iris的使用方法”吧!

成都創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專(zhuān)注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開(kāi)發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開(kāi)發(fā),軟件開(kāi)發(fā),小程序制作,10多年建站對(duì)成都陽(yáng)臺(tái)護(hù)欄等多個(gè)方面,擁有多年的網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn)。

安裝iris
go get github.com/kataras/iris
實(shí)例
注冊(cè)一個(gè)route到服務(wù)的API
app := iris.New()

app.Handle("GET", "/ping", func(ctx iris.Context) {
    ctx.JSON(iris.Map{"message": "pong"})
})

app.Run(iris.Addr(":8080"))

幾行代碼就可以實(shí)現(xiàn),通過(guò)瀏覽器訪問(wèn)http://localhost:8080/ping會(huì)返回{"message":"pong"}

使用Handle函數(shù)可以注冊(cè)方法,路徑和對(duì)應(yīng)的處理函數(shù)

添加middleware

如果我們希望記錄下所有的請(qǐng)求的log信息還希望在調(diào)用相應(yīng)的route時(shí)確認(rèn)請(qǐng)求的UA是否是我們?cè)试S的可以通過(guò)Use函數(shù)添加相應(yīng)的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()
}

golang iris的使用方法

使用postman訪問(wèn)在Header中添加User-Agent訪問(wèn)/ping可以正常返回結(jié)果,如果去掉User-Agent則會(huì)返回我們?cè)O(shè)定的"No authorized for ping"。因?yàn)槲覀兲砑恿薸ris的log middleware所以在訪問(wèn)時(shí)會(huì)在終端顯示相應(yīng)的log信息

取得請(qǐng)求參數(shù),展示到html

bookinfo.html

<html>
    <head>Book information</head>
    <body>
        <h3>{{ .bookName }}</h3>
        <h2>{{ .bookID }}</h2>
        <h2>{{ .author }}</h2>
        <h2>{{ .chapterCount }}</h2>
    </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"))
}

取得請(qǐng)求中帶的參數(shù)

ctx.Params().GetString("bookid")

設(shè)置html中變量的值

ctx.ViewData(key, value)

route允許和禁止外部訪問(wèn)

實(shí)際使用中有時(shí)會(huì)有些route只能內(nèi)部使用,對(duì)外訪問(wèn)不到。
可以通過(guò)使用 XXX_route.Method = iris.MethodNone設(shè)定為offline
內(nèi)部調(diào)用通過(guò)使用函數(shù) 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"))
}

測(cè)試:

<1>訪問(wèn)http://localhost:8080/magicapi,返回Not found。說(shuō)明route magicapi對(duì)外無(wú)法訪問(wèn)
<2>訪問(wèn)http://localhost:8080/execmagicapi,返回I'll be back。在execmagicapi處理函數(shù)中會(huì)執(zhí)行 ctx.Exec("GET", "/magicapi")調(diào)用offline的route magicapi。在magicapi中會(huì)判斷自己是否offline,如果為offline則返回I'll be back。
<3>訪問(wèn)http://localhost:8080/onoffhandler/magicapi/on改變magicapi為online
<4>再次訪問(wèn)http://localhost:8080/magicapi,返回I'm back!。說(shuō)明route /mabicapi已經(jīng)可以對(duì)外訪問(wèn)了

grouping route

在實(shí)際應(yīng)用中會(huì)根據(jù)實(shí)際功能進(jìn)行route的分類(lèi),例如users,books,community等。

/users/getuserdetail
/users/getusercharges
/users/getuserhistory

/books/bookinfo
/books/chapterlist

對(duì)于這類(lèi)route可以把他們劃分在users的group和books的group。對(duì)該group會(huì)有共通的handler處理共同的一些處理

package main

import (
    "time"

    "github.com/kataras/iris"
    "github.com/kataras/iris/middleware/basicauth"
)

func bookInfoHandler(ctx iris.Context) {
    ctx.HTML("<h2>Calling bookInfoHandler </h2>")
    ctx.HTML("<br/>bookID:" + ctx.Params().Get("bookID"))
    ctx.Next()
}

func chapterListHandler(ctx iris.Context) {
    ctx.HTML("<h2>Calling chapterListHandler </h2>")
    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。對(duì)所有訪問(wèn)books group的routes都會(huì)先做auth認(rèn)證。認(rèn)證方式是username和password。

在postman中訪問(wèn) http://localhost:8080/books/sfsg3234/bookinfo
設(shè)定Authorization為Basic Auth,Username和Password設(shè)定為程序中的值,訪問(wèn)會(huì)正確回復(fù)。否則會(huì)回復(fù)Unauthorized
golang iris的使用方法

感謝各位的閱讀,以上就是“golang iris的使用方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)golang iris的使用方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

分享題目:golangiris的使用方法
文章源于:http://aaarwkj.com/article40/iggdho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、、企業(yè)網(wǎng)站制作營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站維護(hù)域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司
av色剧情在线免费观看| 少妇高潮惨叫久久麻豆传| 久久精品国产成人综合| 日本视频一曲二曲三曲四曲| 成人黄片在线免费播放| 日韩黄色成人免费片子| 中文字幕人妻系列东京热| 中文字幕人妻熟女在线| 免费观看在线视频午夜| 日韩欧美第一页在线观看| 在线看日本一区二区| 色婷婷精品综合久久狠狠| 日韩成人在线视频观看| 亚洲av日韩专区在线观看| 国产成人综合久久精品推荐| 日本久久精品免费网站| 欧美性做爰片免费视频网| 国产成人亚洲合色婷婷| 亚洲中文有码在线播放| 99久久伊人精品综合观看| 国产三级国产精品国产| 国产一区二区三区精品久久| 亚洲国产精品一区一区| 丝袜在线美腿视频网站| 在线免费观看日韩黄片| 国产熟女av一区二区| av在线免费观看青青草原| 中文字幕人妻久久精品一区 | 国产传媒剧情剧资源网站| 女同av免费观看网站| 最新日韩中文字幕在线播放| av一区二区三区三| 岛国高清乱码中文字幕| 欧美日韩天堂一区二区| 日本乱码一区二区三区在线观看| 蜜臀综合亚洲国产精品| 亚洲午夜精品美女写真| 色哟哟视频在线免费观看| 色哟哟亚洲精品一区二区| 日韩欧美亚洲一区二区三区| 久久亚洲春色中文字幕|