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

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三区国产毛片| 欧美一区二区三区顶级黄片| 色一区欧美一区亚洲一区| 国产丝袜美腿视频亚洲综合| 欧美成人精品视频在线不卡| 91成人国产综合久久精品| 久久久国产精品视频一区| 久久夜色一区二区三区| 久久精品国产免费夜夜嗨| 国产原创传媒在线观看| 禁止18岁以下的视频| 国产日韩精品在线视频| 久久精品国产亚洲av不卡| 国产三级精品三级精品在一区| 国产精品一区二区熟女| 免费一区二区三区黄色| 欧美日韩亚洲视频二区| 岛国大片一区二区三区| 国产国语网站在线观看| 日本韩国视频一区二区| 九月丁香花开综合网| 精品女厕一区二区三区| 中文国产人精品久久蜜桃| 国产又粗又长又大又长| 国产精品97久久97久久久久| jvid视频在线观看免费| 国产又粗又猛又爽黄老大爷 | 午夜在线成人免费观看| 国产精品久久亚洲一区二区| 蜜桃在线视频在线观看| 中文字幕日本人妻影视| 精品国产三级a在线观看网站| 夫妻性生活在线视频一级片|