日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

golang mysql封装_golang如何封装路由

發布時間:2025/3/8 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 golang mysql封装_golang如何封装路由 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

封裝方式一、路由寫在 main函數中,數據庫初始連接放在 init() 函數中。、

首先看 main.go

一個初始化函數,初始化 dbfunc init() {

db.Connect()

}

第二,路由func main() {

// Configure

router := gin.Default()

// Set html render options

htmlRender := GinHTMLRender.New()

htmlRender.Debug = gin.IsDebugging()

htmlRender.Layout = "layouts/default"

// htmlRender.TemplatesDir = "templates/" // default

// htmlRender.Ext = ".html" // default

// Tell gin to use our html render

router.HTMLRender = htmlRender.Create()

router.RedirectTrailingSlash = true

router.RedirectFixedPath = true

// Middlewares

router.Use(middlewares.Connect)

router.Use(middlewares.ErrorHandler)

// Statics

router.Static("/public", "./public")

// Routes

router.GET("/", func(c *gin.Context) {

c.Redirect(http.StatusMovedPermanently, "/articles")

})

// Articles

router.GET("/new", articles.New)

router.GET("/articles/:_id", articles.Edit)

router.GET("/articles", articles.List)

router.POST("/articles", articles.Create)

router.POST("/articles/:_id", articles.Update)

router.POST("/delete/articles/:_id", articles.Delete)

// Start listening

port := Port

if len(os.Getenv("PORT")) > 0 {

port = os.Getenv("PORT")

}

router.Run(":" + port)

}

封裝方式二、路由和 db單獨封裝到一個函數中,以傳參的形式將路由傳給另一個函數。

func SetUpServer() {

r := gin.Default()

// secret 相當于簽名

store, _ := sessions.NewRedisStore(10, "tcp", CONFIG.Redis.Host, CONFIG.Redis.Password, []byte(CONFIG.Server.Secret))

r.Use(sessions.Sessions("session", store))

r.Use(gin.Recovery())

r.Use(Uniquify())

r.LoadHTMLGlob("templates/*")

r.Static("/static", "public/")

SetUpRoutes(r)

r.GET("/incr", func(c *gin.Context) {

c.JSON(200, gin.H{"count": c.MustGet("key").(string)})

})

// var port string

r.Run(":" + CONFIG.Server.Port)

}

封裝三、將路由等封裝到 Server struct 中。對此 Server 創建一個 new函數,路由等相關內容只包含在方法中。

type Server struct {

Ws *melody.Melody

Routes *gin.Engine

Games []*Game

}

func New() *Server {

ws := melody.New()

routes := gin.Default()

games := make([]*Game, 0)

return &Server{

Ws: ws,

Routes: routes,

Games: games,

}

}

...

...

func (srv *Server) RoutesInit() {

srv.Routes.GET("/jq.js", func(c *gin.Context) {

http.ServeFile(c.Writer, c.Request, "html/jq.js")

})

srv.Routes.GET("/goyaniv.js", func(c *gin.Context) {

http.ServeFile(c.Writer, c.Request, "html/goyaniv.js")

})

srv.Routes.GET("/game/:name", func(c *gin.Context) {

cookiekey, _ := c.Request.Cookie("goyanivkey")

cookieid, _ := c.Request.Cookie("goyanivid")

if cookieid == nil || cookiekey == nil {

cookieid := CreateCookie("goyanivkey", GenerateUnique())

cookiekey := CreateCookie("goyanivid", GenerateUnique())

http.SetCookie(c.Writer, cookieid)

http.SetCookie(c.Writer, cookiekey)

}

http.ServeFile(c.Writer, c.Request, "html/game.html")

})

srv.Routes.GET("/gamedev/:name", func(c *gin.Context) {

cookiekey, _ := c.Request.Cookie("goyanivkey")

cookieid, _ := c.Request.Cookie("goyanivid")

if cookieid == nil || cookiekey == nil {

cookieid := CreateCookie("goyanivkey", GenerateUnique())

cookiekey := CreateCookie("goyanivid", GenerateUnique())

http.SetCookie(c.Writer, cookieid)

http.SetCookie(c.Writer, cookiekey)

}

http.ServeFile(c.Writer, c.Request, "html/gamedev.html")

})

srv.Routes.GET("/game/:name/ws", func(c *gin.Context) {

srv.Ws.HandleRequest(c.Writer, c.Request)

})

srv.Routes.GET("/gamedev/:name/ws", func(c *gin.Context) {

srv.Ws.HandleRequest(c.Writer, c.Request)

})

srv.Ws.HandleMessage(func(s *melody.Session, msg []byte) {

FireMessage(srv, s, msg)

})

srv.Ws.HandleDisconnect(func(s *melody.Session) {

FireDisconnect(srv, s)

})

srv.Ws.HandleConnect(func(s *melody.Session) {

FireConnect(srv, s)

})

}

func (s *Server) Run() {

s.RoutesInit()

s.Routes.Run(":5000")

}

更多golang知識請關注PHP中文網golang教程欄目。

總結

以上是生活随笔為你收集整理的golang mysql封装_golang如何封装路由的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。