Skip to content

Commit

Permalink
Introduced virtual methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seroukhov committed Apr 4, 2021
1 parent 33ab8d8 commit 9892e79
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 101 deletions.
18 changes: 7 additions & 11 deletions build/DefaultRpcFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,19 @@ import (
// See StatusRestService
type DefaultRpcFactory struct {
cbuild.Factory
Descriptor *cref.Descriptor
HttpEndpointDescriptor *cref.Descriptor
StatusServiceDescriptor *cref.Descriptor
HeartbeatServiceDescriptor *cref.Descriptor
}

// NewDefaultRpcFactorymethod create a new instance of the factory.
func NewDefaultRpcFactory() *DefaultRpcFactory {
c := DefaultRpcFactory{}
c.Factory = *cbuild.NewFactory()
c.Descriptor = cref.NewDescriptor("pip-services", "factory", "rpc", "default", "1.0")
c.HttpEndpointDescriptor = cref.NewDescriptor("pip-services", "endpoint", "http", "*", "1.0")
c.StatusServiceDescriptor = cref.NewDescriptor("pip-services", "status-service", "http", "*", "1.0")
c.HeartbeatServiceDescriptor = cref.NewDescriptor("pip-services", "heartbeat-service", "http", "*", "1.0")

c.RegisterType(c.HttpEndpointDescriptor, services.NewHttpEndpoint)
c.RegisterType(c.HeartbeatServiceDescriptor, services.NewHeartbeatRestService)
c.RegisterType(c.StatusServiceDescriptor, services.NewStatusRestService)
httpEndpointDescriptor := cref.NewDescriptor("pip-services", "endpoint", "http", "*", "1.0")
statusServiceDescriptor := cref.NewDescriptor("pip-services", "status-service", "http", "*", "1.0")
heartbeatServiceDescriptor := cref.NewDescriptor("pip-services", "heartbeat-service", "http", "*", "1.0")

c.RegisterType(httpEndpointDescriptor, services.NewHttpEndpoint)
c.RegisterType(heartbeatServiceDescriptor, services.NewHeartbeatRestService)
c.RegisterType(statusServiceDescriptor, services.NewStatusRestService)
return &c
}
12 changes: 6 additions & 6 deletions clients/RestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type RestClient struct {
//The remote service uri which is calculated on open.
Uri string
// add correlation id to headers
correlationIdPlace string
passCorrelationId string
}

// NewRestClient creates new instance of RestClient
Expand All @@ -116,7 +116,7 @@ func NewRestClient() *RestClient {
"options.timeout", 10000,
"options.retries", 3,
"options.debug", true,
"options.correlation_id_place", "query",
"options.correlation_id", "query",
)
rc.ConnectionResolver = *rpccon.NewHttpConnectionResolver()
rc.Logger = *clog.NewCompositeLogger()
Expand All @@ -125,7 +125,7 @@ func NewRestClient() *RestClient {
rc.Retries = 1
rc.Headers = *cdata.NewEmptyStringValueMap()
rc.ConnectTimeout = 10000
rc.correlationIdPlace = "query"
rc.passCorrelationId = "query"
return &rc
}

Expand All @@ -140,7 +140,7 @@ func (c *RestClient) Configure(config *cconf.ConfigParams) {
c.ConnectTimeout = config.GetAsIntegerWithDefault("options.connectTimeout", c.ConnectTimeout)
c.Timeout = config.GetAsIntegerWithDefault("options.timeout", c.Timeout)
c.BaseRoute = config.GetAsStringWithDefault("base_route", c.BaseRoute)
c.correlationIdPlace = config.GetAsStringWithDefault("options.correlation_id_place", c.correlationIdPlace)
c.passCorrelationId = config.GetAsStringWithDefault("options.correlation_id", c.passCorrelationId)
}

// Sets references to dependent components.
Expand Down Expand Up @@ -323,7 +323,7 @@ func (c *RestClient) Call(prototype reflect.Type, method string, route string, c
params = cdata.NewEmptyStringValueMap()
}
route = c.createRequestRoute(route)
if c.correlationIdPlace == "query" || c.correlationIdPlace == "both" {
if c.passCorrelationId == "query" || c.passCorrelationId == "both" {
params = c.AddCorrelationId(params, correlationId)
}
if params.Len() > 0 {
Expand Down Expand Up @@ -355,7 +355,7 @@ func (c *RestClient) Call(prototype reflect.Type, method string, route string, c
}
// Set headers
req.Header.Set("Content-Type", "application/json")
if c.correlationIdPlace == "headers" || c.correlationIdPlace == "both" {
if c.passCorrelationId == "headers" || c.passCorrelationId == "both" {
req.Header.Set("correlation_id", correlationId)
}
//req.Header.Set("User-Agent", c.UserAgent)
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13
FROM golang:1.16

# Set environment variables for Go
ENV GO111MODULE=on \
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.docgen
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13
FROM golang:1.16

# Set environment variables for Go
ENV GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13
FROM golang:1.16

# Set environment variables for Go
ENV GO111MODULE=on \
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/pip-services3-go/pip-services3-rpc-go

go 1.13
go 1.16

require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.7.4
github.com/pip-services3-go/pip-services3-commons-go v1.0.3
github.com/pip-services3-go/pip-services3-components-go v1.0.5
github.com/stretchr/testify v1.5.1
github.com/gorilla/mux v1.8.0
github.com/pip-services3-go/pip-services3-commons-go v1.1.0
github.com/pip-services3-go/pip-services3-components-go v1.1.0
github.com/stretchr/testify v1.7.0
)
33 changes: 0 additions & 33 deletions go.sum

This file was deleted.

33 changes: 22 additions & 11 deletions services/CommandableHttpService.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,39 @@ type CommandableHttpService struct {
// - baseRoute string a service base route.
// Returns: *CommandableHttpService
// pointer on new instance CommandableHttpService
func NewCommandableHttpService(baseRoute string) *CommandableHttpService {
chs := CommandableHttpService{}
chs.RestService = NewRestService()
chs.RestService.IRegisterable = &chs
chs.BaseRoute = baseRoute
chs.SwaggerAuto = true
chs.DependencyResolver.Put("controller", "none")
return &chs
// func NewCommandableHttpService(baseRoute string) *CommandableHttpService {
// c := &CommandableHttpService{}
// c.RestService = InheritRestService(c)
// c.BaseRoute = baseRoute
// c.SwaggerAuto = true
// c.DependencyResolver.Put("controller", "none")
// return c
// }

// InheritCommandableHttpService creates a new instance of the service.
// Parameters:
// - overrides references to child class that overrides virtual methods
// - baseRoute string a service base route.
// Returns: *CommandableHttpService
// pointer on new instance CommandableHttpService
func InheritCommandableHttpService(overrides IRestServiceOverrides, baseRoute string) *CommandableHttpService {
c := &CommandableHttpService{}
c.RestService = InheritRestService(overrides)
c.BaseRoute = baseRoute
c.SwaggerAuto = true
c.DependencyResolver.Put("controller", "none")
return c
}

// Configure method configures component by passing configuration parameters.
// - config configuration parameters to be set.
func (c *CommandableHttpService) Configure(config *cconf.ConfigParams) {
c.RestService.Configure(config)

c.SwaggerAuto = config.GetAsBooleanWithDefault("swagger.auto", c.SwaggerAuto)
}

// Register method are registers all service routes in HTTP endpoint.
func (c *CommandableHttpService) Register() {

resCtrl, depErr := c.DependencyResolver.GetOneRequired("controller")
if depErr != nil {
return
Expand Down Expand Up @@ -162,7 +174,6 @@ func (c *CommandableHttpService) Register() {

if c.SwaggerAuto {
var swaggerConfig = c.config.GetSection("swagger")

var doc = NewCommandableSwaggerDocument(c.BaseRoute, swaggerConfig, commands)
c.RegisterOpenApiSpec(doc.ToString())
}
Expand Down
7 changes: 3 additions & 4 deletions services/HeartbeatRestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ type HeartbeatRestService struct {
Creates a new instance of c service.
*/
func NewHeartbeatRestService() *HeartbeatRestService {
c := HeartbeatRestService{}
c.RestService = NewRestService()
c.RestService.IRegisterable = &c
c := &HeartbeatRestService{}
c.RestService = InheritRestService(c)
c.route = "heartbeat"
return &c
return c
}

/**
Expand Down
5 changes: 0 additions & 5 deletions services/HttpRequestDetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,9 @@ func (c *THttpRequestDetector) DetectPlatform(req *http.Request) string {
pattern = "/Windows NT/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
//try {
re := regexp.MustCompile(`/Windows NT ([0-9\._]+)[\);]/`)
version = re.FindAllStringSubmatch(ua, -1)[0][1]
return "windows " + version
// }
//catch (ex) {
return "unknown"
//}
}
return "unknown"
}
Expand Down
30 changes: 22 additions & 8 deletions services/RestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
clog "github.com/pip-services3-go/pip-services3-components-go/log"
)

type IRestServiceOverrides interface {
Register()
}

/*
RestService Abstract service that receives remove calls via HTTP/REST protocol.
Expand Down Expand Up @@ -113,7 +117,8 @@ Example:
*/
type RestService struct {
IRegisterable
Overrides IRestServiceOverrides

defaultConfig *cconf.ConfigParams
config *cconf.ConfigParams
references crefer.IReferences
Expand All @@ -131,13 +136,15 @@ type RestService struct {
Counters *ccount.CompositeCounters

SwaggerService ISwaggerService
SwaggerEnable bool
SwaggerEnabled bool
SwaggerRoute string
}

// NewRestService is create new instance of RestService
func NewRestService() *RestService {
rs := RestService{}
// InheritRestService is create new instance of RestService
func InheritRestService(overrides IRestServiceOverrides) *RestService {
rs := RestService{
Overrides: overrides,
}
rs.defaultConfig = cconf.NewConfigParamsFromTuples(
"base_route", "",
"dependencies.endpoint", "*:endpoint:http:*:1.0",
Expand All @@ -147,7 +154,7 @@ func NewRestService() *RestService {
rs.DependencyResolver.Configure(rs.defaultConfig)
rs.Logger = clog.NewCompositeLogger()
rs.Counters = ccount.NewCompositeCounters()
rs.SwaggerEnable = false
rs.SwaggerEnabled = false
rs.SwaggerRoute = "swagger"
return &rs
}
Expand All @@ -160,7 +167,8 @@ func (c *RestService) Configure(config *cconf.ConfigParams) {
c.config = config
c.DependencyResolver.Configure(config)
c.BaseRoute = config.GetAsStringWithDefault("base_route", c.BaseRoute)
c.SwaggerEnable = config.GetAsBooleanWithDefault("swagger.enable", c.SwaggerEnable)
c.SwaggerEnabled = config.GetAsBooleanWithDefault("swagger.enable", c.SwaggerEnabled)
c.SwaggerEnabled = config.GetAsBooleanWithDefault("swagger.enabled", c.SwaggerEnabled)
c.SwaggerRoute = config.GetAsStringWithDefault("swagger.route", c.SwaggerRoute)
}

Expand Down Expand Up @@ -522,7 +530,7 @@ func (c *RestService) RegisterOpenApiSpecFromFile(path string) {
}

func (c *RestService) RegisterOpenApiSpec(content string) {
if c.SwaggerEnable {
if c.SwaggerEnabled {
c.RegisterRoute("get", c.SwaggerRoute, nil, func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-Length", cconv.StringConverter.ToString(len(content)))
res.Header().Add("Content-Type", "application/x-yaml")
Expand All @@ -535,3 +543,9 @@ func (c *RestService) RegisterOpenApiSpec(content string) {
}
}
}

// Register method are registers all service routes in HTTP endpoint.
func (c *RestService) Register() {
// Override in child classes
c.Overrides.Register()
}
7 changes: 3 additions & 4 deletions services/StatusRestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ type StatusRestService struct {

// NewStatusRestService method are creates a new instance of this service.
func NewStatusRestService() *StatusRestService {
c := StatusRestService{}
c.RestService = NewRestService()
c.RestService.IRegisterable = &c
c := &StatusRestService{}
c.RestService = InheritRestService(c)
c.startTime = time.Now()
c.route = "status"
c.DependencyResolver.Put("context-info", crefer.NewDescriptor("pip-services", "context-info", "default", "*", "1.0"))
return &c
return c
}

// Configure method are configures component by passing configuration parameters.
Expand Down
12 changes: 5 additions & 7 deletions test/services/DummyCommandableHttpService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import (
)

type DummyCommandableHttpService struct {
*services.CommandableHttpService
services.CommandableHttpService
}

func NewDummyCommandableHttpService() *DummyCommandableHttpService {
c := DummyCommandableHttpService{
CommandableHttpService: services.NewCommandableHttpService("dummies"),
}
c := &DummyCommandableHttpService{}
c.CommandableHttpService = *services.InheritCommandableHttpService(c, "dummies")
c.DependencyResolver.Put("controller", cref.NewDescriptor("pip-services-dummies", "controller", "default", "*", "*"))
c.CommandableHttpService.IRegisterable = &c
return &c
return c
}

func (c *DummyCommandableHttpService) Register() {
if !c.SwaggerAuto && c.SwaggerEnable {
if !c.SwaggerAuto && c.SwaggerEnabled {
c.RegisterOpenApiSpec("swagger yaml content")
}
c.CommandableHttpService.Register()
Expand Down
7 changes: 3 additions & 4 deletions test/services/DummyRestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ type DummyRestService struct {
}

func NewDummyRestService() *DummyRestService {
c := DummyRestService{}
c.RestService = services.NewRestService()
c.RestService.IRegisterable = &c
c := &DummyRestService{}
c.RestService = services.InheritRestService(c)
c.numberOfCalls = 0
c.DependencyResolver.Put("controller", crefer.NewDescriptor("pip-services-dummies", "controller", "default", "*", "*"))
return &c
return c
}

func (c *DummyRestService) Configure(config *cconf.ConfigParams) {
Expand Down

0 comments on commit 9892e79

Please sign in to comment.