文章目录 安装使用 RESTful API 响应页面 获取请求参数 路由讲解 中间件
安装使用
Gin 是一个 golang 的微框架,封装比较优雅,API 友好,源代码比较明确。具有快速灵活,容错方便等特点。其实对于 golang 而言,web 框架的依赖远比 python,java 之类的要小。自身的 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 GO111MODULE=on
修改指定的代理,在命令行终端中输入:go env -w GOPROXY=https:/lgoproxy.io,direct
package mainimport "github.com/gin-gonic/gin"
import "github.com/thinkerou/favicon" func main ( ) { ginServer := gin. Default ( ) ginServer. Use ( favicon. New ( "path/to/your/icon" ) ) ginServer. GET ( "/hello" , func ( context * gin. Context) { context. JSON ( 200 , gin. H{ "msg" : "Hello World!" } ) } ) ginServer. Run ( ":8082" )
}
RESTful API
RESTful API(Representational 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
get / user
post / user
put / user
delete / user
GET:获取资源,不修改服务器上的数据。 POST:创建新的资源,通常用于提交数据。 PUT:更新资源,用于替代现有资源。 DELETE:删除资源。 PATCH:部分更新资源。
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. HTML ( http. StatusOK, "index.html" , gin. H{ "msg" : "This is the data form server." } ) } )
获取请求参数
url?userid=1&username=z3
,url传参方式
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/z3
,RestFul风格请求参数
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, } ) } )
ginServer. GET ( "/json" , func ( context * gin. Context) { b, _ := context. GetRawData ( ) var m map [ string ] interface { } _ = 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" ) } )
ginServer. 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 ) }
中间件
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, } ) } )