https://github.com/ejunjsh/gorest
a restful framework with go
https://github.com/ejunjsh/gorest
Last synced: 29 days ago
JSON representation
a restful framework with go
- Host: GitHub
- URL: https://github.com/ejunjsh/gorest
- Owner: ejunjsh
- Created: 2017-07-04T02:56:32.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-01T05:36:17.000Z (almost 8 years ago)
- Last Synced: 2025-03-27T00:54:55.320Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 20
- Watchers: 0
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gorest
[](https://travis-ci.org/ejunjsh/gorest)[](http://www.babygopher.org)
a restful go framework
## install
````bash
go get github.com/ejunjsh/gorest
````
## usage
### import
````go
import "github.com/ejunjsh/gorest"
````
### create a app and run a server
````go
app:=gorest.NewApp()
app.[Get/Post/Delete/Put/Error]
app.Run(":8081")
````
### supports 4 methods of http request
````go
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 {...})
````
### supports parameters from url path
````go
app.Get("/:abc/:cba", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {
fmt.Println(r.PathParams["abc"],r.PathParams["cba"])
return nil
})
````
### supports string,json,xml,file,template as result of return
````go
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")
})
````
### supports dealing with errors
````go
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()))
}
})
````### see [example](https://github.com/ejunjsh/gorest/blob/master/main/main.go)