一,简单服务
写一个简单的服务
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
//创建一个服务
ginServer:=gin.Default()
//访问地址,处理我们的请求 Request Response
ginServer.GET("/hello",func(ctx *gin.Context){
ctx.JSON(200,gin.H{"msg":"hello,world!"})
})
//服务器端口
ginServer.Run(":8888")
}
二,RestFul
Gin支持RestFul,包括GET,POST,PUT,DELETE
package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
func main() {
//创建一个服务
//导包:"github.com/gin-gonic/gin"
ginServer := gin.Default()
//更改网页图标
//导包"github.com/thinkerou/favicon"
ginServer.Use(favicon.New("./picture.ico"))
//Gin支持RestFul
ginServer.GET("/hello", func(ctx *gin.Context) {
ctx.JSON(200, gin.H{"msg": "hello,world!"})
})
ginServer.POST("/user", func(ctx *gin.Context) {
ctx.JSON(200, gin.H{"msg": "post user"})
})
//ginServer.PUT("/user")
//ginServer.DELETE("/user")
//服务器端口
ginServer.Run(":8888")
}
三,响应页面
需要加载静态资源(html,css,js)
项目下的templates/index.html文件
func main() {
//创建一个服务
ginServer := gin.Default()
//加载静态页面
//ginServer.LoadHTMLFiles("templates/index.html")//加载单个页面
ginServer.LoadHTMLGlob("templates/*") //加载文件夹里的所有页面
//响应一个页面给前端
ginServer.GET("/index", func(context *gin.Context) {
//context.JSON() //返回json数据
context.HTML(http.StatusOK,"index.html",gin.H{
"msg":"这是golang后台传递来的数据",
})
})
//服务器端口
ginServer.Run(":8888")
}
四,获取请求参数
获取浏览器传来的参数
func main() {
//创建一个服务
ginServer := gin.Default()
//获取请求参数
//方式一:
//url?userId=xxx&username=shen
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,
}) //返回给前端
})
//方式二:
// /user/info/userid/username
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.Run(":8888")
}
五,json的传递
func main() {
//创建一个服务
ginServer := gin.Default()
//前端给后端传递json数据
ginServer.POST("/json", func(context *gin.Context) {
//request.body
//[]byte
data, _ := context.GetRawData()
var m map[string]interface{}
//包装为json数据 []byte
_ = json.Unmarshal(data, &m)
context.JSON(http.StatusOK, m)
})
//服务器端口
ginServer.Run(":8888")
}
运行上面代码,利用postman发送信息
在终端得到返回的json数据
六,处理表单
修改项目下的templates/index.html文件
func main() {
//创建一个服务
ginServer := gin.Default()
//加载静态页面
//ginServer.LoadHTMLFiles("templates/index.html")//加载单个页面
ginServer.LoadHTMLGlob("templates/*") //加载文件夹里的所有页面
ginServer.GET("/index", func(context *gin.Context) {
//context.JSON() //返回json数据
context.HTML(http.StatusOK, "index.html", gin.H{
"msg": "这是golang后台传递来的数据",
})
})
//处理表单
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(":8888")
}
访问网址,提交数据
点击提交得到以下页面
七,路由和路由组
在tamplates下添加404.html
输入测试
localhost:8888/noroute 打开自定义404页面
localhost:8888/test 转到指定网址
func main() {
//创建一个服务
ginServer := gin.Default()
//加载静态页面
//ginServer.LoadHTMLFiles("templates/index.html")//加载单个页面
ginServer.LoadHTMLGlob("templates/*") //加载文件夹里的所有页面
//路由重定向
ginServer.GET("/test", func(context *gin.Context) {
//重定向 301
context.Redirect(http.StatusMovedPermanently, "https://www.kuangstudy.com")
})
//NoRoute 404
ginServer.NoRoute(func(context *gin.Context) {
context.HTML(http.StatusNotFound, "404.html", nil)
})
//路由组
userGroup := ginServer.Group("/user")
{
userGroup.GET("/add")
userGroup.POST("/login")
userGroup.POST("/logout")
}
orderGroup := ginServer.Group("/order")
{
orderGroup.GET("/add")
orderGroup.DELETE("/delete")
}
//服务器端口
ginServer.Run(":8888")
}
八,中间件
Go中间件-----拦截器(预处理 验证 授权 分页 耗时处理)
// 自定义Go中间件--拦截器(预处理 验证 授权 分页 耗时处理)
func myHandler() gin.HandlerFunc {
return func(context *gin.Context) {
//通过自定义的中间件,设置的值,在后续处理只要调用了这个中间件的都可以拿到这里的参数
context.Set("usersesion", "userid-1")
//if xxx{
// context.Abort()//阻止
//}
context.Next() //放行
}
}
func main() {
//创建一个服务
ginServer := gin.Default()
//注册中间件
ginServer.Use(myHandler())
//中间件--拦截器
ginServer.GET("user/info", myHandler(), func(context *gin.Context) {
//取出中间件的值
usersesion := context.MustGet("usersesion").(string)
log.Printf("=========>", usersesion)
userId := context.Param("userid")
userName := context.Param("username")
context.JSON(http.StatusOK, gin.H{
"userid": userId,
"username": userName,
})
})
//服务器端口
ginServer.Run(":8888")
}
终端返回以下数据