生命不止,继续 go go go!!!
昨天介绍了iris框架,介绍了如何使用basic认证、Markdown、YAML、Json等:
Go实战–也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)
继续跟大家一起学习iris框架.
TOML
什么是toml?
toml也是一种配置文件,关于golang中配置文件的使用之前也有介绍过:
Go实战–go语言中使用YAML配置文件(与json、xml、ini对比)
toml是Tom’s Obvious, Minimal Language.的缩写!
配置文件的使用由来已久,从.ini、XML、JSON、YAML再到TOML,语言的表达能力越来越强,同时书写便捷性也在不断提升。 TOML是前GitHub CEO, Tom Preston-Werner,于创建的语言,其目标是成为一个小规模的易于使用的语义化配置文件格式。TOML被设计为可以无二义性的转换为一个哈希表(Hash table)。
github地址:
/toml-lang/toml
Star: 6478
例子
# This is a TOML document.title = "TOML Example"[owner]name = "Tom Preston-Werner"dob = 1979-05-27T07:32:00-08:00 # First class dates[database]server = "192.168.1.1"ports = [ 8001, 8001, 8002 ]connection_max = 5000enabled = true[servers]# Indentation (tabs and/or spaces) is allowed but not required[servers.alpha]ip = "10.0.0.1"dc = "eqdc10"[servers.beta]ip = "10.0.0.2"dc = "eqdc10"[clients]data = [ ["gamma", "delta"], [1, 2] ]# Line breaks are OK when inside arrayshosts = ["alpha","omega"]
看到了吧,不关心空格和缩进。
规范
大小写敏感
必须是UTF-8编码
不关心空格、缩进
使用#进行注释
Go中操作TOML的优秀开源库
/BurntSushi/toml
/pelletier/go-toml
具体的之后会专业写博客介绍
iris读取toml
新建iris.tml文件:
DisablePathCorrection = falseEnablePathEscape = falseFireMethodNotAllowed = trueDisableBodyConsumptionOnUnmarshal = falseTimeFormat = "Mon, 01 Jan 15:04:05 GMT"Charset = "UTF-8"[Other]MyServerName = "iris"
main.go:
package mainimport ("/kataras/iris""/kataras/iris/context")func main() {app := iris.New()app.Get("/", func(ctx context.Context) {ctx.HTML("<b>Hello Iris TOML!</b>")})app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.TOML("iris.tml")))}
Cache
关于缓存,一直没有跟大家介绍,github上也有很多关于golang的优秀的Cache库:
patrickmn/go-cache
An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
golang/groupcache
groupcache is a caching and cache-filling library, intended as a replacement for memcached in many cases.
同样,我们以后再进行详细介绍,这里我们看看iris中使用Cache:
package mainimport ("time""/kataras/iris""/kataras/iris/cache""/kataras/iris/context")func main() {app := iris.Default()cachedHandler := cache.WrapHandler(h, 2*time.Minute)app.Get("/hello", cachedHandler)app.Run(iris.Addr(":8080"))}func h(ctx context.Context) {ctx.HTML("<h1> Hello, this should be cached. Every 2 minutes it will be refreshed, check your browser's inspector</h1>")}
cookie
关于cookie,之前也有些文章分享过:
Go实战–go中使用cookie(The way to go)
iris当然不会缺少cookie的功能了。
package mainimport ("/kataras/iris""/kataras/iris/context""/kataras/iris/sessions""/gorilla/securecookie")func newApp() *iris.Application {app := iris.New()cookieName := "mycustomsessionid"// AES only supports key sizes of 16, 24 or 32 bytes.// You either need to provide exactly that amount or you derive the key from what you type in.hashKey := []byte("the-big-and-secret-fash-key-here")blockKey := []byte("lot-secret-of-characters-big-too")secureCookie := securecookie.New(hashKey, blockKey)mySessions := sessions.New(sessions.Config{Cookie: cookieName,Encode: secureCookie.Encode,Decode: secureCookie.Decode,})app.Get("/", func(ctx context.Context) {ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")})app.Get("/set", func(ctx context.Context) {//set session valuess := mySessions.Start(ctx)s.Set("name", "iris")//test if setted herectx.Writef("All ok session setted to: %s", s.GetString("name"))})app.Get("/get", func(ctx context.Context) {// get a specific key, as string, if no found returns just an empty strings := mySessions.Start(ctx)name := s.GetString("name")ctx.Writef("The name on the /set was: %s", name)})app.Get("/delete", func(ctx context.Context) {// delete a specific keys := mySessions.Start(ctx)s.Delete("name")})app.Get("/clear", func(ctx context.Context) {// removes all entriesmySessions.Start(ctx).Clear()})app.Get("/update", func(ctx context.Context) {// updates expire date with a new datemySessions.ShiftExpiration(ctx)})app.Get("/destroy", func(ctx context.Context) {//destroy, removes the entire session data and cookiemySessions.Destroy(ctx)})return app}func main() {app := newApp()app.Run(iris.Addr(":8080"))}
浏览器访问:http://localhost:8080/set
http://localhost:8080/get