0
我正在使用httprouter来设置API端点,但我在正确输出响应时遇到了一些麻烦。为什么我不能在Go中写一个独立包的HTTP响应?
我的主要文件,因为它代表:
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"infrastructure/routing"
"log"
"net/http"
)
func testPrint(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "Test output.")
}
func main() {
fmt.Printf("\n************************************\n")
fmt.Printf("MySite Application Server\n")
fmt.Printf("Version: %s", "Unversioned Development Build\n")
fmt.Printf("************************************\n\n")
router := httprouter.New()
//////Records//////
router.GET("/test", testPrint)
router.GET("/records", routing.RecordList)
router.GET("/records/id/:id", routing.RecordGetById)
router.GET("/records/name/:name", routing.RecordGetByName)
router.GET("/records/search/*search-string", routing.RecordSearch)
router.HEAD("/records/:id", routing.RecordGetExists)
router.POST("/records", routing.RecordCreate)
router.PATCH("/records", routing.RecordUpdate)
router.PUT("/records", routing.RecordUpsert)
router.DELETE("/records/:id", routing.RecordDelete)
router.DELETE("/records/:id/undelete", routing.RecordUndelete)
router.DELETE("/records/:id/purge", routing.RecordPurge)
fmt.Printf("Listening on port 8080...\n")
log.Fatal(http.ListenAndServe(":8080", router))
}
大部分的处理函数位于infrastructure/routing
,但是这是问题的开始。路由代码当前:
package routing
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
)
func RecordList(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
//Returns a list of records.
fmt.Fprint(w, "Returned a list of records.")
}
func RecordGetById(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Return a record with the specified ID.
w.Write([]byte("Returned a record by its ID."))
}
func RecordGetByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Returns a record with the specified string in the name field.
w.Write([]byte("Returned a record by its name."))
}
func RecordSearch(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Full text search for the search term provided.
w.Write([]byte("Search results."))
}
func RecordGetExists(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Returns whether a record exists.
w.Write([]byte("Returns whether a record exists. Should usually return 200 or 404."))
}
func RecordCreate(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Creates new records from JSON data in request body.
w.Write([]byte("Creates new records."))
}
func RecordUpdate(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Updates records using JSON data in request body.
w.Write([]byte("Updates existing records."))
}
func RecordUpsert(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Updates existing records and creates new ones using JSON data in request body.
w.Write([]byte("Upserts records."))
}
func RecordDelete(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Marks the record with the specified ID as deleted.
w.Write([]byte("Marks records as deleted."))
}
func RecordUndelete(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Marks the record with the specified ID as undeleted.
w.Write([]byte("Marks deleted records as undeleted."))
}
func RecordPurge(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//Permanently deletes the record with the specified ID.
w.Write([]byte("Permanently deletes record with given ID."))
}
当我主动要求本地主机:8080 /测试,它调用主包和图案处理函数“测试输出”。但是任何其他定义的URL都会返回一个200 OK
状态码和一个空的(0字节)响应正文,而不是预期的文本。
我相当肯定这是东西做的ResponseWriter
对象通过传递,但我想不通为什么它在不同的包适用于一个功能在同一个包,但没有其他功能,即使尽管这两个函数都有一个通过作者的副本。我已经尝试了几种不同的写作方式,包括fmt.FPrintf
,io.WriteString
和w.Write
,都无济于事。
这应该工作。确保您的'infrastructure/routing'软件包的安装版本是最新的。 – JimB
这是我自己的软件包,我已经完整地发布了它。我不明白这个问题可能是什么,如果它从一个函数在同一个包中,而不是从另一个包中的函数运行。 – leylandski
是的,我的意思是确保安装的目标文件是'go install infrastructure/routing'的最新版本(尽管如果这样做的话,知道它为什么不*重建源代码会很有趣)。当您构建/安装'main'包时,您还可以使用'-x'标志查看到底发生了什么。 – JimB