a restful go framework
go get github.com/ejunjsh/gorest
import "github.com/ejunjsh/gorest"
app:=gorest.NewApp()
app.[Get/Post/Delete/Put/Error]
app.Run(":8081")
app.Get("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...})
app.Post("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...})
app.Delete("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...})
app.Put("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...})
app.Get("/:abc/:cba", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {
fmt.Println(r.PathParams["abc"],r.PathParams["cba"])
return nil
})
app.Get("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {
return w.WriteJson(jsonObj)
//w.WriteString(str)
//w.WriteXml(xmlObj)
//w.WriteFile("/Users/zhouff/file.txt")
//w.WriteTemplates(data,"/Users/zhouff/index.html","/Users/zhouff/header.html")
})
app.Error(func(err error, r *gorest.HttpRequest, w gorest.HttpResponse){
if e,ok:=err.(gorest.NoFoundError);ok {
w.Write([]byte(e.Error()))
}
if e,ok:=err.(gorest.InternalError);ok {
w.Write([]byte(e.Error()))
}
})