This is a creation in Article, where the information may have evolved or changed.
Overview
General Web project, main two points: Routing and database. The business logic is around these two points. Here are a few examples of the packaging logic of Web projects.
Encapsulation method One, the route is written in the main function, the database initial connection is placed in the init () function. Address
Func init () {db. Connect ()}func main () {//Configurerouter: = Gin. Default ()//Set HTML render Optionshtmlrender: = Ginhtmlrender.new () Htmlrender.debug = gin. Isdebugging () htmlrender.layout = "Layouts/default"//Htmlrender.templatesdir = "templates/"//default// Htmlrender.ext = ". html"//default//tell gin to the use of our HTML renderrouter. Htmlrender = htmlrender.create () router. Redirecttrailingslash = Truerouter. Redirectfixedpath = true//Middlewaresrouter.use (middlewares. Connect) router. Use (middlewares. ErrorHandler)//Staticsrouter.static ("/public", "./public")//Routesrouter.get ("/", Func (c *gin. Context) {C.redirect (http. statusmovedpermanently, "/articles")})//Articlesrouter.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 Listeningport: = Portif len (OS. Getenv ("PORT")); 0 {port = os. Getenv ("PORT")}router. Run (":" + Port)}
Encapsulation Method II, Routing and DB are encapsulated in a single function, and routed to another function in the form of a parameter. Address
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 stringr.Run(":" + CONFIG.Server.Port)}
Encapsulate three, encapsulate the route, etc. into the Server struct. Create a new function for this Server, and the related content such as routing is only included in the method. (Object-oriented) address
Type Server struct {Ws *melody. Melodyroutes *gin. Enginegames []*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) Ro Utesinit () {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")}