Go實戰--也許最快的Go語言Web框架kataras/iris初識(basic認證、Markdown、YAML、Json)
ris自稱是Go語言中所有Web框架最快的,它的特點如下:
1.聚焦高效能
2.健壯的靜態路由支援和萬用字元子域名支援。
3.檢視系統支援超過5以上模板
4.支援定製事件的高可擴充套件性Websocket API
5.帶有GC, 記憶體 & redis 提供支援的會話
6.方便的中介軟體和外掛
7.完整 REST API
8.能定製 HTTP 錯誤
9.Typescript編譯器 + 基於瀏覽器的編輯器
10.內容 negotiation & streaming
11.傳送層安全性
12.原始碼改變後自動載入
13.OAuth, OAuth2 支援27+ API providers
14.JSON Web Tokens
kataras/iris簡介
github地址
https://github.com/kataras/iris
Star: 7938
文件地址
https://docs.iris-go.com/
描述
關於kataras/iris的描述十分霸氣:
The fastest web framework for Go in (THIS) Earth. HTTP/2 Ready to GO. MVC when you need it.
還是那句話,暫時不去計較,只是學習。
獲取
go get -u github.com/kataras/iris
快速開始
新建main.go
新建views資料夾,在views中新建hello.html
main.go
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
app.Get("/", func(ctx iris.Context) {
ctx.ViewData("message", "Hello world!")
ctx.View("hello.html")
})
app.Run(iris.Addr(":8080"))
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
hello.html
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
執行,在瀏覽器輸入http://localhost:8080/,得到執行結果。
basic認證
之前寫過關於basic認證的文章:
Go實戰–通過basic認證的http(basic authentication)
package main
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/middleware/basicauth"
)
func newApp() *iris.Application {
app := iris.New()
authConfig := basicauth.Config{
Users: map[string]string{"wangshubo": "wangshubo", "superWang": "superWang"},
Realm: "Authorization Required",
Expires: time.Duration(30) * time.Minute,
}
authentication := basicauth.New(authConfig)
app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })
needAuth := app.Party("/admin", authentication)
{
//http://localhost:8080/admin
needAuth.Get("/", h)
// http://localhost:8080/admin/profile
needAuth.Get("/profile", h)
// http://localhost:8080/admin/settings
needAuth.Get("/settings", h)
}
return app
}
func main() {
app := newApp()
app.Run(iris.Addr(":8080"))
}
func h(ctx context.Context) {
username, password, _ := ctx.Request().BasicAuth()
ctx.Writef("%s %s:%s", ctx.Path(), username, password)
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
Markdown
Go實戰–golang中使用markdown(russross/blackfriday)
package main
import (
"time"
<span class="hljs-string">"github.com/kataras/iris"</span>
<span class="hljs-string">"github.com/kataras/iris/context"</span>
<span class="hljs-string">"github.com/kataras/iris/cache"</span>
)
var markdownContents = []byte(`## Hello Markdown
This is a sample of Markdown contents
Features
All features of Sundown are supported, including:
Compatibility. The Markdown v1.0.3 test suite passes with
the –tidy option. Without –tidy, the differences are
mostly in whitespace and entity escaping, where blackfriday is
more consistent and cleaner.Common extensions, including table support, fenced code
blocks, autolinks, strikethroughs, non-strict emphasis, etc.Safety. Blackfriday is paranoid when parsing, making it safe
to feed untrusted user input without fear of bad things
happening. The test suite stress tests this and there are no
known inputs that make it crash. If you find one, please let me
know and send me the input that does it.NOTE: “safety” in this context means runtime safety only. In order to
protect yourself against JavaScript injection in untrusted content, see
this example.Fast processing. It is fast enough to render on-demand in
most web applications without having to cache the output.Routine safety. You can run multiple parsers in different
goroutines without ill effect. There is no dependence on global
shared state.Minimal dependencies. Blackfriday only depends on standard
library packages in Go. The source code is pretty
self-contained, so it is easy to add to any project, including
Google App Engine projects.Standards compliant. Output successfully validates using the
W3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.
func main() {
app := iris.New()
app.Get(<span class="hljs-string">"/"</span>, cache.Handler<span class="hljs-number">(10</span>*time.Second), writeMarkdown)
app.Run(iris.Addr(<span class="hljs-string">":8080"</span>))
}
func writeMarkdown(ctx context.Context) {
println(“Handler executed. Content refreshed.”)
ctx.Markdown(markdownContents)
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
YAML
Go實戰–go語言中使用YAML配置檔案(與json、xml、ini對比)
iris.yml
DisablePathCorrection: false
EnablePathEscape: false
FireMethodNotAllowed: true
DisableBodyConsumptionOnUnmarshal: true
TimeFormat: Mon, 01 Jan 2006 15:04:05 GMT
Charset: UTF-8
- 1
- 2
- 3
- 4
- 5
- 6
main.go
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.HTML("<b>Hello!</b>")
})
app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.YAML("./iris.yml")))
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Post Json
Go實戰–net/http中JSON的使用(The way to go)
package main
import (
"fmt"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
type Company struct {
Name string
City string
Other string
}
func MyHandler(ctx context.Context) {
c := &Company{}
if err := ctx.ReadJSON(c); err != nil {
panic(err.Error())
} else {
fmt.Printf("Company: %#v", c)
ctx.Writef("Company: %#v", c)
}
}
func main() {
app := iris.New()
app.Post("/bind_json", MyHandler)
app.Run(iris.Addr(":8080"))
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
curl命令列執行:
curl -d '{"Name":"vSuperWang", "City":"beijing", "Other":"shit"}' -H "Content-Type: application/json" -X POST http://localhost:8080/bind_json
- 1
相關文章
- Go語言————1、初識GO語言Go
- 初識go語言Go
- go語言web開發框架_Iris框架講解(五)MVC包使用GoWeb框架MVC
- go語言web開發框架_Iris框架講解(六):Session的使用和控制GoWeb框架Session
- 初識Go語言-1Go
- Go語言筆記[實現一個Web框架實戰]——EzWeb框架(一)Go筆記Web框架
- GO 語言 Web 開發實戰一GoWeb
- Go 語言實戰 GraphQLGo
- Go 語言解析 yaml 檔案的方法GoYAML
- Go語言開發的Web框架都有哪些?GoWeb框架
- 手把手和你一起實現一個Web框架實戰——EzWeb框架(二)[Go語言筆記]Go專案實戰Web框架Go筆記
- 手把手和你一起實現一個Web框架實戰——EzWeb框架(三)[Go語言筆記]Go專案實戰Web框架Go筆記
- 手把手和你一起實現一個Web框架實戰——EzWeb框架(四)[Go語言筆記]Go專案實戰Web框架Go筆記
- 手把手和你一起實現一個Web框架實戰——EzWeb框架(五)[Go語言筆記]Go專案實戰Web框架Go筆記
- Go 語言處理 yaml 檔案GoYAML
- go語言json的使用技巧GoJSON
- go語言Json解析實用工具 - gjsonGoJSON
- Go語言SQL操作實戰GoSQL
- go語言的初體驗Go
- 帶讀 |《Go in Action》(中文:Go語言實戰)(一)Go
- 乾貨分享:六個知名的Go語言web框架GoWeb框架
- 基於go語言gin框架的web專案骨架Go框架Web
- Go語言RESTful JSON API建立GoRESTJSONAPI
- Go語言微服務開發框架實踐-go chassis(上篇)Go微服務框架
- Go語言微服務開發框架實踐-go chassis(中篇)Go微服務框架
- Go 語言實戰: 編寫可維護 Go 語言程式碼建議Go
- 初學Go語言 變數Go變數
- 非常適合GO語言新手學習的《Go語言從入門到實戰——簡明高效的Go語言實戰指南》課程——推薦分享Go
- 認識 Go 語言中的陣列Go陣列
- 使用Go語言web框架wego實現使用者登入功能GoWeb框架
- 用 Go 語言實戰 Limit Concurrency 方法GoMIT
- Go語言實戰(三)- 內建容器Go
- 有Go語言實戰培訓班嗎?go語言開發環境搭建Go開發環境
- [譯] Go 語言實戰: 編寫可維護 Go 語言程式碼建議Go
- 帶讀 |《Go in Action》(中文:Go語言實戰) 語法和語言結構概覽(三)Go
- 帶讀 |《Go in Action》(中文:Go語言實戰)語法和語言結構概覽 (二)Go
- go 學習筆記之初識 go 語言Go筆記
- Go語言開發的Web框架都有哪些?詳細介紹GoWeb框架