Skip to content

Commit 47ba06f

Browse files
authored
feat: support tls (#146)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 00f8ea4 commit 47ba06f

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

cmd/tx-submit-api-mirror/main.go

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

7373
// Start API listener
74-
logger.Infof(
75-
"starting API listener on %s:%d",
76-
cfg.Api.ListenAddress,
77-
cfg.Api.ListenPort,
78-
)
7974
if err := api.Start(cfg); err != nil {
8075
logger.Fatalf("failed to start API: %s", err)
8176
}

internal/api/api.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ import (
3535
)
3636

3737
func Start(cfg *config.Config) error {
38+
// Standard logging
39+
logger := logging.GetLogger()
40+
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
41+
logger.Infof(
42+
"starting API TLS listener on %s:%d",
43+
cfg.Api.ListenAddress,
44+
cfg.Api.ListenPort,
45+
)
46+
} else {
47+
logger.Infof(
48+
"starting API listener on %s:%d",
49+
cfg.Api.ListenAddress,
50+
cfg.Api.ListenPort,
51+
)
52+
}
3853
// Disable gin debug output
3954
gin.SetMode(gin.ReleaseMode)
4055
gin.DisableConsoleColor()
@@ -52,11 +67,18 @@ func Start(cfg *config.Config) error {
5267
router.GET("/healthcheck", handleHealthcheck)
5368
router.POST("/api/submit/tx", handleSubmitTx)
5469

55-
// Start listener
56-
err := router.Run(
57-
fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort),
58-
)
59-
return err
70+
// Start API listener
71+
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
72+
return router.RunTLS(
73+
fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort),
74+
cfg.Tls.CertFilePath,
75+
cfg.Tls.KeyFilePath,
76+
)
77+
} else {
78+
return router.Run(fmt.Sprintf("%s:%d",
79+
cfg.Api.ListenAddress,
80+
cfg.Api.ListenPort))
81+
}
6082
}
6183

6284
func handleHealthcheck(c *gin.Context) {

internal/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
type Config struct {
2626
Logging LoggingConfig `yaml:"logging"`
2727
Api ApiConfig `yaml:"api"`
28+
Tls TlsConfig `yaml:"tls"`
2829
Backends []string `yaml:"backends" envconfig:"BACKENDS"`
2930
}
3031

@@ -38,6 +39,11 @@ type ApiConfig struct {
3839
ClientTimeout uint `yaml:"client_timeout" envconfig:"CLIENT_TIMEOUT"`
3940
}
4041

42+
type TlsConfig struct {
43+
CertFilePath string `yaml:"certFilePath" envconfig:"TLS_CERT_FILE_PATH"`
44+
KeyFilePath string `yaml:"keyFilePath" envconfig:"TLS_KEY_FILE_PATH"`
45+
}
46+
4147
// Singleton config instance with default values
4248
var globalConfig = &Config{
4349
Logging: LoggingConfig{

0 commit comments

Comments
 (0)