Go basic learning record - write Web application - Some Thoughts on Routing and program startup

Keywords: Go github git

In recent years, I have reconsidered my Web application. First of all, in the main.go file of program startup, there is not too much problem in the temporary route adding. However, according to the previous project development experience, if this system is shared with others, then when making a large project, there will be many routes added, and then the file will become larger and larger, The key is that the route is also put in the main.go file, which is too troublesome, so I made some changes today.

Every time I put the code I practice on github and I will type a tag for the convenience of the students who use it later. Here I share the code in the following branches

https://github.com/durban89/typescript_demo.git
tag: 1.0.12

1. Change the main.go file

Original

http.HandleFunc("/view/", helpers.MakeHandler(controllers.ArticleView))
http.HandleFunc("/save/", helpers.MakeHandler(controllers.ArticleSave))
http.HandleFunc("/edit/", helpers.MakeHandler(controllers.ArticleEdit))
http.HandleFunc("/upload/", controllers.UploadHandler)
http.HandleFunc("/postFile/", controllers.PostFileHandler)

Replace with the following code

router.Routes()

In this way, we divide the logic of routing into a file responsible for routing, so that the main.go file looks simple.

2. Add router.go

Create the file router/router.go, and add the following code

package router

import (
    "net/http"

    "github.com/durban89/wiki/controllers"
)

// RouterMap route
type RouterMap struct {
    Path string
    Fn   func(http.ResponseWriter, *http.Request)
}

// RouterMaps route list
var RouterMaps = []*RouterMap{
    {
        Path: "/view/",
        Fn:   controllers.ArticleViewWithID,
    },
    {
        Path: "/save/",
        Fn:   controllers.ArticleViewWithID,
    },
    {
        Path: "/edit/",
        Fn:   controllers.ArticleViewWithID,
    },
    {
        Path: "/upload/",
        Fn:   controllers.UploadHandler,
    },
    {
        Path: "/postFile/",
        Fn:   controllers.PostFileHandler,
    },
}

// Routes operation
func Routes() {
    for i := 0; i < len(RouterMaps); i++ {
        cRoute := RouterMaps[i]
        http.HandleFunc(cRoute.Path, cRoute.Fn)
    }
}

From the above code, we can see that the routing management is a little better, which also achieves the purpose of optimizing the main.go file.

3, test

Add the following functions to the controllers/article.go file

func ArticleViewWithID(w http.ResponseWriter, r *http.Request) {
    if strings.ToLower(r.Method) == "get" {
        var validPath = regexp.MustCompile("^/(view)/([a-zA-Z0-9]+)$")
        m := validPath.FindStringSubmatch(r.URL.Path)
        if m == nil {
            http.NotFound(w, r)
            return
        }

        // Get article title or article ID
        fmt.Println(m[2:])

        fmt.Fprintf(w, "Welcome to the home page!")
        return
    }

    http.NotFound(w, r)
    return

}

After adding, recompile the project and run it. Everything is OK.

Project update address

https://github.com/durban89/typescript_demo.git
tag: 1.1.0

Posted by TechGnome on Sat, 14 Dec 2019 09:54:08 -0800