Skip to content

Commit

Permalink
cleanup and refactor std packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Feb 13, 2022
1 parent e13ef3c commit 562db44
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions desc/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (r *contractImpl) setModifier(modifiers ...ronykit.Modifier) *contractImpl
return r
}

func (r *contractImpl) Query(q string) interface{} {
return r.selector.Query(q)
func (r *contractImpl) Selector() ronykit.RouteSelector {
return r.selector
}

func (r *contractImpl) Handlers() []ronykit.Handler {
Expand Down
2 changes: 2 additions & 0 deletions exmples/simple-rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func main() {
fasthttp.Listen(":80"),
fasthttp.WithServerName("RonyKIT Server"),
fasthttp.WithCORS(fasthttp.CORSConfig{}),
fasthttp.WithWebsocketEndpoint("/ws"),
fasthttp.WithPredicateKey("cmd"),
),
),
ronykit.RegisterService(
Expand Down
15 changes: 5 additions & 10 deletions exmples/simple-rest-server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ func NewSample() *Sample {
return s
}

func (x *Sample) Desc() desc.Service {
d := desc.Service{
Name: "SampleService",
}

d.
func (x *Sample) Desc() *desc.Service {
return desc.NewService("SampleService").
Add(
desc.NewContract().
SetInput(&echoRequest{}).
AddSelector(fasthttp.Selector{
Method: fasthttp.MethodGet,
Path: "/echo/:randomID",
Method: fasthttp.MethodGet,
Predicate: "echo",
Path: "/echo/:randomID",
}).
AddSelector(fastws.Selector{
Predicate: "echoRequest",
Expand Down Expand Up @@ -66,8 +63,6 @@ func (x *Sample) Desc() desc.Service {
}).
SetHandler(sumRedirectHandler),
)

return d
}

func echoHandler(ctx *ronykit.Context) {
Expand Down
2 changes: 1 addition & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func WrapService(svc Service, wrappers ...ServiceWrapper) Service {
// In other words, Contract 'r' must return valid response for 'q's required by Bundle 'b' in
// order to be usable by Bundle 'b' otherwise it panics.
type Contract interface {
RouteSelector
Selector() RouteSelector
Input() Message
Handlers() []Handler
Modifiers() []Modifier
Expand Down
11 changes: 6 additions & 5 deletions std/bundle/fasthttp/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

const (
queryMethod = "fasthttp.Method"
queryPath = "fasthttp.path"
queryDecoder = "fasthttp.decoder"
queryMethod = "fasthttp.Method"
queryPath = "fasthttp.path"
queryDecoder = "fasthttp.decoder"
queryPredicate = "fasthttp.predicate"
)

type bundle struct {
Expand Down Expand Up @@ -99,7 +100,7 @@ func (b *bundle) Register(svc ronykit.Service) {
}

func (b *bundle) registerRPC(svcName string, c ronykit.Contract) {
rpcSelector, ok := c.(ronykit.RPCRouteSelector)
rpcSelector, ok := c.Selector().(ronykit.RPCRouteSelector)
if !ok {
return
}
Expand All @@ -116,7 +117,7 @@ func (b *bundle) registerRPC(svcName string, c ronykit.Contract) {
}

func (b *bundle) registerREST(svcName string, c ronykit.Contract) {
restSelector, ok := c.(ronykit.RESTRouteSelector)
restSelector, ok := c.Selector().(ronykit.RESTRouteSelector)
if !ok {
return
}
Expand Down
20 changes: 13 additions & 7 deletions std/bundle/fasthttp/options.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package fasthttp

type Option func(r *bundle)
type Option func(b *bundle)

func WithServerName(name string) Option {
return func(r *bundle) {
r.srv.Name = name
return func(b *bundle) {
b.srv.Name = name
}
}

func Listen(addr string) Option {
return func(r *bundle) {
r.listen = addr
return func(b *bundle) {
b.listen = addr
}
}

func WithCORS(cfg CORSConfig) Option {
return func(r *bundle) {
r.cors = newCORS(cfg)
return func(b *bundle) {
b.cors = newCORS(cfg)
}
}

Expand All @@ -25,3 +25,9 @@ func WithPredicateKey(key string) Option {
b.predicateKey = key
}
}

func WithWebsocketEndpoint(endpoint string) Option {
return func(b *bundle) {
b.wsEndpoint = endpoint
}
}
2 changes: 2 additions & 0 deletions std/bundle/fasthttp/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (r Selector) Query(q string) interface{} {
return r.Method
case queryPath:
return r.Path
case queryPredicate:
return r.Predicate
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions std/bundle/fastws/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func (b *bundle) Register(svc ronykit.Service) {
h = append(h, contract.Handlers()...)
h = append(h, svc.PostHandlers()...)

predicate, ok := contract.Query(queryPredicate).(string)
rpcSelector, ok := contract.Selector().(ronykit.RPCRouteSelector)
if !ok {
continue
}

b.routes[predicate] = routeData{
b.routes[rpcSelector.GetPredicate()] = routeData{
ServiceName: svc.Name(),
Predicate: predicate,
Predicate: rpcSelector.GetPredicate(),
Handlers: h,
Modifiers: contract.Modifiers(),
Factory: ronykit.CreateMessageFactory(contract.Input()),
Expand Down

0 comments on commit 562db44

Please sign in to comment.