Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acoshift/hrpc
Convert RPC style function into http.Handler
https://github.com/acoshift/hrpc
golang http-handler rpc
Last synced: about 2 months ago
JSON representation
Convert RPC style function into http.Handler
- Host: GitHub
- URL: https://github.com/acoshift/hrpc
- Owner: acoshift
- License: mit
- Created: 2017-03-18T10:56:47.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-10-19T08:53:10.000Z (over 2 years ago)
- Last Synced: 2024-11-17T12:10:00.735Z (2 months ago)
- Topics: golang, http-handler, rpc
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hrpc
![Build Status](https://github.com/acoshift/hrpc/actions/workflows/test.yaml/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/acoshift/hrpc/branch/master/graph/badge.svg?token=3AMuow5vXh)](https://codecov.io/gh/acoshift/hrpc)
[![Go Report Card](https://goreportcard.com/badge/github.com/acoshift/hrpc)](https://goreportcard.com/report/github.com/acoshift/hrpc)
[![GoDoc](https://pkg.go.dev/badge/github.com/acoshift/hrpc)](https://pkg.go.dev/github.com/acoshift/hrpc)Convert RPC style function into http.Handler
## Support input types
- context.Context
- *http.Request
- http.ResponseWriter
- any## Support output types
- any
- error## Usage
```go
import "github.com/acoshift/hrpc/v3"
```### Create new hrpc Manager
```go
m := hrpc.Manager{
Decoder: func(r *http.Request, dst any) error {
return json.NewDecoder(r.Body).Decode(dst)
},
Encoder: func(w http.ResponseWriter, r *http.Request, res any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(res)
},
ErrorEncoder: func(w http.ResponseWriter, r *http.Request, err error) {
res := &struct {
Error string `json:"error"`
}{err.Error()}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(res)
},
Validate: true,
}
```### RPC style function
```go
type UserRequest struct {
ID int `json:"id"`
}func (req *UserRequest) Valid() error {
// Valid will be called when decode, if set validate to true
if req.ID <= 0 {
return fmt.Errorf("invalid id")
}
return nil
}type UserResponse struct {
ID int `json:"id"`
Username string `json:"username"`
}http.Handle("/user.get", m.Handler(func(ctx context.Context, req *UserRequest) (*UserResponse, error) {
return &UserResponse{ID: 1, Username: "acoshift"}, nil
}))// or use non-ptr struct
http.Handle("/user.get2", m.Handler(func(ctx context.Context, req UserRequest) (res UserResponse, err error) {
res.ID = 1
res.Username = "acoshift"
return
}))
```### gRPC service client (generated from .proto)
```go
m.Handler(func(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*UserResponse, error) {
return nil, errors.New("not implemented")
})
```### Mixed types
```go
m.Handler(func(r *http.Request) error {
buf := &bytes.Buffer{}
_, err := io.Copy(buf, r.Body)
if err != nil {
return err
}
fmt.Printf("upload data: %s\n", buf.String())
return nil
})m.Handler(func(w http.ResponseWriter, r *http.Request) {})
m.Handler(func(ctx context.Context, r *http.Request, w http.ResponseWriter) {})
```