Skip to content

Commit 72f74c8

Browse files
authored
feat: tls support (#244)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent d8cec18 commit 72f74c8

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

cmd/tx-submit-api/main.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ func main() {
9898
}
9999

100100
// Start API listener
101-
logger.Infof(
102-
"starting API listener on %s:%d",
103-
cfg.Api.ListenAddress,
104-
cfg.Api.ListenPort,
105-
)
106101
if err := api.Start(cfg); err != nil {
107102
logger.Fatalf("failed to start API: %s", err)
108103
}

internal/api/api.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ var staticFS embed.FS
5252
// @license.name Apache 2.0
5353
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
5454
func Start(cfg *config.Config) error {
55+
// Standard logging
56+
logger := logging.GetLogger()
57+
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
58+
logger.Infof(
59+
"starting API TLS listener on %s:%d",
60+
cfg.Api.ListenAddress,
61+
cfg.Api.ListenPort,
62+
)
63+
} else {
64+
logger.Infof(
65+
"starting API listener on %s:%d",
66+
cfg.Api.ListenAddress,
67+
cfg.Api.ListenPort,
68+
)
69+
}
5570
// Disable gin debug and color output
5671
gin.SetMode(gin.ReleaseMode)
5772
gin.DisableConsoleColor()
@@ -60,8 +75,6 @@ func Start(cfg *config.Config) error {
6075
router := gin.New()
6176
// Catch panics and return a 500
6277
router.Use(gin.Recovery())
63-
// Standard logging
64-
logger := logging.GetLogger()
6578
// Access logging
6679
accessLogger := logging.GetAccessLogger()
6780
skipPaths := []string{}
@@ -138,9 +151,17 @@ func Start(cfg *config.Config) error {
138151
router.GET("/api/hastx/:tx_hash", handleHasTx)
139152

140153
// Start API listener
141-
return router.Run(fmt.Sprintf("%s:%d",
142-
cfg.Api.ListenAddress,
143-
cfg.Api.ListenPort))
154+
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
155+
return router.RunTLS(
156+
fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort),
157+
cfg.Tls.CertFilePath,
158+
cfg.Tls.KeyFilePath,
159+
)
160+
} else {
161+
return router.Run(fmt.Sprintf("%s:%d",
162+
cfg.Api.ListenAddress,
163+
cfg.Api.ListenPort))
164+
}
144165
}
145166

146167
func handleHealthcheck(c *gin.Context) {

internal/config/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Config struct {
2828
Metrics MetricsConfig `yaml:"metrics"`
2929
Debug DebugConfig `yaml:"debug"`
3030
Node NodeConfig `yaml:"node"`
31+
Tls TlsConfig `yaml:"tls"`
3132
}
3233

3334
type LoggingConfig struct {
@@ -60,14 +61,19 @@ type NodeConfig struct {
6061
Timeout uint `yaml:"timeout" envconfig:"CARDANO_NODE_SOCKET_TIMEOUT"`
6162
}
6263

64+
type TlsConfig struct {
65+
CertFilePath string `yaml:"certFilePath" envconfig:"TLS_CERT_FILE_PATH"`
66+
KeyFilePath string `yaml:"keyFilePath" envconfig:"TLS_KEY_FILE_PATH"`
67+
}
68+
6369
// Singleton config instance with default values
6470
var globalConfig = &Config{
6571
Logging: LoggingConfig{
6672
Level: "info",
6773
Healthchecks: false,
6874
},
6975
Api: ApiConfig{
70-
ListenAddress: "",
76+
ListenAddress: "0.0.0.0",
7177
ListenPort: 8090,
7278
},
7379
Debug: DebugConfig{

0 commit comments

Comments
 (0)