Skip to content

Commit

Permalink
feat: enable https/tls for apimanager
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Jun 22, 2024
1 parent 275d0cb commit 8d5b72b
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions apimanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -66,18 +67,49 @@ type RouteGroup struct {
App fiber.Router
}

// Config is the configuration of the server.
type Config struct {
// Address is the address to listen on.
Address string `yaml:"address" mapstructure:"address"`
// BasePath is the base path of the API.
BasePath string `yaml:"basePath" mapstructure:"basePath"`
// TLS is the TLS configuration.
TLS TLSConfig `yaml:"tls" mapstructure:"tls"`
}

// TLSConfig is the TLS configuration.
type TLSConfig struct {
// Enabled indicates if TLS is enabled.
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
// CertFile is the path to the certificate file.
CertFile string `yaml:"certPath" mapstructure:"certPath"`
// CertKeyFile is the path to the certificate key file.
CertKeyFile string `yaml:"keyPath" mapstructure:"keyPath"`
}

// IsEmpty checks if the configuration is empty.
func (c Config) IsEmpty() bool {
return reflect.DeepEqual(c, Config{})
}

// Validate validates the configuration.
func (c *Config) Validate() error {
var err error
if c.Address == "" {
return errors.New("api.address is required")
err = errors.New("api.address is required")
}
return nil

if c.TLS.Enabled {
if c.TLS.CertFile == "" {
err = errors.Join(err, errors.New("api.tls.certPath is required"))
}

if c.TLS.CertKeyFile == "" {
err = errors.Join(err, errors.New("api.tls.keyPath is required"))
}
}

return err
}

type server struct {
Expand Down Expand Up @@ -105,6 +137,10 @@ func New(c *Config, middlewares ...fiber.Handler) Server {
middlewares = append(middlewares, middleware.Recover(), middleware.Logger("/healthz"))
}

if c.BasePath == "" {
c.BasePath = "/"
}

return &server{
mu: sync.Mutex{},
config: c,
Expand All @@ -126,9 +162,17 @@ func (s *server) Run(ctx context.Context) error {
return err
}

var cfg []fiber.ListenConfig
if s.config.TLS.Enabled {
cfg = append(cfg, fiber.ListenConfig{
CertFile: s.config.TLS.CertFile,
CertKeyFile: s.config.TLS.CertKeyFile,
})
}

cErr := make(chan error, 1)
go func() {
cErr <- s.app.Listen(s.config.Address)
cErr <- s.app.Listen(s.config.Address, cfg...)
}()

select {
Expand Down

0 comments on commit 8d5b72b

Please sign in to comment.