diff --git a/config/config.go b/config/config.go index 6d0c3eb..d67ae9e 100644 --- a/config/config.go +++ b/config/config.go @@ -14,8 +14,9 @@ type Config struct { } type Observability struct { - LogLevel string `default:"debug" split_words:"true"` - LogFile string `default:"out.log" split_words:"true"` + LogLevel string `default:"debug" split_words:"true"` + LogFile string `default:"out.log" split_words:"true"` + HealthPort uint16 `default:"9001" split_words:"true"` } type Prover struct { diff --git a/config/config_test.go b/config/config_test.go index f1ec775..3708a20 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -41,8 +41,9 @@ func (s *ConfigTestSuite) Test_LoadConfig_DefaultValues() { s.Nil(err) s.Equal(c, &config.Config{ Observability: &config.Observability{ - LogLevel: "debug", - LogFile: "out.log", + LogLevel: "debug", + LogFile: "out.log", + HealthPort: 9001, }, Prover: &config.Prover{ URL: "http://prover.com", @@ -54,6 +55,7 @@ func (s *ConfigTestSuite) Test_LoadConfig_DefaultValues() { func (s *ConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() { os.Setenv("SPECTRE_OBSERVABILITY_LOG_LEVEL", "info") os.Setenv("SPECTRE_OBSERVABILITY_LOG_FILE", "out2.log") + os.Setenv("SPECTRE_OBSERVABILITY_HEALTH_PORT", "9003") os.Setenv("SPECTRE_PROVER_URL", "http://prover.com") os.Setenv("SPECTRE_DOMAINS", "1:evm,2:evm") @@ -65,8 +67,9 @@ func (s *ConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() { s.Nil(err) s.Equal(c, &config.Config{ Observability: &config.Observability{ - LogLevel: "info", - LogFile: "out2.log", + LogLevel: "info", + LogFile: "out2.log", + HealthPort: 9003, }, Prover: &config.Prover{ URL: "http://prover.com", diff --git a/health/health.go b/health/health.go new file mode 100644 index 0000000..e72f696 --- /dev/null +++ b/health/health.go @@ -0,0 +1,21 @@ +// The Licensed Work is (c) 2023 Sygma +// SPDX-License-Identifier: LGPL-3.0-only + +package health + +import ( + "fmt" + "net/http" + + "github.com/rs/zerolog/log" +) + +// StartHealthEndpoint starts /health endpoint on provided port that returns ok on invocation +func StartHealthEndpoint(port uint16) { + http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("ok")) + }) + + _ = http.ListenAndServe(fmt.Sprintf(":%d", port), nil) + log.Info().Msgf("started /health endpoint on port %d", port) +} diff --git a/main.go b/main.go index f0e9f8a..64ca2c7 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ import ( evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message" "github.com/sygmaprotocol/spectre-node/chains/evm/prover" "github.com/sygmaprotocol/spectre-node/config" + "github.com/sygmaprotocol/spectre-node/health" "github.com/sygmaprotocol/sygma-core/chains/evm" "github.com/sygmaprotocol/sygma-core/chains/evm/client" "github.com/sygmaprotocol/sygma-core/chains/evm/transactor/gas" @@ -51,6 +52,8 @@ func main() { log.Info().Msg("Loaded configuration") + go health.StartHealthEndpoint(cfg.Observability.HealthPort) + domains := make([]uint8, 0) for domain := range cfg.Domains { domains = append(domains, domain)