Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/connerdouglass/ezrpc
Tiny Go library for generic RPCs
https://github.com/connerdouglass/ezrpc
generics golang rpc
Last synced: 6 days ago
JSON representation
Tiny Go library for generic RPCs
- Host: GitHub
- URL: https://github.com/connerdouglass/ezrpc
- Owner: connerdouglass
- License: mit
- Created: 2022-02-23T17:37:45.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-05-25T14:37:29.000Z (over 2 years ago)
- Last Synced: 2024-11-30T07:26:31.534Z (2 months ago)
- Topics: generics, golang, rpc
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ezrpc
`ezrpc` is a super small Go library that uses the new Go 1.18 generics to enable RPC functionality.
I built this for myself so I can separate my application logic from gateway/HTTP logic.
## Example
Easily connect RPCs to your existing HTTP router:
```go
func main() {// Create a pipeline of middleware
rpc := ezrpc.
New().
Then(
func(ctx context.Context, _ *http.Request) (context.Context, error) {
log.Println("First middleware")
return ctx, nil
},
func(ctx context.Context, _ *http.Request) (context.Context, error) {
log.Println("Second middleware")
return ctx, nil
},
)// Create an HTTP mux and register the RPC hooks
mux := http.NewServeMux()
mux.Handle("/v1/sum", rpc.Handle(ezrpc.Handle(Sum)))// Serve the HTTP mux
http.ListenAndServe("127.0.0.1:8080", mux)}
```Define request and response types, and a handler function for each RPC hook:
```go
type sumRequest struct {
Numbers []int `json:"numbers"`
}type sumResponse struct {
Sum int `json:"sum"`
}// Sum calculates the sum of a slice of numbers
func Sum(ctx context.Context, req *sumRequest) (*sumResponse, error) {
var sum int
for _, number := range req.Numbers {
sum += number
}
return &sumResponse{Sum: sum}, nil
}
```## Contributions
Contributions are welcome and encouraged. Please feel free to submit PRs!
Some features that would be useful but don't yet exist:
- Middleware
- Optional support for non-JSON encodings (XML, YAML, etc.)
- Storing more metadata in `context` value