Skip to content

Commit

Permalink
feat: add health port
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Jan 8, 2024
1 parent b12fc2b commit 9ffdb13
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")

Expand All @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions health/health.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down

0 comments on commit 9ffdb13

Please sign in to comment.