当前位置: 首页 > wzjs >正文

做黑彩网站赚钱吗谷歌google play官网下载

做黑彩网站赚钱吗,谷歌google play官网下载,wordpress mysql分离,icp备案号查询官网一、概述 在 Gin 框架中,中间件(Middleware)是一种在处理 HTTP 请求之前或之后执行的函数。使用中间件可以方便地实现诸如身份验证(Auth)、日志记录、请求限流等功能。 二、编写中间件 // 中间件 func StartCost1(c …

一、概述

        在 Gin 框架中,中间件(Middleware)是一种在处理 HTTP 请求之前或之后执行的函数。使用中间件可以方便地实现诸如身份验证(Auth)、日志记录、请求限流等功能。

二、编写中间件

// 中间件
func StartCost1(c *gin.Context) {log.Info("......start1......")start := time.Now()c.Next() //调用后续的处理函数// c.Abort() //阻止调用后续的处理函数cost := time.Since(start)log.Info("cost1:", cost)log.Info("......end1......")
}func StartCost2(c *gin.Context) {log.Info("......start2......")log.Info("......end2......")
}func authMiddleware(doCheck bool) gin.HandlerFunc {return func(ctx *gin.Context) {if doCheck {//检查用户信息ctx.Set("token", "20240814")// TODOlog.Info("检查用户登录信息")ctx.Next()} else {//不检查用户信息log.Error("不检查用户信息即可处理")ctx.Next()}}
}

三、使用中间件

func main() {r := gin.Default()r.Use(StartCost1, StartCost2, authMiddleware(true))r.LoadHTMLFiles("./index.html", "./home.html")r.GET("/web", func(c *gin.Context) {name := c.Query("query")c.JSON(http.StatusOK, gin.H{"name": name,})})r.GET("/login", func(ctx *gin.Context) {ctx.HTML(http.StatusOK, "index.html", nil)})r.GET("/home", func(ctx *gin.Context) {ctx.Request.URL.Path = "/b"r.HandleContext(ctx)})r.GET("/b", func(ctx *gin.Context) {log.Info("request /b GetLocalIP: " + GetLocalIP())ctx.Redirect(http.StatusMovedPermanently, GetLocalIP()+":9090/login")})r.POST("/login", func(ctx *gin.Context) {log.Info("request /login GetLocalIP: " + GetLocalIP())username := ctx.PostForm("username")password := ctx.PostForm("password")//从请求中获取携带的参数f, err := ctx.FormFile("file")if err != nil {ctx.JSON(http.StatusBadRequest, gin.H{"error": err,})} else {filePath := path.Join("./", f.Filename)ctx.SaveUploadedFile(f, filePath)ctx.HTML(http.StatusOK, "home.html", gin.H{"Name":     username,"Password": password,"Token":    ctx.GetString("token"),})}})r.Run(":9090")}

四、后端完整代码

package mainimport ("net""net/http""path""time""github.com/gin-gonic/gin"log "github.com/sirupsen/logrus"
)func GetLocalIP() string {ip := "127.0.0.1"addrs, err := net.InterfaceAddrs()if err != nil {return ip}for _, a := range addrs {if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {if ipnet.IP.To4() != nil {ip = ipnet.IP.String()break}}}return ip}// 中间件
func StartCost1(c *gin.Context) {log.Info("......start1......")start := time.Now()c.Next() //调用后续的处理函数// c.Abort() //阻止调用后续的处理函数cost := time.Since(start)log.Info("cost1:", cost)log.Info("......end1......")
}func StartCost2(c *gin.Context) {log.Info("......start2......")log.Info("......end2......")
}func authMiddleware(doCheck bool) gin.HandlerFunc {return func(ctx *gin.Context) {if doCheck {//检查用户信息ctx.Set("token", "20240814")// TODOlog.Info("检查用户登录信息")ctx.Next()} else {//不检查用户信息log.Error("不检查用户信息即可处理")ctx.Next()}}
}func main() {r := gin.Default()r.Use(StartCost1, StartCost2, authMiddleware(true))r.LoadHTMLFiles("./index.html", "./home.html")r.GET("/web", func(c *gin.Context) {name := c.Query("query")c.JSON(http.StatusOK, gin.H{"name": name,})})r.GET("/login", func(ctx *gin.Context) {ctx.HTML(http.StatusOK, "index.html", nil)})r.GET("/home", func(ctx *gin.Context) {ctx.Request.URL.Path = "/b"r.HandleContext(ctx)})r.GET("/b", func(ctx *gin.Context) {log.Info("request /b GetLocalIP: " + GetLocalIP())ctx.Redirect(http.StatusMovedPermanently, GetLocalIP()+":9090/login")})r.POST("/login", func(ctx *gin.Context) {log.Info("request /login GetLocalIP: " + GetLocalIP())username := ctx.PostForm("username")password := ctx.PostForm("password")//从请求中获取携带的参数f, err := ctx.FormFile("file")if err != nil {ctx.JSON(http.StatusBadRequest, gin.H{"error": err,})} else {filePath := path.Join("./", f.Filename)ctx.SaveUploadedFile(f, filePath)ctx.HTML(http.StatusOK, "home.html", gin.H{"Name":     username,"Password": password,"Token":    ctx.GetString("token"),})}})r.Run(":9090")}

五、前端页面

1、index.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Form</title>
</head><body><form action="/login" method="post" enctype="multipart/form-data"><div><label for="username">用户名:</label><input type="text" name="username" id="username" /></div><div><label for="password">密 码:</label><input type="text" name="password" id="password" /></div><div><label for="file">上传文件:</label><input type="file" name="file" id="file" /></div><div><input type="submit" value="提交"></div></form></body></html>

2、home.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>用户信息</title>
</head><body><h1> 您好,{{.Name}}</h1><span>密码:{{.Password}}</span><span>Token:{{.Token}}</span>
</body></html>

六、运行代码

go run main.go

七、展示

http://www.dtcms.com/wzjs/187698.html

相关文章:

  • 做水果为主的b2c网站有哪些5000人朋友圈推广多少钱
  • 华为网络服务商江西seo推广方案
  • 专业微信网站开发公司输入关键词进行搜索
  • 企业网站建设的意义seo优缺点
  • 网站cms系统排名百度快照收录
  • 网站建设的目标定义网络推广与网络营销的区别
  • 有没有像一起做网店做男装的网站网络排名优化软件
  • 网页设计图片位置代码上海百度整站优化服务
  • 网站建设科技有限公司如何做好搜索引擎优化工作
  • 湘潭网络推广公司seo专员工资待遇
  • 做网站推广的联系方式手机最新产品新闻
  • 响应式网站设计的优点域名查询ip网站
  • 免费进b站2023推广任务接单平台
  • 柳州做网站价格网络推广公司方案
  • wix网站做图片能折叠吗软文100字左右案例
  • 手机公司网站建设做一个自己的网站
  • 写简历的网站最专业的seo公司
  • 美丽乡村网站建设策划书seo关键词优化技巧
  • wordpress仿论坛主题漯河搜狗关键词优化排名软件
  • 软件开发中seo公司多少钱
  • 做网站虚拟主机有没有专门做营销的公司
  • 职业生涯规划网站开发背景北京百度关键词推广
  • 福步外贸论坛注册南宁seo费用服务
  • 帝国cms做新闻网站企业建站平台
  • 茂名网站建设服务seo学徒
  • tech域名可以做网站吗交换友链要注意什么
  • 网站制作网络推广方案网页制作平台有哪些
  • 西安免费做网站公司聊城优化seo
  • 手机网站建设系统优化seo网站
  • 做pc端网站渠道百度广告位价格表