Skip to content

Commit

Permalink
Added integration with swagger ui
Browse files Browse the repository at this point in the history
  • Loading branch information
seroukhov committed Feb 23, 2021
1 parent c61c9e6 commit 2c12553
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# <img src="https://uploads-ssl.webflow.com/5ea5d3315186cf5ec60c3ee4/5edf1c94ce4c859f2b188094_logo.svg" alt="Pip.Services Logo" width="200"> <br/> Remote Procedure Calls Golang Changelog

## <a name="3.1.0"></a> 3.1.0 (2021-02-21)

### Features
* **services** Added integration with Swagger UI

## <a name="1.0.13"></a> 1.0.13 (2020-12-10)

Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pip-services3-rpc-go",
"registry": "github.com/pip-services3-go",
"version": "3.0.0",
"version": "3.1.0",
"build": 0
}
8 changes: 8 additions & 0 deletions services/HttpResponseSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type THttpResponseSender struct {
func (c *THttpResponseSender) SendError(res http.ResponseWriter, req *http.Request, err error) {

appErr := cerr.ApplicationError{}
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(500)
jsonObj, jsonErr := json.Marshal(appErr.Wrap(err))
if jsonErr == nil {
Expand All @@ -49,8 +50,10 @@ func (c *THttpResponseSender) SendResult(res http.ResponseWriter, req *http.Requ
return
}
if result == nil {
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(204)
} else {
res.Header().Add("Content-Type", "application/json")
jsonObj, jsonErr := json.Marshal(result)
if jsonErr == nil {
io.WriteString(res, (string)(jsonObj))
Expand All @@ -67,6 +70,7 @@ func (c *THttpResponseSender) SendEmptyResult(res http.ResponseWriter, req *http
HttpResponseSender.SendError(res, req, err)
return
}
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(204)
}

Expand All @@ -85,8 +89,10 @@ func (c *THttpResponseSender) SendCreatedResult(res http.ResponseWriter, req *ht
return
}
if result == nil {
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(204)
} else {
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(201)
jsonObj, jsonErr := json.Marshal(result)
if jsonErr == nil {
Expand All @@ -110,8 +116,10 @@ func (c *THttpResponseSender) SendDeletedResult(res http.ResponseWriter, req *ht
return
}
if result == nil {
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(204)
} else {
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(200)
jsonObj, jsonErr := json.Marshal(result)
if jsonErr == nil {
Expand Down
2 changes: 1 addition & 1 deletion services/ISwaggerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package services
type ISwaggerService interface {

// Perform required Swagger registration steps.
RegisterOpenApiSpec(baseRoute string, content string)
RegisterOpenApiSpec(baseRoute string, swaggerRoute string)
}
16 changes: 13 additions & 3 deletions services/RestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type RestService struct {
//The performance counters.
Counters *ccount.CompositeCounters

SwaggerService ISwaggerService
SwaggerEnable bool
SwaggerRoute string
}
Expand All @@ -140,6 +141,7 @@ func NewRestService() *RestService {
rs.defaultConfig = cconf.NewConfigParamsFromTuples(
"base_route", "",
"dependencies.endpoint", "*:endpoint:http:*:1.0",
"dependencies.swagger", "*:swagger-service:*:*:1.0"
)
rs.DependencyResolver = crefer.NewDependencyResolver()
rs.DependencyResolver.Configure(rs.defaultConfig)
Expand Down Expand Up @@ -187,6 +189,11 @@ func (c *RestService) SetReferences(references crefer.IReferences) {
}
// Add registration callback to the endpoint
c.Endpoint.Register(c)

depRes = c.DependencyResolver.GetOneOptional("swagger")
if depRes != nil {
c.SwaggerService = depRes.(ISwaggerService)
}
}

// UnsetReferences method are unsets (clears) previously set references to dependent components.
Expand All @@ -196,6 +203,7 @@ func (c *RestService) UnsetReferences() {
c.Endpoint.Unregister(c)
c.Endpoint = nil
}
c.SwaggerService = nil
}

func (c *RestService) createEndpoint() *HttpEndpoint {
Expand Down Expand Up @@ -505,7 +513,6 @@ func (c *RestService) GetCorrelationId(req *http.Request) string {
}

func (c *RestService) RegisterOpenApiSpecFromFile(path string) {

content, err := ioutil.ReadFile(path)
if err != nil {
c.Logger.Error("RestService", err, "Can't read swagger file by path %s", path)
Expand All @@ -517,11 +524,14 @@ func (c *RestService) RegisterOpenApiSpecFromFile(path string) {
func (c *RestService) RegisterOpenApiSpec(content string) {
if c.SwaggerEnable {
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")
res.WriteHeader(200)
io.WriteString(res, content)

})

if c.SwaggerService != nil {
c.SwaggerService.RegisterOpenApiSpec(c.BaseRoute, c.SwaggerRoute)
}
}
}

0 comments on commit 2c12553

Please sign in to comment.