为什么我使用gorilla mux在这条golang路线上获得404?

时间:2017-03-07 21:50:23

标签: go routes http-status-code-404 gorilla

我去的时候得到404:

http://127.0.0.1:12345/list-issues/sortbycategory

当我遇到/ list-issues时,情况并非如此。我正确使用gorilla mux吗?通过查看示例,在我看来,我是。

main.go:

package main

import (
    "log"
    "net/http"
    "text/template"
    "my_proj/uservoice_app/controllers"
    "github.com/gorilla/mux"


)

func init() {
    controllers.Tpl = template.Must(template.ParseGlob("templates/*.html"))


    log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func main() {
    router := mux.NewRouter()
    http.HandleFunc("/hello", controllers.Hello_cont)
    http.HandleFunc("/write", controllers.Write_cont)
    http.HandleFunc("/test", controllers.Test)
    http.HandleFunc("/signup", controllers.Signup)
    http.HandleFunc("/create-issue", controllers.CreateIssue)
    http.HandleFunc("/list-issues", controllers.ListIssues)

    //ROUTER:
    router.HandleFunc("/list-issues/{sortby}", controllers.ListIssuesBy).Methods("GET")


    fs := http.FileServer(http.Dir("static"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))
    log.Fatal(http.ListenAndServe(":12345", nil))


}

列表issues.go

package controllers

import (
    "net/http"
    "database/sql"
    "github.com/dustin/go-humanize"
    "github.com/gorilla/mux"
    "fmt"
)

func ListIssuesBy(w http.ResponseWriter, r *http.Request) {

    vars := mux.Vars(r)
    sorter := vars["sortby"]
    if sorter == "sortbycategory" {

        fmt.Print("hello")

    }

    db, _ := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/mydb?parseTime=true")
    rows, _ := db.Query("SELECT * from issues")

    issuelist := []Issue{}
    for rows.Next() {
        var r Issue

        err := rows.Scan(&r.ID, &r.Title, &r.Description, &r.Category, &r.Username, &r.Votes_up, &r.Votes_down, &r.StartDate, &r.Status, &r.CommentsThrd)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
        HRDate2 := humanize.Time(r.StartDate.Local())
        r.HRSinceDate = HRDate2

        /*r.StartDate = HRDate*/
        issuelist = append(issuelist, r)
    }
    renderlistissues(w, issuelist)
}

func ListIssues(w http.ResponseWriter, r *http.Request) {
    db, _ := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/mydb?parseTime=true")
    rows, _ := db.Query("SELECT * from issues")

    issuelist := []Issue{}
    for rows.Next() {
        var r Issue

        err := rows.Scan(&r.ID, &r.Title, &r.Description, &r.Category, &r.Username, &r.Votes_up, &r.Votes_down, &r.StartDate, &r.Status, &r.CommentsThrd)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
        HRDate2 := humanize.Time(r.StartDate.Local())
        r.HRSinceDate = HRDate2

        /*r.StartDate = HRDate*/
        issuelist = append(issuelist, r)
    }
    renderlistissues(w, issuelist)

}

func renderlistissues(w http.ResponseWriter, data []Issue) {
    if err := Tpl.ExecuteTemplate(w, "list-issues", data); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }

}

0 个答案:

没有答案