Skip to content

Commit

Permalink
merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Jan 3, 2024
2 parents c33de21 + d2a8454 commit c8ca49e
Show file tree
Hide file tree
Showing 161 changed files with 6,449 additions and 2,121 deletions.
3 changes: 3 additions & 0 deletions .fleet/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.guides": []
}
15 changes: 8 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.20', '1.21' ]
name: Set up Go ${{ matrix.go }}
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- uses: actions/checkout@v4
- name: Test
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
run: |
go work init
go work use ./kit
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,4 @@ Temporary Items

_*
*.pprof
go.work
go.work.sum
186 changes: 12 additions & 174 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,188 +3,26 @@ RonyKIT is a set of tools that are designed to be extendable and flexible to wri
When you develop API handlers using RonyKIT framework, you can support RPC and REST style APIs without re-writing the
endpoint layer multiple times.

Basically when you develop a handler in RonyKIT you just need to think about the input and output of your API, and define
these DTOs by defining appropriate structs.
If you are looking for a framework to write your API/Edge server, you can use `rony` package directly, which provides
an easy-to-use framework and with help of Go Generics, it provides a very clean and easy to use API. This is the
recommended way to use RonyKIT framework. However, if you want to have more control over your API server, and are
very concern about performance and memory usage, you can use `kit` package directly, which is the heart of the RonyKIT
framework.

For example if you want to implement an `Echo` handler you can write your handler like this:
```go
package main
- For more information about `rony` package, please visit [rony](./rony/README.MD)
- For more information about `kit` package, please visit [kit](./kit/README.MD)

import "github.com/clubpay/ronykit/kit"

type EchoRequest struct {
ID string `json:"Id"`
Timestamp int64 `json:"timestamp"`
}

type EchoResponse struct {
ID string `json:"Id"`
}

func echoHandler(ctx *kit.Context) {
req := ctx.In().GetMsg().(*EchoRequest)

ctx.In().
Reply().
SetMsg(
&EchoResponse{
ID: req.ID,
},
).Send()
}
```

Then you can write the ServiceDescriptor to define how your handler is going to be accessed. For example if
your `Echo` request is going to be access using REST API, you can define your service descriptor like this:

```go
package main

import (
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/desc"
"github.com/clubpay/ronykit/std/gateways/fasthttp"
)


var MyServiceDesc desc.ServiceDescFunc = func() *desc.Service {
return desc.NewService("MyServiceName").
SetEncoding(kit.JSON).
AddContract(
desc.NewContract().
SetInput(&EchoRequest{}).
SetOutput(&EchoResponse{}).
AddSelector(fasthttp.GET("/echo/:Id")).
AddSelector(fasthttp.POST("/echo")).
SetHandler(echoHandler),
)
}
```
In the ServiceDescriptor we defined two REST endpoint which our Handler would be served. RonyKIT's EdgeServer tries its best to fill the
input struct (i.e. EchoRequest) from the parameters: urlParam, queryParam or request's body

The last step is to set up our EdgeServer and bind our desired Gateway bundle and the `MyServiceDesc`
```go
package main

import (
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/desc"
"github.com/clubpay/ronykit/std/gateways/fasthttp"
)

type EchoRequest struct {
ID string `json:"Id"`
Timestamp int64 `json:"timestamp"`
}

type EchoResponse struct {
ID string `json:"Id"`
}

func echoHandler(ctx *kit.Context) {
req := ctx.In().GetMsg().(*EchoRequest)

ctx.In().
Reply().
SetMsg(
&EchoResponse{
ID: req.ID,
},
).Send()
}


var MyServiceDesc desc.ServiceDescFunc = func() *desc.Service {
return desc.NewService("MyServiceName").
SetEncoding(kit.JSON).
AddContract(
desc.NewContract().
SetInput(&EchoRequest{}).
SetOutput(&EchoResponse{}).
AddSelector(fasthttp.GET("/echo/:Id")).
AddSelector(fasthttp.POST("/echo")).
SetHandler(echoHandler),
)
}

func main() {
defer kit.NewServer(
kit.WithGateway(
fasthttp.MustNew(
fasthttp.Listen(":80"),
),
),
kit.WithServiceDesc(MyServiceDesc()),
).
Start(context.TODO()).
PrintRoutes(os.Stdout).
Shutdown(context.TODO(), os.Kill, os.Interrupt)
}
```
---

# RonyKIT Components

## 1. Handler
Handler is the function/method that accepts `kit.Context` as argument, and it will be called based on the
selector that is defined in the `desc.Service`.

## 2. Contract
Contract defines a business use-case that is very similar to what a function does, just it will be triggered
based on the selectors that we define the ServiceDescriptor.

## 3. Service
One or more Contracts that are doing some operations in a similar domain could be grouped in one Service. Each
EdgeServer can have one or more service attached.

## 4. Gateway
Gateways handle the endpoints of the service. For example, if you need to provide REST apis you can use one of the
standard gateway bundles : fasthttp or silverhttp
For advanced cases you can develop your own Gateway, but it is not necessary for most cases.

## 5. Cluster
Cluster defines the relation between different instances of the EdgeServer. This bundle is optional but if attached
to EdgeServer then in multi instance cases it can help and provides some facilities to share data between different
instances of your EdgeServer.

## 6. EdgeServer
EdgeServer is the main component of the RonyKIT which glues different components together to make it a working
server.

---

---

---
# RonyKIT Storage layers
When we develop API server usually we need to store data with different lifecycles. Sometimes we need to store data
that lives between different handlers, in some cases, we need to keep the data between different requests of a connection.
(i.e. for websocket connection we want to keep some data for next request from this connection).
Basically in RonyKIT we provide 4 layers of storage:

| Layer | Lifecycle |
|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Context | in the request, but available in all the handlers |
| Connection | in the connection, for REST apis usually the lifecycle of Connection storage and Context are same, but for websocket data will be available until websocket connection is active |
| Local | this is shared between different contracts, services and is in the local memory of the server |
| Cluster | this is shared between different instances of the EdgeServer. This is enabled ONLY if a Cluster bundle is attached |

---

---

---
# RonyKIT Standard Packages
This repository is holding all the projects related to RonyKIT. The main package which is the heart of
the RonyKIT framework is [kit](./kit) and the standard modules are in the /std folders.

| Package | BundleType | Version | Description |
|---------------|------------|----------|--------------------------------------------------------------------------------------------------------------------------------------|
| KIT | - | v0.10.26 | the main package of the RonyKIT framework |
| fasthttp | Gateway | v0.10.26 | the Gateway bundle implemented using [fasthttp](https://github.com/valyala/fasthttp) framework |
| fastws | Gateway | v0.10.26 | the Gateway bundle implemented using [gnet](https://github.com/panjf2000/gnet) and [gobwas](https://github.com/gobwas/ws) frameworks |
| silverhttp | Gateway | v0.10.26 | the Gateway bundle implemented using the new kid super-fast http server [silverlining](https://github.com/go-www/silverlining) |
| rediscluster | Cluster | v0.10.26 | the Cluster bundle implemented using [redis](https://github.com/go-redis/redis) |
| KIT | - | v0.11.15 | the main package of the RonyKIT framework |
| fasthttp | Gateway | v0.11.15 | the Gateway bundle implemented using [fasthttp](https://github.com/valyala/fasthttp) framework |
| fastws | Gateway | v0.11.15 | the Gateway bundle implemented using [gnet](https://github.com/panjf2000/gnet) and [gobwas](https://github.com/gobwas/ws) frameworks |
| silverhttp | Gateway | v0.11.15 | the Gateway bundle implemented using the new kid super-fast http server [silverlining](https://github.com/go-www/silverlining) |
| rediscluster | Cluster | v0.11.15 | the Cluster bundle implemented using [redis](https://github.com/go-redis/redis) |
| gossipcluster | Cluster | soon | Work in progress |

7 changes: 7 additions & 0 deletions cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ go fmt ./...
go vet ./...
GOWORK=off golangci-lint run

echo "Cleaning up [rony]..."
cd "$wd"/rony || exit
go mod tidy
go fmt ./...
go vet ./...
GOWORK=off golangci-lint run

echo "Cleaning up [redisCluster]..."
cd "$wd"/std/clusters/rediscluster || exit
go mod tidy
Expand Down
1 change: 1 addition & 0 deletions contrib/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ linters:
- structcheck
- gocritic
- exhaustive
- depguard
fast: false
linters-settings:
# decorder:
Expand Down
47 changes: 24 additions & 23 deletions contrib/go.mod
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
module github.com/clubpay/ronykit/contrib

go 1.19
go 1.20

require (
github.com/clubpay/ronykit/kit v0.10.36
github.com/clubpay/ronykit/std/gateways/fasthttp v0.10.36
github.com/go-openapi/spec v0.20.9
github.com/onsi/ginkgo/v2 v2.10.0
github.com/onsi/gomega v1.27.8
github.com/clubpay/ronykit/kit v0.11.30
github.com/clubpay/ronykit/std/gateways/fasthttp v0.11.30
github.com/go-openapi/spec v0.20.12
github.com/onsi/ginkgo/v2 v2.13.2
github.com/onsi/gomega v1.30.0
github.com/rbretecher/go-postman-collection v0.9.0
go.opentelemetry.io/contrib/propagators/b3 v1.16.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
go.opentelemetry.io/contrib/propagators/b3 v1.21.1
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/trace v1.21.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/fasthttp/websocket v1.5.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/fasthttp/router v1.4.22 // indirect
github.com/fasthttp/websocket v1.5.7 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/jsonpointer v0.20.1 // indirect
github.com/go-openapi/jsonreference v0.20.3 // indirect
github.com/go-openapi/swag v0.22.5 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-reflect v1.2.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.9 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/klauspost/compress v1.17.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.47.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.3 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit c8ca49e

Please sign in to comment.