用凡科帮别人做网站,浙0577 icp网站建设,外贸在什么网站做,电商工作计划怎么写文章目录 安装使用RESTful API响应页面获取请求参数路由讲解中间件 安装使用
Gin 是一个 golang 的微框架#xff0c;封装比较优雅#xff0c;API 友好#xff0c;源代码比较明确。具有快速灵活#xff0c;容错方便等特点。其实对于 golang 而言#xff0c;web 框架的依赖… 文章目录 安装使用RESTful API响应页面获取请求参数路由讲解中间件 安装使用
Gin 是一个 golang 的微框架封装比较优雅API 友好源代码比较明确。具有快速灵活容错方便等特点。其实对于 golang 而言web 框架的依赖远比 pythonjava 之类的要小。自身的 net/http 足够简单性能也非常不错。框架更像是一个常用函数或者工具的集合。借助框架开发不仅可以省去很多常用的封装带来的时间也有助于团队的编码风格和形成规范。Gin 官方文档地址https://gin-gonic.com/zh-cn.docs安装 Gin
go get -u github.com/gin-gonic/gin在 windows 10 系统中安装 Go1.19 之后的版本然后打开 go module在命令行终端中输入go env -w GO111MODULEon修改指定的代理在命令行终端中输入go env -w GOPROXYhttps:/lgoproxy.io,direct
package mainimport github.com/gin-gonic/gin
import github.com/thinkerou/faviconfunc main() {// 创建一个服务ginServer : gin.Default()// 为网页标签导入一个iconginServer.Use(favicon.New(path/to/your/icon))// 连接数据库的代码// 访问地址处理请求 Request ResponseginServer.GET(/hello, func(context *gin.Context) {context.JSON(200, gin.H{msg: Hello World!})})// gin.H 其实就是一个 map[string]any// 服务器端口ginServer.Run(:8082)
}RESTful API
RESTful APIRepresentational State Transfer API是一种基于REST架构风格的网络应用编程接口它通过HTTP协议进行通信常用于Web服务的实现。RESTful API遵循一些基本的设计原则使得服务更加灵活、简单和易于维护。REST的核心思想是通过定义资源Resource并通过HTTP动词GET、POST、PUT、DELETE等对资源进行操作。
// 以前写网站
get /user
post /create_user
post /update_user
post /delete_user// RESTful API
get /user
post /user
put /user
delete /userGET获取资源不修改服务器上的数据。POST创建新的资源通常用于提交数据。PUT更新资源用于替代现有资源。DELETE删除资源。PATCH部分更新资源。 // 访问地址处理请求 Request Response// Gin RestFul 十分简单ginServer.GET(/hello, func(context *gin.Context) {context.JSON(200, gin.H{msg: Hello World!})})ginServer.POST(/user, func(context *gin.Context) {context.JSON(200, gin.H{msg: Post user})})ginServer.PUT(/user, func(context *gin.Context) {context.JSON(200, gin.H{msg: Put user})})ginServer.DELETE(/user, func(context *gin.Context) {context.JSON(200, gin.H{msg: Delete user})})响应页面 // 加载静态页面(全局加载)ginServer.loadHTMLGlob(templates/*)// 加载资源文件gin.Server.Static(./static,./static)// 响应一个页面给前端ginServer.GET(/index, func(context *gin.Context) {// context.JSON() json数据context.HTML(http.StatusOK, index.html, gin.H{msg:This is the data form server.})// 前端用 {{.msg}} 赋值表达式即可取出})获取请求参数
url?userid1usernamez3url传参方式 ginServer.GET(/user/info, func(context *gin.Context) {userid : context.Query(userid)username : context.Query(username)context.JSON(http.StatusOK, gin.H {userid:userid,username:username,})})url/user/info/1/z3RestFul风格请求参数 // :就可以直接取出这个值了ginServer.GET(/user/info/:userid/:username, func(context *gin.Context) {userid : context.Param(userid)username : context.Param(username)context.JSON(http.StatusOK, gin.H {userid:userid,username:username,})})// 前端给后端传递 jsonginServer.GET(/json, func(context *gin.Context) {// request.bodyb, _ : context.GetRawData()var m map[string]interface{}// 返回的是byte切片包装为json数据_ json.Unmarshal(b, m)context.JSON(http.StatusOK, m)})// 处理表单ginServer.GET(/user/add, func(context *gin.Context) {username : context.PostForm(username)password : context.PostForm(password)// 校验逻辑略context.JSON(http.StatusOK, gin.H {msg:ok,username:username,password:password,})})路由讲解 ginServer.GET(/json, func(context *gin.Context) {// 重定向context.Redirect(http.StatusMovedPermanently, https://www.4399.com)})// 404 NoRouteginServer.NoRoute(func(context *gin.Context) {context.Redirect(http.StatusNotFound, 404.html, nil)})// 路由组userGroup : ginServer.Group(/user){userGroup.POST(/add, func)userGroup.POST(/login, func)userGroup.POST(/logout, func)}orderGroup : ginServer.Group(/order){orderGroup.POST(/add, func)orderGroup.DELETE(/delete, func)}中间件 // go中间件可以进行预处理登录授权、验证、分页等// 自定义go中间件 拦截器func myHandler() (gin.HandlerFunc) {return func(context *gin.Context) {// 通过自定义的中间件设置的值在后续处理只要调用了这个中间件都可以拿到这里的值context.Set(usersession, userid-1)if condition {context.Next() // 放行} else {context.Abort() // 阻止}}}ginServer.GET(/user/info, myHandler(), func(context *gin.Context) {// 取出中间件中的值usersession : context.MustGet(userSession).(string)log.Println(userSession, usersession)userid : context.Query(userid)username : context.Query(username)context.JSON(http.StatusOK, gin.H {userid:userid,username:username,})})