Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/edermanoel94/rest-go
A package that provide many helpful methods for working with rest api.
https://github.com/edermanoel94/rest-go
Last synced: about 1 month ago
JSON representation
A package that provide many helpful methods for working with rest api.
- Host: GitHub
- URL: https://github.com/edermanoel94/rest-go
- Owner: edermanoel94
- License: mit
- Created: 2019-07-29T18:56:08.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-16T04:57:53.000Z (over 4 years ago)
- Last Synced: 2024-04-22T13:32:26.755Z (8 months ago)
- Language: Go
- Homepage:
- Size: 82 KB
- Stars: 16
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-go-extra - rest-go - 07-29T18:56:08Z|2020-08-16T04:57:53Z| (Utilities / Fail injection)
README
Rest GO - Helpful library for Rest API
======================================[![Build Status](https://travis-ci.org/edermanoel94/rest-go.svg?branch=master)](https://travis-ci.org/edermanoel94/rest-go)
[![eder](https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/github.com/edermanoel94/rest-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/edermanoel94/rest-go)](https://goreportcard.com/report/github.com/edermanoel94/rest-go)
[![codecov](https://codecov.io/gh/edermanoel94/rest-go/branch/master/graph/badge.svg)](https://codecov.io/gh/edermanoel94/rest-go)A package that provide many helpful methods for working with rest api.
And if u are tired of written, this:
```go
func SomeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
product := &product{"Smart TV", 50.00}
bytes, err := json.Marshal(product)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
// this is super bad!
message := fmt.Sprintf(`{"message": "%s"}`, err.Error())
w.Write([]byte(message))
return
}
w.WriteHeader(http.StatusOk)
w.Write(bytes)
}
```Get started:
* Install rest GO with [one line of code](#installation)
[`rest`](http://godoc.org/github.com/edermanoel94/rest-go "API documentation") package
-------------------------------------------------------------------------------------------The `rest` package provides some helpful methods that allow you to write better rest api in GO.
* Allows for very readable code
See it in action:
```go
package yoursimport (
"github.com/edermanoel94/rest-go"
"net/http"
)type product struct {
Name string `json:"name"`
Price float32 `json:"price"`
}func SomeHandler(w http.ResponseWriter, r *http.Request) {
rest.Marshalled(w, &product{"Smart TV", 50.00}, http.StatusOK)
}
```A payload send to your API and desarialize to a struct, to easy!
```go
package yoursimport (
"github.com/edermanoel94/rest-go"
"net/http"
)type product struct {
Name string `json:"name"`
Price float32 `json:"price"`
}// [POST] body: {"name": "eder", "price": 20.00}
func SomePostHandler(w http.ResponseWriter, r *http.Request) {product := product{}
err := rest.GetBody(r.Body, &product)
if err != nil {
// do stuff with error
}
// Do stuff...
}
```Working with [`mux`](https://github.com/gorilla/mux "API documentation") package to check if path variable exist.
```go
package yoursimport (
"github.com/edermanoel94/rest-go"
"github.com/gorilla/mux"
"net/http"
)type product struct {
Name string `json:"name"`
Price float32 `json:"price"`
}// [GET] url: /product/{id}
func SomePostHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
err := rest.CheckPathVariables(params, "id")
if err != nil {
// do stuff with error
}
}
```TODO List
=========- [x] Working with custom errors
- [ ] Add Response for ALB and API Gateway
- [ ] Benchmarking (Memory, CPU)
- [ ] Working with CheckPathVariables and GetPathVariable in Standard library
- [ ] More tests
- [ ] Working with paginationInstallation
============To install, use `go get` using go modules:
```
go get github.com/edermanoel94/rest-go@latest
```Example
=======To install and build:
```
cd examples
```And use go modules for install packages:
```
go mod tidy
```Then, to run:
```
go build
```Contributing
============Please feel free to submit issues, fork the repository and send pull requests!
------
License
=======This project is licensed under the terms of the MIT license.