html5彩票网站模板,做网站遇到的困难总结,龙岗区住房建设局网站,网页制作基础教程背景图片背景
掌握了 Go 语言的基础后就该开始实践了#xff0c;编写Web应用首先需要一个 web 开发框架。做框架选型时#xff0c;处理web请求是基本功能#xff0c;至于MVC是更进一步需要。现在比较流行的web架构是前后端分离#xff0c;后端响应RESTful的请求#xff0c;Iris 能…背景
掌握了 Go 语言的基础后就该开始实践了编写Web应用首先需要一个 web 开发框架。做框架选型时处理web请求是基本功能至于MVC是更进一步需要。现在比较流行的web架构是前后端分离后端响应RESTful的请求Iris 能满足我们的需要。
Iris简介 它是用Go编写的一个相当新的web框架。它是精心编写的最快的HTTP/2 web 框架。IRIS提供了相当优美的表达语法和简单易用的框架支持你开发网站、API或分布式应用程序 简单来说Iris的特点
语法简单小巧轻量快支持中间件插件请求拦截支持 开发网站、API或分布式应用程序
本文结构
代码语言javascript
复制
开始吧导入包设定一些参数和启动响应 HTTP 中的各种方法method): Get, Post, Put, Patch, Delete 和 Options从 请求 中获得参数的值从查询字符串 QueryString 中获得值从表单Form) 中获得值上传文件支持对路由的分组中间件写入日志到文件中Cookie 操作处理跨域CORS
开始吧
导入包
设定一些参数和启动
开始吧
导入包
一些说明
iris包 github.com/kataras/iris/v12iris的日志github.com/kataras/iris/v12/middleware/logger能够在崩溃时记录和恢复github.com/kataras/iris/v12/middleware/recover
代码示例 package main
import (github.com/kataras/iris/v12github.com/kataras/iris/v12/middleware/loggergithub.com/kataras/iris/v12/middleware/recover
)
设定一些参数和启动 func main() {app : iris.New()app.Logger().SetLevel(debug)// 可选的recover 和logger 是内建的中间件帮助在 崩溃时记录和恢复app.Use(recover.New())app.Use(logger.New())// GET方法 返回一个 HTML// 示例 URL : http://localhost:8080app.Handle(GET, /, func(ctx iris.Context) {ctx.HTML(h1Welcome/h1)})// GET方法 返回 字符串// 示例 URL : http://localhost:8080/pingapp.Get(/ping, func(ctx iris.Context) {ctx.WriteString(pong)})// GET 方法 返回 JSON 格式的数据// 示例 URL : http://localhost:8080/helloapp.Get(/hello, func(ctx iris.Context) {ctx.JSON(iris.Map{message: Hello Iris!})})// 启动// http://localhost:8080// http://localhost:8080/ping// http://localhost:8080/helloapp.Run(iris.Addr(:8080), iris.WithoutServerError(iris.ErrServerClosed))
}
上面演示了 GET 方法 返回一个 HTML返回字符串返回 JSON的情形。 代码很简单一看就懂。
响应 HTTP 中的各种方法method): Get, Post, Put, Patch, Delete 和 Options func main() {// Creates an application with default middleware:// logger and recovery (crash-free) middleware.app : iris.Default()app.Get(/someGet, getting)app.Post(/somePost, posting)app.Put(/somePut, putting)app.Delete(/someDelete, deleting)app.Patch(/somePatch, patching)app.Head(/someHead, head)app.Options(/someOptions, options)app.Run(iris.Addr(:8080))
}
从 请求 中获得参数的值 app.Get(/users/{id:uint64}, func(ctx iris.Context){id : ctx.Params().GetUint64Default(id, 0)// [...]
})
从查询字符串 QueryString 中获得值
查询字符串 QueryString是网址中的 键值对的格式。 比如这样格式 /welcome?firstnameJanelastnameDoe. app.Get(/welcome, func(ctx iris.Context) {// 下面这个是 ctx.Request().URL.Query().Get(lastname). 的简单写法// 读取值lastname : ctx.URLParam(lastname) // 读取值支持默认值的方式firstname : ctx.URLParamDefault(firstname, Guest)})
从表单Form) 中获得值
通过POST发来的请求中有 “表单Form) 数据”, 这样来获取 app.Post(/form_post, func(ctx iris.Context) {message : ctx.FormValue(message)nick : ctx.FormValueDefault(nick, anonymous)})
上传文件
设定 maxSize 控制 请求包的大小和保存的文件名。
复制
const maxSize 5 20 // 5MBfunc main() {app : iris.Default()app.Post(/upload, iris.LimitRequestBodySize(maxSize), func(ctx iris.Context) {// 这里指示了 网址和 beforeSavectx.UploadFormFiles(./uploads, beforeSave)})app.Run(iris.Addr(:8080))
}func beforeSave(ctx iris.Context, file *multipart.FileHeader) {ip : ctx.RemoteAddr()// 获取ip地址转成字符串ip strings.Replace(ip, ., _, -1)ip strings.Replace(ip, :, _, -1)// 这里处理了文件名file.Filename ip - file.Filename
}
支持对路由的分组
复制 func main() {app : iris.Default()// Simple group: v1.v1 : app.Party(/v1){v1.Post(/login, loginEndpoint)v1.Post(/submit, submitEndpoint)v1.Post(/read, readEndpoint)}// Simple group: v2.v2 : app.Party(/v2){v2.Post(/login, loginEndpoint)v2.Post(/submit, submitEndpoint)v2.Post(/read, readEndpoint)}app.Run(iris.Addr(:8080))}
中间件
使用 app.Use() 函数来添加中间件
示例
代码语言javascript
复制 app : iris.New()app.Use(recover.New())
日志中间件 requestLogger : logger.New(logger.Config{// Status displays status codeStatus: true,// IP displays requests remote addressIP: true,// Method displays the http methodMethod: true,// Path displays the request pathPath: true,// Query appends the url query to the Path.Query: true,// if !empty then its contents derives from ctx.Values().Get(logger_message)// will be added to the logs.MessageContextKeys: []string{logger_message},// if !empty then its contents derives from ctx.GetHeader(User-Agent)MessageHeaderKeys: []string{User-Agent},})app.Use(requestLogger)
为某个分组的 url 段添加中间件
// Authorization party /user.
// authorized : app.Party(/user, AuthRequired())
// exactly the same as:
authorized : app.Party(/user)
// per party middleware! in this case we use the custom created
// AuthRequired() middleware just in the authorized group/party.
authorized.Use(AuthRequired())
{authorized.Post(/login, loginEndpoint)authorized.Post(/submit, submitEndpoint)authorized.Post(/read, readEndpoint)// nested group: /user/testingtesting : authorized.Party(/testing)testing.Get(/analytics, analyticsEndpoint)
}
写入日志到文件中
先准备一个 file 流
// Get a filename based on the date, just for the sugar.
func todayFilename() string {today : time.Now().Format(Jan 02 2006)return today .txt
}func newLogFile() *os.File {filename : todayFilename()// Open the file, this will append to the todays file if server restarted.f, err : os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)if err ! nil {panic(err)}return f
}
调用 app.Logger().SetOutput 指向这个文件
f : newLogFile()
defer f.Close()app : iris.New()
app.Logger().SetOutput(f)
Cookie 操作 ctx.SetCookieKV(name, value)value : ctx.GetCookie(name)ctx.RemoveCookie(name)
示例 app.Get(/cookies/{name}/{value}, func(ctx iris.Context) {name : ctx.Params().Get(name)value : ctx.Params().Get(value)ctx.SetCookieKV(name, value)ctx.Writef(cookie added: %s %s, name, value)})
处理跨域CORS
跨域资源共享(CORS) 是一种机制它使用额外的 HTTP 头来告诉浏览器 让运行在一个 origin (domain) 上的Web应用被准许访问来自不同源服务器上的指定的资源。 出于安全原因浏览器限制从脚本内发起的跨源HTTP请求。 例如XMLHttpRequest和Fetch API遵循同源策略。 这意味着使用这些API的Web应用程序只能从加载应用程序的同一个域请求HTTP资源除非响应报文包含了正确CORS响应头。
跨域资源共享 CORS 机制允许 Web 应用服务器进行跨域访问控制从而使跨域数据传输得以安全进行。现代浏览器支持在 API 容器中例如 XMLHttpRequest 或 Fetch 使用 CORS以降低跨域 HTTP 请求所带来的风险。
Iris 的一个社区框架可以帮助解决跨域问题分几个步骤
配置 crs 对象的参数AllowedOrigins 参数设定服务器地址为你的 Party 加入允许。方法 app.Party(/api/v1, crs).AllowMethods(iris.MethodOptions)
代码示例 package mainimport (github.com/kataras/iris/v12github.com/iris-contrib/middleware/cors)func main() {app : iris.New()crs : cors.New(cors.Options{AllowedOrigins: []string{*}, // 这里写允许的服务器地址* 号标识任意AllowCredentials: true,})v1 : app.Party(/api/v1, crs).AllowMethods(iris.MethodOptions) // - important for the preflight.{v1.Get(/home, func(ctx iris.Context) {ctx.WriteString(Hello from /home)})v1.Get(/about, func(ctx iris.Context) {ctx.WriteString(Hello from /about)})v1.Post(/send, func(ctx iris.Context) {ctx.WriteString(sent)})v1.Put(/send, func(ctx iris.Context) {ctx.WriteString(updated)})v1.Delete(/send, func(ctx iris.Context) {ctx.WriteString(deleted)})}app.Run(iris.Addr(localhost:8080))}
详细见https://github.com/iris-contrib/middleware/tree/master/cors
了解更多
更多请参考官方文档https://iris-go.com/
官方GIT地址 https://github.com/kataras/iris