首先,我使用了github.com/ant0ine/go-json-rest/rest 这个包
直接贴代码
```go
func Start(){
NeptuneLog := logs.New()
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Get("/:type/:action", handle),
rest.Post("/:type/:action", handle),
)
if err != nil {
NeptuneLog.Fatal(err)
}
api.SetApp(router)
NeptuneLog.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}
```
=================start 负责调用 handle ====================
```go
func handle (w rest.ResponseWriter, r *rest.Request) {
NeptuneLog := logs.New()
t := r.PathParam("type")
if (t == "submit") {
action := r.PathParam("action")
submitNode := SubmitNode{R:r,W:w,Action:action}
SubmitChan <- submitNode
NeptuneLog.Printf("%v",w)
//node := test11{Action:action}
//w.WriteJson(node)
} else if t =="browser" {
action := r.PathParam("action")
browserNode := BrowserNode{R:r,W:w,Action:action}
BrowserChan <- browserNode
} else {
//w.WriteJson("not found")
}
}
```
============= handle 负责把request 和 response 放到结构体,然后放到channel ============
```go
func doSubmit () {
NeptuneLog := logs.New()
for item := range SubmitChan{
NeptuneLog.Println("111")
node := test11{Action:item.Action}
NeptuneLog.Printf("%v",node)
NeptuneLog.Printf("%v",item.W)
item.W.WriteJson(node)
NeptuneLog.Printf("%v",item.W)
}
}
```
=========== dosubmit 真正干活,负责从channel拿东西,然后返回 ======
调用之后,server打印出了
http: multiple response.WriteHeader calls
的错误
==========更新下最新进展 ========
我把打印的返回拿出来的
发现有 Conn.Write wrote more than the declared Content-Length 这个错误
更多评论
我又做了几次测试,我觉得错误应该是,handle函数退出会导致这个请求被置为完成的状态,然后这个ResponseWriter变量就不可用了,然后又再去读写这个变量,那么就挂掉了
#2