Skip to content

Commit 184ea38

Browse files
authored
Merge pull request #14 from cloudstruct/feature/debug-endpoint
feat: optional debug endpoint
2 parents 964d4d1 + 38f7bb3 commit 184ea38

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

cmd/cardano-submit-api/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/cloudstruct/go-cardano-submit-api/internal/api"
77
"github.com/cloudstruct/go-cardano-submit-api/internal/config"
88
"github.com/cloudstruct/go-cardano-submit-api/internal/logging"
9+
"net/http"
10+
_ "net/http/pprof"
911
"os"
1012
)
1113

@@ -36,6 +38,17 @@ func main() {
3638
}
3739
}()
3840

41+
// Start debug listener
42+
if cfg.Debug.ListenPort > 0 {
43+
logger.Infof("starting debug listener on %s:%d", cfg.Debug.ListenAddress, cfg.Debug.ListenPort)
44+
go func() {
45+
err := http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Debug.ListenAddress, cfg.Debug.ListenPort), nil)
46+
if err != nil {
47+
logger.Fatalf("failed to start debug listener: %s", err)
48+
}
49+
}()
50+
}
51+
3952
// Start API listener
4053
logger.Infof("starting API listener on %s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort)
4154
if err := api.Start(cfg); err != nil {

config.yaml.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ api:
1919
# This can also be set via the API_LISTEN_PORT environment variable
2020
port: 8090
2121

22+
# The debug endpoint provides access to pprof for debugging purposes. This is
23+
# disabled by default, but it can be enabled by setting the port to a non-zero
24+
# value
25+
debug:
26+
# Listen address for the debug endpoint
27+
#
28+
# This can also be set via the DEBUG_ADDRESS environment variable
29+
address: localhost
30+
31+
# Listen port for the debug endpoint
32+
#
33+
# This can also be set via the DEBUG_PORT environment variable
34+
port: 0
35+
2236
node:
2337
# Path to UNIX socket file for cardano-node
2438
#

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
type Config struct {
1717
Logging LoggingConfig `yaml:"logging"`
1818
Api ApiConfig `yaml:"api"`
19+
Debug DebugConfig `yaml:"debug"`
1920
Node NodeConfig `yaml:"node"`
2021
}
2122

@@ -28,6 +29,11 @@ type ApiConfig struct {
2829
ListenPort uint `yaml:"port" envconfig:"API_LISTEN_PORT"`
2930
}
3031

32+
type DebugConfig struct {
33+
ListenAddress string `yaml:"address" envconfig:"DEBUG_ADDRESS"`
34+
ListenPort uint `yaml:"port" envconfig:"DEBUG_PORT"`
35+
}
36+
3137
type NodeConfig struct {
3238
Network string `yaml:"network" envconfig:"NETWORK"`
3339
NetworkMagic uint32 `yaml:"networkMagic" envconfig:"CARDANO_NODE_NETWORK_MAGIC"`
@@ -45,6 +51,10 @@ var globalConfig = &Config{
4551
ListenAddress: "",
4652
ListenPort: 8090,
4753
},
54+
Debug: DebugConfig{
55+
ListenAddress: "localhost",
56+
ListenPort: 0,
57+
},
4858
Node: NodeConfig{
4959
Network: "mainnet",
5060
SocketPath: "/node-ipc/node.socket",

0 commit comments

Comments
 (0)