https://github.com/hoenirvili/rester
An opinionated library for building REST APIs in Go
https://github.com/hoenirvili/rester
api library rest rest-api rest-library
Last synced: 5 months ago
JSON representation
An opinionated library for building REST APIs in Go
- Host: GitHub
- URL: https://github.com/hoenirvili/rester
- Owner: hoenirvili
- License: mit
- Created: 2019-06-17T20:19:27.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-12T20:34:56.000Z (almost 6 years ago)
- Last Synced: 2024-06-20T12:49:15.010Z (about 2 years ago)
- Topics: api, library, rest, rest-api, rest-library
- Language: Go
- Homepage:
- Size: 174 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rester
[](https://travis-ci.com/hoenirvili/rester) [](https://godoc.org/github.com/hoenirvili/rester) [](https://goreportcard.com/report/github.com/hoenirvili/rester) [](https://coveralls.io/github/hoenirvili/rester?branch=master) 
An Opinionated library for building REST APIs in Go.
## Simple rest resource with one method without checking the permissions
```go
package main
import (
"net/http"
"github.com/hoenirvili/rester"
"github.com/hoenirvili/rester/request"
"github.com/hoenirvili/rester/resource"
"github.com/hoenirvili/rester/response"
"github.com/hoenirvili/rester/route"
)
type jsonResponse struct {
Message string `json:"message"`
}
type root struct{}
func (r *root) index(req request.Request) resource.Response {
return response.Payload(&jsonResponse{"Hello World !"})
}
func (r *root) Routes() route.Routes {
return route.Routes{{
URL: "/",
Method: resource.Get,
Handler: r.index,
}}
}
func main() {
rester := rester.New()
rester.Resource("/", new(root))
rester.Build()
if err := http.ListenAndServe(":8080", rester); err != nil {
panic(err)
}
}
```
## Using the query api to query url parameters
```go
package main
import (
"fmt"
"net/http"
"github.com/hoenirvili/rester"
"github.com/hoenirvili/rester/query"
"github.com/hoenirvili/rester/request"
"github.com/hoenirvili/rester/resource"
"github.com/hoenirvili/rester/response"
"github.com/hoenirvili/rester/route"
"github.com/hoenirvili/rester/value"
)
type root struct {
}
type message struct {
Str string `json:"message"`
}
func (r root) index(req request.Request) resource.Response {
if err := req.Query("page").Error(); err != nil {
return response.BadRequest("invalid page param")
}
page := req.Query("page").Int()
str := fmt.Sprintf("get request with query param page=%d", page)
return response.Payload(&message{str})
}
func (r root) Routes() route.Routes {
return route.Routes{{
URL: "/",
Method: http.MethodGet,
Handler: r.index,
QueryPairs: query.Pairs{
"page": query.Value{Type: value.Int},
},
}}
}
func main() {
rester := rester.New()
rester.Resource("/", new(root))
rester.Build()
if err := http.ListenAndServe(":8080", rester); err != nil {
panic(err)
}
}
```
### For more examples how to use the API, please visit the [examples](https://github.com/hoenirvili/rester/tree/master/examples) folder.