go web 

1.停掉一个http 服务

(1.)取消context
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
(2.)执行shutdown()
http.Server.Shutdown(ctx)

2.启动一个http服务

httpServer := &http.Server{
	Addr:              fmt.Sprintf(":%d", p.httpPort),
	Handler:           routers.Router,
	ReadHeaderTimeout: 5 * time.Second,
}

httpServer.ListenAndServe()

3.解析.ini文件

// 先获取章节,再获取相应的字段,并赋值默认值
*ini.File.Section("rtsp").Key("ffmpeg_path").MustString("")

4.md5加密

func MD5(str string) string {
	encoder := md5.New()
	encoder.Write([]byte(str))
	return hex.EncodeToString(encoder.Sum(nil))
}

5.session token

//创建token
sess := sessions.Default(c)
sess.Set("uid", user.ID)
sess.Set("uname", user.Username)
c.IndentedJSON(200, gin.H{
	"token": sessions.Default(c).ID(),
})

//获取token
sess := sessions.Default(c)
uid := sess.Get("uid")
if uid != nil {
	c.IndentedJSON(200, gin.H{
		"id":   uid,
		"name": sess.Get("uname"),
	})
}

//销毁session,退出登录
sess := sessions.Default(c)
sess.Destroy()

//修改密码后,token要重新赋值
sess := sessions.Default(c)
token, _ := sess.RenewID()
c.IndentedJSON(http.StatusOK, gin.H{
	"token": token,
})

猜你喜欢

转载自www.cnblogs.com/tomtellyou/p/12599550.html