Skip to content

Commit

Permalink
Merge pull request #53 from yunkon-kim/240503-15
Browse files Browse the repository at this point in the history
Provide the API to check system readiness instead of `health`
  • Loading branch information
yunkon-kim authored May 3, 2024
2 parents 56fe741 + 487f40d commit 6ff922a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 60 deletions.
6 changes: 6 additions & 0 deletions cmd/poc-mc-net-tf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ import (
// _ "github.com/mattn/go-sqlite3"

restServer "github.com/cloud-barista/poc-mc-net-tf/pkg/api/rest/server"

"github.com/cloud-barista/poc-mc-net-tf/pkg/readyz"
)

func init() {
readyz.SetReady(false)
}

func main() {

log.Info().Msg("starting POC-MC-Net-TF server")
Expand Down
32 changes: 16 additions & 16 deletions pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/health": {
"/httpVersion": {
"get": {
"description": "Check API server is running",
"description": "Checks and logs the HTTP version of the incoming request to the server console.",
"consumes": [
"application/json"
],
Expand All @@ -35,26 +35,32 @@ const docTemplate = `{
"tags": [
"[System] Utility"
],
"summary": "Check API server is running",
"summary": "Check HTTP version of incoming request",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"503": {
"description": "Service Unavailable",
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
}
}
}
},
"/httpVersion": {
"/readyz": {
"get": {
"description": "Checks and logs the HTTP version of the incoming request to the server console.",
"description": "Check mc-net server is ready",
"consumes": [
"application/json"
],
Expand All @@ -64,22 +70,16 @@ const docTemplate = `{
"tags": [
"[System] Utility"
],
"summary": "Check HTTP version of incoming request",
"summary": "Check mc-net server is ready",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"500": {
"description": "Internal Server Error",
"503": {
"description": "Service Unavailable",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
},
"basePath": "/mc-net",
"paths": {
"/health": {
"/httpVersion": {
"get": {
"description": "Check API server is running",
"description": "Checks and logs the HTTP version of the incoming request to the server console.",
"consumes": [
"application/json"
],
Expand All @@ -28,26 +28,32 @@
"tags": [
"[System] Utility"
],
"summary": "Check API server is running",
"summary": "Check HTTP version of incoming request",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"503": {
"description": "Service Unavailable",
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
}
}
}
},
"/httpVersion": {
"/readyz": {
"get": {
"description": "Checks and logs the HTTP version of the incoming request to the server console.",
"description": "Check mc-net server is ready",
"consumes": [
"application/json"
],
Expand All @@ -57,22 +63,16 @@
"tags": [
"[System] Utility"
],
"summary": "Check HTTP version of incoming request",
"summary": "Check mc-net server is ready",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
},
"500": {
"description": "Internal Server Error",
"503": {
"description": "Service Unavailable",
"schema": {
"$ref": "#/definitions/models.ResponseText"
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,47 +197,47 @@ info:
title: POC-MC-Net-TF REST API
version: latest
paths:
/health:
/httpVersion:
get:
consumes:
- application/json
description: Check API server is running
description: Checks and logs the HTTP version of the incoming request to the
server console.
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.ResponseText'
"503":
description: Service Unavailable
"404":
description: Not Found
schema:
$ref: '#/definitions/models.ResponseText'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/models.ResponseText'
summary: Check API server is running
summary: Check HTTP version of incoming request
tags:
- '[System] Utility'
/httpVersion:
/readyz:
get:
consumes:
- application/json
description: Checks and logs the HTTP version of the incoming request to the
server console.
description: Check mc-net server is ready
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.ResponseText'
"404":
description: Not Found
schema:
$ref: '#/definitions/models.ResponseText'
"500":
description: Internal Server Error
"503":
description: Service Unavailable
schema:
$ref: '#/definitions/models.ResponseText'
summary: Check HTTP version of incoming request
summary: Check mc-net server is ready
tags:
- '[System] Utility'
/rg/{resourceGroupId}:
Expand Down
22 changes: 14 additions & 8 deletions pkg/api/rest/handlers/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,30 @@ import (
"net/http"

"github.com/cloud-barista/poc-mc-net-tf/pkg/api/rest/models"
"github.com/cloud-barista/poc-mc-net-tf/pkg/readyz"
"github.com/cloud-barista/poc-mc-net-tf/pkg/tofu"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
)

// Health godoc
// @Summary Check API server is running
// @Description Check API server is running
// Readyz func is for checking mc-net server is ready.
// Readyz godoc
// @Summary Check mc-net server is ready
// @Description Check mc-net server is ready
// @Tags [System] Utility
// @Accept json
// @Produce json
// @Success 200 {object} models.ResponseText
// @Failure 503 {object} models.ResponseText
// @Router /health [get]
func Health(c echo.Context) error {
res := models.ResponseText{Success: true, Text: "POC-MC-Net-TF API server is running"}

return c.JSON(http.StatusOK, res)
// @Router /readyz [get]
func Readyz(c echo.Context) error {
res := models.ResponseText{}
res.Text = "mc-net server is ready"
if !readyz.IsReady() {
res.Text = "mc-net server is NOT ready"
return c.JSON(http.StatusServiceUnavailable, &res)
}
return c.JSON(http.StatusOK, &res)
}

// HTTPVersion godoc
Expand Down
19 changes: 14 additions & 5 deletions pkg/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import (
// echo-swagger middleware
_ "github.com/cloud-barista/poc-mc-net-tf/pkg/api/rest/docs"
echoSwagger "github.com/swaggo/echo-swagger"

"github.com/cloud-barista/poc-mc-net-tf/pkg/readyz"
)

//var masterConfigInfos confighandler.MASTERCONFIGTYPE
Expand Down Expand Up @@ -145,7 +147,7 @@ func RunServer(port string) {
e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{
// Skip authentication for some routes that do not require authentication
Skipper: func(c echo.Context) bool {
if c.Path() == "/mc-net/health" ||
if c.Path() == "/mc-net/readyz" ||
c.Path() == "/mc-net/httpVersion" {
return true
}
Expand All @@ -170,10 +172,16 @@ func RunServer(port string) {
fmt.Println("\n \n ")

// Route for system management
e.GET("/mc-net/swagger/*", echoSwagger.WrapHandler)

swaggerRedirect := func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "/mc-net/api/index.html")
}
e.GET("/mc-net/api", swaggerRedirect)
e.GET("/mc-net/api/", swaggerRedirect)
e.GET("/mc-net/api/*", echoSwagger.WrapHandler)
// e.GET("/mc-net/swagger/*", echoSwagger.WrapHandler)
// e.GET("/mc-net/swaggerActive", rest_common.RestGetSwagger)
e.GET("/mc-net/health", handlers.Health)

e.GET("/mc-net/readyz", handlers.Readyz)
e.GET("/mc-net/httpVersion", handlers.HTTPVersion)
e.GET("/mc-net/tofuVersion", handlers.TofuVersion)

Expand All @@ -189,7 +197,7 @@ func RunServer(port string) {
route.RegisterSampleRoutes(groupSample)

selfEndpoint := viper.GetString("self.endpoint")
apidashboard := " http://" + selfEndpoint + "/mc-net/swagger/index.html"
apidashboard := " http://" + selfEndpoint + "/mc-net/api"

if enableAuth {
fmt.Println(" Access to API dashboard" + " (username: " + apiUser + " / password: " + apiPass + ")")
Expand Down Expand Up @@ -230,6 +238,7 @@ func RunServer(port string) {

log.Info().Msg("starting POC-MC-Net-TF REST API server")
port = fmt.Sprintf(":%s", port)
readyz.SetReady(true)
if err := e.Start(port); err != nil && err != http.ErrServerClosed {
e.Logger.Panic("shuttig down the server")
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/readyz/readyz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package readyz

import (
"sync/atomic"
)

// isSystemReady is declared as an atomic.Value to ensure thread safety.
var isSystemReady atomic.Value

// init function initializes isSystemReady with its default value.
func init() {
isSystemReady.Store(false) // Initialize as the system is not ready.
}

// IsReady returns the current readiness status of the system.
func IsReady() bool {
return isSystemReady.Load().(bool)
}

// SetReady sets the readiness status of the system.
func SetReady(ready bool) {
isSystemReady.Store(ready)
}

0 comments on commit 6ff922a

Please sign in to comment.