Skip to content

Commit

Permalink
feat: tls support (#244)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 authored Oct 4, 2024
1 parent d8cec18 commit 72f74c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
5 changes: 0 additions & 5 deletions cmd/tx-submit-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ func main() {
}

// Start API listener
logger.Infof(
"starting API listener on %s:%d",
cfg.Api.ListenAddress,
cfg.Api.ListenPort,
)
if err := api.Start(cfg); err != nil {
logger.Fatalf("failed to start API: %s", err)
}
Expand Down
31 changes: 26 additions & 5 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ var staticFS embed.FS
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func Start(cfg *config.Config) error {
// Standard logging
logger := logging.GetLogger()
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
logger.Infof(
"starting API TLS listener on %s:%d",
cfg.Api.ListenAddress,
cfg.Api.ListenPort,
)
} else {
logger.Infof(
"starting API listener on %s:%d",
cfg.Api.ListenAddress,
cfg.Api.ListenPort,
)
}
// Disable gin debug and color output
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
Expand All @@ -60,8 +75,6 @@ func Start(cfg *config.Config) error {
router := gin.New()
// Catch panics and return a 500
router.Use(gin.Recovery())
// Standard logging
logger := logging.GetLogger()
// Access logging
accessLogger := logging.GetAccessLogger()
skipPaths := []string{}
Expand Down Expand Up @@ -138,9 +151,17 @@ func Start(cfg *config.Config) error {
router.GET("/api/hastx/:tx_hash", handleHasTx)

// Start API listener
return router.Run(fmt.Sprintf("%s:%d",
cfg.Api.ListenAddress,
cfg.Api.ListenPort))
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
return router.RunTLS(
fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort),
cfg.Tls.CertFilePath,
cfg.Tls.KeyFilePath,
)
} else {
return router.Run(fmt.Sprintf("%s:%d",
cfg.Api.ListenAddress,
cfg.Api.ListenPort))
}
}

func handleHealthcheck(c *gin.Context) {
Expand Down
8 changes: 7 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {
Metrics MetricsConfig `yaml:"metrics"`
Debug DebugConfig `yaml:"debug"`
Node NodeConfig `yaml:"node"`
Tls TlsConfig `yaml:"tls"`
}

type LoggingConfig struct {
Expand Down Expand Up @@ -60,14 +61,19 @@ type NodeConfig struct {
Timeout uint `yaml:"timeout" envconfig:"CARDANO_NODE_SOCKET_TIMEOUT"`
}

type TlsConfig struct {
CertFilePath string `yaml:"certFilePath" envconfig:"TLS_CERT_FILE_PATH"`
KeyFilePath string `yaml:"keyFilePath" envconfig:"TLS_KEY_FILE_PATH"`
}

// Singleton config instance with default values
var globalConfig = &Config{
Logging: LoggingConfig{
Level: "info",
Healthchecks: false,
},
Api: ApiConfig{
ListenAddress: "",
ListenAddress: "0.0.0.0",
ListenPort: 8090,
},
Debug: DebugConfig{
Expand Down

0 comments on commit 72f74c8

Please sign in to comment.