Skip to content

Commit

Permalink
health: Adds version endpoint (ory#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored and arekkas committed May 16, 2018
1 parent da6bb30 commit 14739b4
Show file tree
Hide file tree
Showing 33 changed files with 956 additions and 183 deletions.
1 change: 1 addition & 0 deletions cmd/server/handler_health_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newHealthHandler(c *config.Config, router *httprouter.Router) *health.Handl
Metrics: c.GetTelemetryMetrics(),
H: herodot.NewJSONWriter(c.GetLogger()),
ResourcePrefix: c.AccessControlResourcePrefix,
VersionString: c.BuildVersion,
}
h.SetRoutes(router)
return h
Expand Down
62 changes: 48 additions & 14 deletions docs/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,31 @@
"operationId": "getInstanceStatus",
"responses": {
"200": {
"$ref": "#/responses/healthStatus"
"description": "healthStatus",
"schema": {
"$ref": "#/definitions/healthStatus"
}
},
"500": {
"$ref": "#/responses/genericError"
}
}
}
},
"/health/version": {
"get": {
"description": "This endpoint returns the version as `{ \"version\": \"VERSION\" }`. The version is only correct with the prebuilt binary and not custom builds.",
"tags": [
"health"
],
"summary": "Get the version of Hydra",
"operationId": "getVersion",
"responses": {
"200": {
"description": "healthVersion",
"schema": {
"$ref": "#/definitions/healthVersion"
}
},
"500": {
"$ref": "#/responses/genericError"
Expand Down Expand Up @@ -1457,6 +1481,29 @@
"x-go-name": "FlushInactiveOAuth2TokensRequest",
"x-go-package": "github.com/ory/hydra/oauth2"
},
"healthStatus": {
"type": "object",
"properties": {
"status": {
"description": "Status always contains \"ok\"",
"type": "string",
"x-go-name": "Status"
}
},
"x-go-name": "HealthStatus",
"x-go-package": "github.com/ory/hydra/health"
},
"healthVersion": {
"type": "object",
"properties": {
"version": {
"type": "string",
"x-go-name": "Version"
}
},
"x-go-name": "HealthVersion",
"x-go-package": "github.com/ory/hydra/health"
},
"joseWebKeySetRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2270,19 +2317,6 @@
}
}
},
"healthStatus": {
"description": "Instance health report",
"schema": {
"type": "object",
"properties": {
"status": {
"description": "Status always contains \"ok\"",
"type": "string",
"x-go-name": "Status"
}
}
}
},
"oAuth2ClientList": {
"description": "A list of clients.",
"schema": {
Expand Down
17 changes: 9 additions & 8 deletions health/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

package health

// Instance health report
// swagger:response healthStatus
type swaggerListClientsResult struct {
// in: body
Body struct {
// Status always contains "ok"
Status string `json:"status"`
}
// swagger:model healthStatus
type HealthStatus struct {
// Status always contains "ok"
Status string `json:"status"`
}

// swagger:model healthVersion
type HealthVersion struct {
Version string `json:"version"`
}
24 changes: 23 additions & 1 deletion health/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import (

const (
HealthStatusPath = "/health/status"
HealthVersionPath = "/health/version"
PrometheusStatusPath = "/health/prometheus"
)

type Handler struct {
Metrics *telemetry.MetricsManager
H *herodot.JSONWriter
ResourcePrefix string
VersionString string
}

func (h *Handler) PrefixResource(resource string) string {
Expand All @@ -54,6 +56,9 @@ func (h *Handler) PrefixResource(resource string) string {

func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET(HealthStatusPath, h.Health)
r.GET(HealthVersionPath, h.Version)

// using r.Handler because promhttp.Handler() returns http.Handler
r.Handler("GET", PrometheusStatusPath, promhttp.Handler())
}

Expand All @@ -69,5 +74,22 @@ func (h *Handler) SetRoutes(r *httprouter.Router) {
// 200: healthStatus
// 500: genericError
func (h *Handler) Health(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
rw.Write([]byte(`{"status": "ok"}`))
h.H.Write(rw, r, &HealthStatus{
Status: "ok",
})
}

// swagger:route GET /health/version health getVersion
//
// Get the version of Hydra
//
// This endpoint returns the version as `{ "version": "VERSION" }`. The version is only correct with the prebuilt binary and not custom builds.
//
// Responses:
// 200: healthVersion
// 500: genericError
func (h *Handler) Version(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
h.H.Write(rw, r, &HealthVersion{
Version: h.VersionString,
})
}
55 changes: 55 additions & 0 deletions health/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright © 2015-2018 Aeneas Rekkas <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <[email protected]>
* @Copyright 2017-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*/

package health

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/sdk/go/hydra/swagger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHealth(t *testing.T) {
handler := &Handler{
H: herodot.NewJSONWriter(nil),
VersionString: "test version",
}
router := httprouter.New()
handler.SetRoutes(router)
ts := httptest.NewServer(router)

client := swagger.NewHealthApiWithBasePath(ts.URL)

body, response, err := client.GetInstanceStatus()
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, response.StatusCode)
assert.EqualValues(t, "ok", body.Status)

version, response, err := client.GetVersion()
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, response.StatusCode)
require.EqualValues(t, version.Version, handler.VersionString)
}
4 changes: 3 additions & 1 deletion sdk/go/hydra/swagger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All URIs are relative to *http://localhost*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*HealthApi* | [**GetInstanceStatus**](docs/HealthApi.md#getinstancestatus) | **Get** /health/status | Check the Health Status
*HealthApi* | [**GetVersion**](docs/HealthApi.md#getversion) | **Get** /health/version | Get the version of Hydra
*JsonWebKeyApi* | [**CreateJsonWebKeySet**](docs/JsonWebKeyApi.md#createjsonwebkeyset) | **Post** /keys/{set} | Generate a new JSON Web Key
*JsonWebKeyApi* | [**DeleteJsonWebKey**](docs/JsonWebKeyApi.md#deletejsonwebkey) | **Delete** /keys/{set}/{kid} | Delete a JSON Web Key
*JsonWebKeyApi* | [**DeleteJsonWebKeySet**](docs/JsonWebKeyApi.md#deletejsonwebkeyset) | **Delete** /keys/{set} | Delete a JSON Web Key Set
Expand Down Expand Up @@ -61,7 +62,8 @@ Class | Method | HTTP request | Description
- [ConsentRequestSession](docs/ConsentRequestSession.md)
- [FlushInactiveOAuth2TokensRequest](docs/FlushInactiveOAuth2TokensRequest.md)
- [Handler](docs/Handler.md)
- [InlineResponse200](docs/InlineResponse200.md)
- [HealthStatus](docs/HealthStatus.md)
- [HealthVersion](docs/HealthVersion.md)
- [InlineResponse401](docs/InlineResponse401.md)
- [JoseWebKeySetRequest](docs/JoseWebKeySetRequest.md)
- [JsonWebKey](docs/JsonWebKey.md)
Expand Down
31 changes: 29 additions & 2 deletions sdk/go/hydra/swagger/docs/HealthApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
[**GetInstanceStatus**](HealthApi.md#GetInstanceStatus) | **Get** /health/status | Check the Health Status
[**GetVersion**](HealthApi.md#GetVersion) | **Get** /health/version | Get the version of Hydra


# **GetInstanceStatus**
> InlineResponse200 GetInstanceStatus()
> HealthStatus GetInstanceStatus()
Check the Health Status

Expand All @@ -20,7 +21,33 @@ This endpoint does not need any parameter.

### Return type

[**InlineResponse200**](inline_response_200.md)
[**HealthStatus**](healthStatus.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: application/json, application/x-www-form-urlencoded
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# **GetVersion**
> HealthVersion GetVersion()
Get the version of Hydra

This endpoint returns the version as `{ \"version\": \"VERSION\" }`. The version is only correct with the prebuilt binary and not custom builds.


### Parameters
This endpoint does not need any parameter.

### Return type

[**HealthVersion**](healthVersion.md)

### Authorization

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# InlineResponse200
# HealthStatus

## Properties
Name | Type | Description | Notes
Expand Down
10 changes: 10 additions & 0 deletions sdk/go/hydra/swagger/docs/HealthVersion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# HealthVersion

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Version** | **string** | | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


65 changes: 62 additions & 3 deletions sdk/go/hydra/swagger/health_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func NewHealthApiWithBasePath(basePath string) *HealthApi {
* Check the Health Status
* This endpoint returns a 200 status code when the HTTP server is up running. &#x60;{ \&quot;status\&quot;: \&quot;ok\&quot; }&#x60;. This status does currently not include checks whether the database connection is working. This endpoint does not require the &#x60;X-Forwarded-Proto&#x60; header when TLS termination is set. Be aware that if you are running multiple nodes of ORY Hydra, the health status will never refer to the cluster state, only to a single instance.
*
* @return *InlineResponse200
* @return *HealthStatus
*/
func (a HealthApi) GetInstanceStatus() (*InlineResponse200, *APIResponse, error) {
func (a HealthApi) GetInstanceStatus() (*HealthStatus, *APIResponse, error) {

var localVarHttpMethod = strings.ToUpper("Get")
// create path and map variables
Expand Down Expand Up @@ -77,7 +77,7 @@ func (a HealthApi) GetInstanceStatus() (*InlineResponse200, *APIResponse, error)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
var successPayload = new(InlineResponse200)
var successPayload = new(HealthStatus)
localVarHttpResponse, err := a.Configuration.APIClient.CallAPI(localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)

var localVarURL, _ = url.Parse(localVarPath)
Expand All @@ -94,3 +94,62 @@ func (a HealthApi) GetInstanceStatus() (*InlineResponse200, *APIResponse, error)
err = json.Unmarshal(localVarHttpResponse.Body(), &successPayload)
return successPayload, localVarAPIResponse, err
}

/**
* Get the version of Hydra
* This endpoint returns the version as &#x60;{ \&quot;version\&quot;: \&quot;VERSION\&quot; }&#x60;. The version is only correct with the prebuilt binary and not custom builds.
*
* @return *HealthVersion
*/
func (a HealthApi) GetVersion() (*HealthVersion, *APIResponse, error) {

var localVarHttpMethod = strings.ToUpper("Get")
// create path and map variables
localVarPath := a.Configuration.BasePath + "/health/version"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := make(map[string]string)
var localVarPostBody interface{}
var localVarFileName string
var localVarFileBytes []byte
// add default headers if any
for key := range a.Configuration.DefaultHeader {
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
}

// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-www-form-urlencoded"}

// set Content-Type header
localVarHttpContentType := a.Configuration.APIClient.SelectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}
// to determine the Accept header
localVarHttpHeaderAccepts := []string{
"application/json",
}

// set Accept header
localVarHttpHeaderAccept := a.Configuration.APIClient.SelectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
var successPayload = new(HealthVersion)
localVarHttpResponse, err := a.Configuration.APIClient.CallAPI(localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)

var localVarURL, _ = url.Parse(localVarPath)
localVarURL.RawQuery = localVarQueryParams.Encode()
var localVarAPIResponse = &APIResponse{Operation: "GetVersion", Method: localVarHttpMethod, RequestURL: localVarURL.String()}
if localVarHttpResponse != nil {
localVarAPIResponse.Response = localVarHttpResponse.RawResponse
localVarAPIResponse.Payload = localVarHttpResponse.Body()
}

if err != nil {
return successPayload, localVarAPIResponse, err
}
err = json.Unmarshal(localVarHttpResponse.Body(), &successPayload)
return successPayload, localVarAPIResponse, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package swagger

type InlineResponse200 struct {
type HealthStatus struct {

// Status always contains \"ok\"
Status string `json:"status,omitempty"`
Expand Down
Loading

0 comments on commit 14739b4

Please sign in to comment.