https://github.com/ghmeier/go-service
HTTP service gateway in go.
https://github.com/ghmeier/go-service
gateway-microservice golang golang-tools services
Last synced: about 2 months ago
JSON representation
HTTP service gateway in go.
- Host: GitHub
- URL: https://github.com/ghmeier/go-service
- Owner: ghmeier
- License: mit
- Created: 2017-03-29T16:28:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-09T02:59:31.000Z (about 9 years ago)
- Last Synced: 2025-08-14T19:01:19.089Z (11 months ago)
- Topics: gateway-microservice, golang, golang-tools, services
- Language: Go
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Service
[](https://travis-ci.org/ghmeier/go-service)
[](https://coveralls.io/github/ghmeier/go-service)
[](https://goreportcard.com/report/github.com/ghmeier/go-service)
[](http://godoc.org/github.com/ghmeier/service)
HTTP service gateway in go. A simple library to send requests to external services without marshaling JSON yourself.
## Installation
```
go get github.com/ghmeier/go-service
```
## Examples
*Note* All fields of the receiveing data type must be exported.
Sending one `GET` request:
```
package example
import "github.com/ghmeier/go-service"
type Foo struct {
name string `json:"name"`
}
func Get() {
s := service.New()
var f Foo
err := s.Send(&service.Request{
Method: "GET",
URL: "http://some.url.com/foo",
}, &f)
}
```
The default response will fill `f` with the object in the `data` field of the response and return an error if Response.`success` is `false`.
Sending one `POST` request:
```
package example
import "github.com/ghmeier/go-service"
func Post() {
s := service.New()
f := &Foo{}
err := s.Send(&service.Request{
Method: "POST",
URL: "http://some.url.com/foo",
Data: f,
}, nil)
}
```
Check out `examples.go` for an example of implementing a service gateway using `go-service`.
## Custom Response
If you want to handle responses from services with different repsponse types, implement the `service.Responder` and `service.Response` interfaces.
### Example Custom Responder/Response:
```
type CustomResponder struct{}
//Marshal should return a new response based on an http response. Usually this Unmarshals the body.
func (c *CustomResponder) Marshal(r *http.Response) (service.Response, error) {
res := &CustomResponse{}
res.OK = r.StatusCode == 200
res.Status = r.Status
return res
}
type CustomResponse struct {
Status string `json:"status"`
OK bool `json:"ok"`
}
//Error should return an error based on the CustomResponse state
func (c *CustomResponse) Error() error {
if !c.OK {
return fmt.Errorf(c.Status)
}
return nil
}
//Body should return a []byte that can be marshalled into the second argument of Service.Send()
func (c *CustomResponse) Body() ([]byte, error) {
return json.Marshal(c)
}
```
Using Custom Responder:
```
s := service.NewCustom(&CustomResponder{})
```