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

部队网站怎么做企点客服

部队网站怎么做,企点客服,建设网站南沙区,调用wordpress文章一、概述 在 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/172433.html

相关文章:

  • 企业融资渠道和融资方式有哪些汕头网站建设优化
  • 网站制作公司网站源码提高百度搜索排名工具
  • 音乐网站功能市场调研方法有哪几种
  • 做查询新生寝室的网站seo是怎么优化的
  • 做任务网站有哪些天天外链官网
  • 一级a做爰片免费网站无毒宁波网站建设制作报价
  • wordpress删除主题seo技术培训中心
  • wordpress建站的好处搜索网站排名优化
  • 相亲网站用什么做的谷歌chrome浏览器
  • wordpress 小工具插件下载地址晨阳seo顾问
  • 免费搭建个人博客网站网络营销推广策略有哪些
  • 南京政府网站建设谷歌浏览器直接打开
  • 如何低成本做网站推广一个网站如何推广
  • 蓝牙 技术支持 东莞网站建设网络推广网站推广淘宝运营商
  • 网页设计与制作教程的教学目标宁阳网站seo推广
  • 网站推广是干嘛的微信朋友圈广告投放
  • 兰州网站定制公司在线培训管理系统
  • 电商网站搜索引擎怎么做免费观看短视频的app软件推荐
  • seo整站优化网站建设花钱推广的网络平台
  • 网站服务器带宽多少合适百度游戏客服在线咨询
  • 做网站安全的公司有哪些seo推广知识
  • 西安市建设局网站南宁seo产品优化服务
  • 怎么添加网站内锚点百度指数查询
  • 网站建设的前景推广网站的文案
  • 深圳 网站优化公司排名seo搜索排名优化
  • 在新闻网站做采编杭州专业seo
  • 网站开发方式包括3步打造seo推广方案
  • wordpress前端投稿上传图片seo技术培训班
  • b2c电子商务网站功能模块百度推广代运营
  • 圣都装饰长沙市seo百度关键词