orion/cmd/server/jsonrpc/service.go

33 lines
850 B
Go
Raw Normal View History

2018-12-06 22:12:32 +01:00
package jsonrpc
import (
"github.com/gorilla/rpc"
2019-02-12 10:17:06 +01:00
"forge.cadoles.com/wpetit/goweb/service"
2018-12-06 22:12:32 +01:00
"github.com/pkg/errors"
)
const ServiceName service.Name = "jsonrpc"
// From retrieves the JSONRPC service in the given container or panic
func From(container *service.Container) (*rpc.Server, error) {
service, err := container.Service(ServiceName)
if err != nil {
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
}
jsonRPCService, ok := service.(*rpc.Server)
if !ok {
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
}
return jsonRPCService, nil
}
// Must retrieves the json-rpc service in the given container or panic otherwise
func Must(container *service.Container) *rpc.Server {
service, err := From(container)
if err != nil {
panic(err)
}
return service
}