Get single message API

This commit is contained in:
Ian Kent 2014-04-20 20:01:53 +01:00
parent 9a6426f359
commit a759177090
2 changed files with 16 additions and 4 deletions

View file

@ -24,9 +24,10 @@ func CreateAPIv1(exitCh chan int, conf *mailhog.Config, server *http.Server) *AP
server: server,
}
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages$"), apiv1.messages)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/delete$"), apiv1.delete_all)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/delete$"), apiv1.delete_one)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/?$"), apiv1.messages)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/delete/?$"), apiv1.delete_all)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/?$"), apiv1.message)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/delete/?$"), apiv1.delete_one)
return apiv1
}
@ -41,6 +42,17 @@ func (apiv1 *APIv1) messages(w http.ResponseWriter, r *http.Request, route *hand
w.Write(bytes)
}
func (apiv1 *APIv1) message(w http.ResponseWriter, r *http.Request, route *handler.Route) {
match := route.Pattern.FindStringSubmatch(r.URL.Path)
id := match[1]
log.Printf("[APIv1] GET /api/v1/messages/%s\n", id)
message, _ := storage.Load(apiv1.config, id)
bytes, _ := json.Marshal(message)
w.Header().Set("Content-Type", "text/json")
w.Write(bytes)
}
func (apiv1 *APIv1) delete_all(w http.ResponseWriter, r *http.Request, route *handler.Route) {
log.Println("[APIv1] POST /api/v1/messages/delete")

View file

@ -52,7 +52,7 @@ func Start(exitCh chan int, conf *mailhog.Config) {
Handler: &handler.RegexpHandler{},
}
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/exit$"), web_exit)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/exit/?$"), web_exit)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/js/controllers.js$"), web_jscontroller)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/images/hog.png$"), web_imgcontroller)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/$"), web_index)