Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit efb6f65

Browse files
authored
feat: add health port (#24)
1 parent b12fc2b commit efb6f65

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

config/config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type Config struct {
1414
}
1515

1616
type Observability struct {
17-
LogLevel string `default:"debug" split_words:"true"`
18-
LogFile string `default:"out.log" split_words:"true"`
17+
LogLevel string `default:"debug" split_words:"true"`
18+
LogFile string `default:"out.log" split_words:"true"`
19+
HealthPort uint16 `default:"9001" split_words:"true"`
1920
}
2021

2122
type Prover struct {

config/config_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ func (s *ConfigTestSuite) Test_LoadConfig_DefaultValues() {
4141
s.Nil(err)
4242
s.Equal(c, &config.Config{
4343
Observability: &config.Observability{
44-
LogLevel: "debug",
45-
LogFile: "out.log",
44+
LogLevel: "debug",
45+
LogFile: "out.log",
46+
HealthPort: 9001,
4647
},
4748
Prover: &config.Prover{
4849
URL: "http://prover.com",
@@ -54,6 +55,7 @@ func (s *ConfigTestSuite) Test_LoadConfig_DefaultValues() {
5455
func (s *ConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
5556
os.Setenv("SPECTRE_OBSERVABILITY_LOG_LEVEL", "info")
5657
os.Setenv("SPECTRE_OBSERVABILITY_LOG_FILE", "out2.log")
58+
os.Setenv("SPECTRE_OBSERVABILITY_HEALTH_PORT", "9003")
5759
os.Setenv("SPECTRE_PROVER_URL", "http://prover.com")
5860
os.Setenv("SPECTRE_DOMAINS", "1:evm,2:evm")
5961

@@ -65,8 +67,9 @@ func (s *ConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
6567
s.Nil(err)
6668
s.Equal(c, &config.Config{
6769
Observability: &config.Observability{
68-
LogLevel: "info",
69-
LogFile: "out2.log",
70+
LogLevel: "info",
71+
LogFile: "out2.log",
72+
HealthPort: 9003,
7073
},
7174
Prover: &config.Prover{
7275
URL: "http://prover.com",

health/health.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// The Licensed Work is (c) 2023 Sygma
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
package health
5+
6+
import (
7+
"fmt"
8+
"net/http"
9+
10+
"github.com/rs/zerolog/log"
11+
)
12+
13+
// StartHealthEndpoint starts /health endpoint on provided port that returns ok on invocation
14+
func StartHealthEndpoint(port uint16) {
15+
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
16+
_, _ = w.Write([]byte("ok"))
17+
})
18+
19+
_ = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
20+
log.Info().Msgf("started /health endpoint on port %d", port)
21+
}

main.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message"
2626
"github.com/sygmaprotocol/spectre-node/chains/evm/prover"
2727
"github.com/sygmaprotocol/spectre-node/config"
28+
"github.com/sygmaprotocol/spectre-node/health"
2829
"github.com/sygmaprotocol/sygma-core/chains/evm"
2930
"github.com/sygmaprotocol/sygma-core/chains/evm/client"
3031
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/gas"
@@ -51,6 +52,8 @@ func main() {
5152

5253
log.Info().Msg("Loaded configuration")
5354

55+
go health.StartHealthEndpoint(cfg.Observability.HealthPort)
56+
5457
domains := make([]uint8, 0)
5558
for domain := range cfg.Domains {
5659
domains = append(domains, domain)

0 commit comments

Comments
 (0)