Skip to content

Commit

Permalink
fix/api should allways honor HTTP_PORT env-variable (#27)
Browse files Browse the repository at this point in the history
* remove development flag in router setup, allways use HTTP_PORT variable

* get host from env

* migrate config to host/port for both http and health

* bum internal library

* update template

* remove old config from template
  • Loading branch information
havardelnan authored Feb 12, 2025
1 parent ee28c80 commit e722b25
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func main() {
}()
sseserver.StartEventServer()
rlog.Infoc(ctx, "Initializing health server")
_ = healthserver.Start(healthserver.ServerString(viper.GetString(configconsts.HEALTH_ENDPOINT)))
_ = healthserver.Start(healthserver.ServerString(apiconfig.GetHealthEndpoint()))

if apiconnections.RabbitMQConnection.Ping() {
switchboard.PublishStarted(ctx)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/NorskHelsenett/ror-api
go 1.23.1

require (
github.com/NorskHelsenett/ror v1.1.0
github.com/NorskHelsenett/ror v1.1.1
github.com/blang/semver/v4 v4.0.0
github.com/coreos/go-oidc/v3 v3.12.0
github.com/dotse/go-health v0.2.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.3.3 h1:H5xDQaE3Xow
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.3/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/NorskHelsenett/ror v1.1.0 h1:gM3iW/yktkRUX1/zZgO2OXJkvVLS4jaYDhg0s7vRKcs=
github.com/NorskHelsenett/ror v1.1.0/go.mod h1:sCh2/8XYn75b/+yrrWwOXmyxw1i5wo9i4CDOZ5/2TGM=
github.com/NorskHelsenett/ror v1.1.1 h1:d4Mb8rXs7uijUXxUVwdEB5MITuM4RKH/sj+ci7znRoA=
github.com/NorskHelsenett/ror v1.1.1/go.mod h1:sCh2/8XYn75b/+yrrWwOXmyxw1i5wo9i4CDOZ5/2TGM=
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI=
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
4 changes: 3 additions & 1 deletion hacks/vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"LOG_LEVEL": "debug",
"GIN_MODE": "debug",
"ALLOW_ORIGINS": "https://ror.sky.test.nhn.no;http://localhost:11000;http://127.0.0.1:11000",
"HTTP_HOST": "localhost",
"HTTP_PORT": "10000",
"HTTP_HEALTH_HOST": "localhost",
"HTTP_HEALTH_PORT": "9999",
"VAULT_URL": "http://localhost:8200",
"MONGODB_PORT": "27017",
"OPENTELEMETRY_COLLECTOR_ENDPOINT": "localhost:4317",
Expand All @@ -27,7 +30,6 @@
"CONTAINER_REG_IMAGE_PATH": "nhnsdi/",
"CONTAINER_REG_HELM_PATH": "nhnhelm/",
"LOCAL_KUBERNETES_ROR_BASE_URL": "http://host.docker.internal:10000",
"HEALTHCHECK_ENDPOINT": "localhost:9999"
}
},
{
Expand Down
16 changes: 15 additions & 1 deletion internal/apiconfig/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ func InitViper() {
// Remove we dont set env in variables.
viper.SetDefault(configconsts.DEVELOPMENT, false)
//Remove used in gin.go
viper.SetDefault(configconsts.HTTP_HOST, "0.0.0.0")
viper.SetDefault(configconsts.HTTP_PORT, "8080")
viper.SetDefault(configconsts.HTTP_TIMEOUT, "15s")

viper.SetDefault(configconsts.HTTP_HEALTH_PORT, "9999")

viper.SetDefault(configconsts.PROFILER_ENABLED, false)
viper.SetDefault(configconsts.ENABLE_TRACING, true)
viper.SetDefault(configconsts.TRACER_ID, "ror-api")
Expand Down Expand Up @@ -65,9 +68,20 @@ func InitViper() {
viper.SetDefault(configconsts.OPENTELEMETRY_COLLECTOR_ENDPOINT, "opentelemetry-collector:4317")
viper.SetDefault(configconsts.HELSEGITLAB_BASE_URL, "https://helsegitlab.nhn.no/api/v4/projects/")

viper.SetDefault(configconsts.HEALTH_ENDPOINT, "0.0.0.0:9999")
}

func GetRorVersion() rorversion.RorVersion {
return RorVersion
}

func GetHTTPEndpoint() string {
return fmt.Sprintf("%s:%s", viper.GetString(configconsts.HTTP_HOST), viper.GetString(configconsts.HTTP_PORT))
}

func GetHealthEndpoint() string {
if viper.IsSet(configconsts.HEALTH_ENDPOINT) {
rlog.Info("Using deprecated HEALTH_ENDPOINT configuration. Please use HTTP_HEALTH_HOST and HTTP_HEALTH_PORT instead")
return viper.GetString(configconsts.HEALTH_ENDPOINT)
}
return fmt.Sprintf("%s:%s", viper.GetString(configconsts.HTTP_HEALTH_HOST), viper.GetString(configconsts.HTTP_HEALTH_PORT))
}
7 changes: 1 addition & 6 deletions internal/webserver/gin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package webserver

import (
"fmt"
"strings"

"github.com/NorskHelsenett/ror-api/internal/apiconfig"
Expand Down Expand Up @@ -54,11 +53,7 @@ func InitHttpServer() {
}
_ = router.SetTrustedProxies([]string{"localhost"})
routes.SetupRoutes(router)
if !viper.GetBool(configconsts.DEVELOPMENT) {
rlog.Fatal("router failing", router.Run())
} else {
rlog.Fatal("router failing", router.Run(fmt.Sprintf("localhost:%s", viper.GetString(configconsts.HTTP_PORT))))
}
rlog.Fatal("router failing", router.Run(apiconfig.GetHTTPEndpoint()))
}

func headersMiddleware() gin.HandlerFunc {
Expand Down

0 comments on commit e722b25

Please sign in to comment.