我正在使用httprouter来设置API端点,但我无法正确输出响应。
我的主要档案:
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."))
}
当我向 localhost:8080 / test 发出请求时,它会调用主程序包中的处理函数并打印“Test output。”。但是,任何其他已定义的URL都会返回200 OK
状态代码和空(0字节)响应正文,而不是预期的文本。
我很确定这是某些东西与传递的ResponseWriter
对象有关但是我无法弄清楚为什么它适用于同一个包中的一个函数但不是另一个函数在另一个包中,即使两个函数都有一个传递的作者的副本。我尝试了几种不同的写作方式,包括fmt.FPrintf
,io.WriteString
和w.Write
,但无济于事。
答案 0 :(得分:1)
好吧,因为当我构建我的代码时出于某种原因,它并没有保持更新目标文件。我已经使用LiteIDE了一段时间,这是第一次单独点击所有相关软件包上的Build还没有工作。
我清理它然后使用Build All,现在它可以工作。