Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add health port #24

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading