https://github.com/struckchure/xrpc
RPC Framework for Golang
https://github.com/struckchure/xrpc
golang grpc microservices rpc
Last synced: about 1 year ago
JSON representation
RPC Framework for Golang
- Host: GitHub
- URL: https://github.com/struckchure/xrpc
- Owner: struckchure
- License: mit
- Created: 2025-02-23T16:26:58.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-26T19:27:49.000Z (about 1 year ago)
- Last Synced: 2025-02-26T19:34:06.505Z (about 1 year ago)
- Topics: golang, grpc, microservices, rpc
- Language: Go
- Homepage: https://xrpc.vercel.app
- Size: 61.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xRPC
This project was inspired by [TRPC](https://trpc.io) and [Zod](https://zod.dev). It provides a simple and flexible way to define and validate input for HTTP procedures using [Echo](https://echo.labstack.com) as the HTTP server.
## Features
- **Validation**: Define validation rules for your input types.
- **Procedures**: Create query and mutation procedures with type-safe input and output.
- **Echo Integration**: Easily integrate with Echo to handle HTTP requests and responses.
## Installation
To install the library, run:
```bash
go get github.com/struckchure/xrpc
```
## Usage
### Define Your Types
Define the types for your input and output:
```go
type Post struct {
Id int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
}
type ListPostInput struct {
Skip *int `query:"skip" json:"skip"`
Limit *int `query:"limit" json:"limit"`
}
type CreatePostInput struct {
Title string `json:"title"`
Content string `json:"content"`
}
type GetPostInput struct {
Id *int `query:"id" json:"id"`
AuthorId string `query:"author_id" json:"author_id"`
}
```
### Create Procedures
Create procedures for listing posts, creating a post, and getting a post:
```go
func main() {
t := xrpc.InitTRPC()
listPostsProcedure := xrpc.NewProcedure[ListPostInput, []Post]("list").
Input(xrpc.NewValidator().
Field("Skip", xrpc.Number().Min(0).Required()).
Field("Limit", xrpc.Number().Max(10)),
).
Query(func(c xrpc.Context[ListPostInput, []Post]) error {
return c.Json(200, []Post{})
})
createPostProcedure := xrpc.NewProcedure[CreatePostInput, *Post]("create").
Input(xrpc.NewValidator().
Field("Title", xrpc.String().MinLength(10)).
Field("Content", xrpc.String().MinLength(10)),
).
Mutation(func(c xrpc.Context[CreatePostInput, *Post]) error {
return c.Json(201, &Post{})
})
getPostProcedure := xrpc.NewProcedure[GetPostInput, *Post]("get").
Input(xrpc.NewValidator().
Field("Id", xrpc.Number().Required()).
Field("AuthorId", xrpc.String()),
).
Query(func(c xrpc.Context[GetPostInput, *Post]) error {
return c.Json(200, &Post{})
})
listPostsProcedure(t)
createPostProcedure(t)
getPostProcedure(t)
t.Start(9090)
}
```
### Running the HTTP Server
The `main` function initializes the TRPC instance, defines the procedures, and starts the Echo HTTP server on port 9090.
## Custom Validation Library
The custom validation library provides a fluent API for defining validation rules. The library supports validation for various types such as strings and numbers, and allows specifying custom error messages.
### Example Usage
```go
validator := xrpc.NewValidator().
Field("Name", xrpc.String().MinLength(3).MaxLength(50)).
Field("Email", xrpc.String().Email()).
Field("Age", xrpc.Number().Min(18).Max(65))
err := validator.Validate(input)
if err != nil {
// handle validation error
}
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Acknowledgements
- [TRPC](https://xrpc.io)
- [Zod](https://zod.dev)
- [Echo](https://echo.labstack.com)