diff --git a/go.mod b/go.mod index 2e64369ac868925a8d7811cd8ea289d6f946cfe2..83356a2074fd300d8262cec2bed8fbc9e27c55bb 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/caarlos0/env/v9 v9.0.0 + github.com/go-playground/validator/v10 v10.15.5 github.com/gofiber/fiber/v2 v2.49.2 github.com/stretchr/testify v1.8.4 ) @@ -11,8 +12,12 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect github.com/google/uuid v1.3.1 // indirect github.com/klauspost/compress v1.16.7 // indirect + github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect @@ -22,6 +27,9 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.49.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/crypto v0.7.0 // indirect + golang.org/x/net v0.8.0 // indirect golang.org/x/sys v0.12.0 // indirect + golang.org/x/text v0.8.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index dcc028b538efd3d9b44c05726bc703f073b249c1..91ab0deb44983346aa4005d9112d66cd0c41c750 100644 --- a/go.sum +++ b/go.sum @@ -5,12 +5,17 @@ github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0X github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs= github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -24,10 +29,10 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -36,10 +41,13 @@ github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJ github.com/valyala/fasthttp v1.49.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index 604f8d6b472d69fa1d3a65cf906f4c4391a8cf39..7eb62c5ab4956c6adddb9531fa802ee46581141b 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,6 @@ import ( ) -var isHealthy = false - - func main() { config, err := configuration.New() if err != nil { @@ -30,7 +27,9 @@ func main() { DisableStartupMessage: config.Environment != "development", }) - routing.SetRoutes( server ) + var isHealthy = false + + routing.SetRoutes( server, config, &isHealthy ) go func(){ err := server.Listen( fmt.Sprintf( "%s:%d", config.Host, config.Port ) ) diff --git a/routing/routes.go b/routing/routes.go index 8c43e4fe1408207a1b662cca6de4c8b8c483aba8..19f38d3a19d411860dc6c4941054a13490208d85 100644 --- a/routing/routes.go +++ b/routing/routes.go @@ -1,6 +1,7 @@ package routing import ( + "encoding/json" "os" "fmt" "net/http" @@ -11,12 +12,37 @@ import ( ) -func SetRoutes( router *f.App, config *configuration.Config ){ +func SetRoutes( router *f.App, config *configuration.Config, healthiness *bool ){ router.Get( "/", func( c *f.Ctx ) error { return c.SendString( "Hello, World!" ) }) + + router.Get( "/health", func( c *f.Ctx ) error { + type response struct { + Status string `json:"status" validate:"oneof=passed failed"` + } + + c.Type( "json", "utf-8" ) + + var res response + if *healthiness == false { + res = response{ + Status: "failed", + } + c.Status( http.StatusServiceUnavailable ) + } else { + res = response{ + Status: "passed", + } + c.Status( http.StatusOK ) + } + + return c.JSON( res ) + }) + + router.Get( "/env", func( c *f.Ctx ) error { c.Type( "txt", "utf-8" ) diff --git a/routing/routes_test.go b/routing/routes_test.go index 385f49f3f97e8c218e5c6cd2dd50253f41af5d93..09bb4d10b211ec804b718779c2113cd78795ded7 100644 --- a/routing/routes_test.go +++ b/routing/routes_test.go @@ -6,6 +6,7 @@ import ( "os" "time" "math/rand" + "encoding/json" "testing" "net/http" ht "net/http/httptest" @@ -17,7 +18,7 @@ import ( ) -func setup() ( *f.App, *configuration.Config ){ +func setup() ( *f.App, *configuration.Config, *bool ){ os.Setenv( "ENV_NAME", "testing" ) config, _ := configuration.New() @@ -26,9 +27,10 @@ func setup() ( *f.App, *configuration.Config ){ DisableStartupMessage: false, }) - SetRoutes( server, config ) + var isHealthy = true + SetRoutes( server, config, &isHealthy ) - return server, config + return server, config, &isHealthy } @@ -43,6 +45,21 @@ func bodyToString( body *io.ReadCloser ) ( string, error ) { } +func jsonToMap( body *io.ReadCloser ) ( map[string]interface{}, error ) { + defer ( *body ).Close() + + var data map[string]interface{} + bodyBytes, err := io.ReadAll( *body ) + if err != nil { + return data, err + } + if err := json.Unmarshal( bodyBytes, &data ); err != nil { + return data, err + } + return data, nil +} + + func generateRandomNumberString() string { r := rand.New( rand.NewSource( time.Now().Unix() ) ) randomNumber := r.Int63() @@ -51,7 +68,7 @@ func generateRandomNumberString() string { func TestIndexRoute( t *testing.T ){ - router, _ := setup() + router, _, _ := setup() req := ht.NewRequest( "GET", "/", nil ) res, err := router.Test( req, -1 ) @@ -62,8 +79,31 @@ func TestIndexRoute( t *testing.T ){ } +func TestHealthRoute( t *testing.T ){ + router, _, healthiness := setup() + + req := ht.NewRequest( "GET", "/health", nil ) + res, err := router.Test( req, -1 ) + bodyContent, err := jsonToMap( &res.Body ) + status := bodyContent[ "status" ].( string ) + assert.Equal( t, http.StatusOK, res.StatusCode ) + assert.Nil( t, err ) + assert.Equal( t, "passed", status ) + + *healthiness = false + + req = ht.NewRequest( "GET", "/health", nil ) + res, err = router.Test( req, -1 ) + bodyContent, err = jsonToMap( &res.Body ) + status = bodyContent[ "status" ].( string ) + assert.Equal( t, http.StatusServiceUnavailable, res.StatusCode ) + assert.Nil( t, err ) + assert.Equal( t, "failed", status ) +} + + func TestEnvRoute( t *testing.T ){ - router, config := setup() + router, config, _ := setup() envVarName := "TEST_ENV_VAR" envVarValue := generateRandomNumberString()