
24.Gin框架
Gin框架
Go是一门正在快速增长的编程语言, 专为构建简单、快速且可靠的软件而设计. golang提供的net/http库已经很好了, 对于http协议的实现非常好, 基于此再造框架也不会是难事, 因此生态中出现了很多框架.
Gin: Go语言编写的Web框架, 以更好的性能实现类似Martini框架的API
Gin是一个golang的微框架, 封装比较优雅, API友好, 源码注释比较明确. 具有快速灵活, 容错方便等特点
Beego: 开源的高性能Go语言Web框架
beego是一个快速开发Go应用的http框架, go语言方面技术大牛, beego可以用来快速开发API、Web、后端服务等各种应用, 是一个RESTFul的框架, 主要设计灵感来源于tornado、sinatra、flask这三个框架, 但是结合了Go本身的一些特性(interface、struct继承等)而设计的一个框架
lris: 全宇宙最快的Go语言Web框架。 完备MVC支持,未来尽在掌握
lris是一个快速, 简单但功能齐全和非常有效的web框架。提供了一个优美的表现力和容易使用你的下一个网站或API的基础。
1.Gin安装使用
Gin官网地址:https://gin-gonic.com/zh-cn/docs
1.1.安装gin
go get -u github.com/gin-gonic/gin
1.2.导入gin
import "github.com/gin-gonic/gin"
2.Hello
package main
import "github.com/gin-gonic/gin"
import "github.com/thinkerou/favicon"
func main() {
//创建一个服务
ginServer := gin.Default()
ginServer.Use(favicon.New("./avter.png"))
//连接数据库的代码
// 访问地址,处理我们的请求 Request Response
ginServer.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello world"})
})
//服务器端口
ginServer.Run(":8082")
}
3.RESTFul API
get /user
post /user
put /user
delete /user
Gin框架支持RESTFul请求
4.给前端响应页面
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
import "github.com/thinkerou/favicon"
func main() {
//创建一个服务
ginServer := gin.Default()
ginServer.Use(favicon.New("./avter.png"))
//加载静态页面
ginServer.LoadHTMLGlob("templates/*")
//加载资源文件
ginServer.Static("/static", "./static")
//连接数据库的代码
// 访问地址,处理我们的请求 Request Response
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.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{
"msg": "这是go后台传递的数据",
})
})
//服务器端口
ginServer.Run(":8082")
}
5.获取请求中的参数
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"net/http"
)
import "github.com/thinkerou/favicon"
func main() {
//创建一个服务
ginServer := gin.Default()
ginServer.Use(favicon.New("./avter.png"))
//加载静态页面
ginServer.LoadHTMLGlob("templates/*")
//加载资源文件
ginServer.Static("/static", "./static")
//连接数据库的代码
// 访问地址,处理我们的请求 Request Response
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.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{
"msg": "这是go后台传递的数据",
})
})
// ? userid=xxx&username=zjzaki
// user/info/1/zjzaki
ginServer.GET("/user/info", func(context *gin.Context) {
userId := context.Query("userId")
userName := context.Query("userName")
context.JSON(200, gin.H{
"userId": userId,
"userName": userName,
})
})
ginServer.GET("/user/info/:userId/:userName", func(context *gin.Context) {
userId := context.Param("userId")
userName := context.Param("userName")
context.JSON(200, gin.H{
"userId": userId,
"userName": userName,
})
})
//前端传递JSON
ginServer.POST("/json", func(context *gin.Context) {
//request.body
b, _ := context.GetRawData()
var m map[string]interface{}
_ = json.Unmarshal(b, &m)
context.JSON(http.StatusOK, m)
})
//支持函数式编程
ginServer.POST("/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.Run(":8082")
}
6.路由
7.中间件(拦截器)
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
import "github.com/thinkerou/favicon"
//自定义Go中间件 拦截器
func myHandler() gin.HandlerFunc {
return func(context *gin.Context) {
//通过自定义的中间件,设置的值,在后续处理只要调用了这个中间件都可以拿到这里的参数
context.Set("usersession", "userid-1")
//放行
context.Next()
//阻断
//context.Abort()
}
}
func main() {
//创建一个服务
ginServer := gin.Default()
ginServer.Use(favicon.New("./avter.png"))
//加载静态页面
ginServer.LoadHTMLGlob("templates/*")
//加载资源文件
ginServer.Static("/static", "./static")
//连接数据库的代码
// 访问地址,处理我们的请求 Request Response
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.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{
"msg": "这是go后台传递的数据",
})
})
// ? userid=xxx&username=zjzaki
// user/info/1/zjzaki
ginServer.GET("/user/info", myHandler(), func(context *gin.Context) {
//取出中间件的值
userSession := context.MustGet("usersession").(string)
log.Println(userSession)
userId := context.Query("userId")
userName := context.Query("userName")
context.JSON(200, gin.H{
"userId": userId,
"userName": userName,
})
})
ginServer.GET("/user/info/:userId/:userName", func(context *gin.Context) {
userId := context.Param("userId")
userName := context.Param("userName")
context.JSON(200, gin.H{
"userId": userId,
"userName": userName,
})
})
//前端传递JSON
ginServer.POST("/json", func(context *gin.Context) {
//request.body
b, _ := context.GetRawData()
var m map[string]interface{}
_ = json.Unmarshal(b, &m)
context.JSON(http.StatusOK, m)
})
//支持函数式编程
ginServer.POST("/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.NoRoute(func(context *gin.Context) {
context.HTML(http.StatusNotFound, "404.html", nil)
})
ginServer.GET("/test", func(context *gin.Context) {
context.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
})
//路由组
userGroup := ginServer.Group("/user")
{
userGroup.GET("/add")
userGroup.GET("/login")
}
orderGroup := ginServer.Group("/order")
{
orderGroup.GET("/add")
orderGroup.GET("/login")
}
//服务器端口
ginServer.Run(":8082")
}
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果