Skip to content

Commit

Permalink
Add TLS cert and key flags for server (#260)
Browse files Browse the repository at this point in the history
Signed-off-by: Douglass Kirkley <[email protected]>
Co-authored-by: Douglass Kirkley <[email protected]>
  • Loading branch information
dougkirkley and Douglass Kirkley authored Feb 5, 2025
1 parent 38fc076 commit 408edae
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ import (
persesMiddleware "github.com/perses/common/echo/middleware"
)

var hidePort bool
var (
hidePort bool
// https cert for server
cert string
// https key for server
key string
)

func init() {
flag.BoolVar(&hidePort, "web.hide-port", false, "If true, it won't be print on stdout the port listened to receive the HTTP request")
flag.StringVar(&cert, "web.tls-cert-file", "", "The path to the cert to use for the HTTPS server")
flag.StringVar(&key, "web.tls-key-file", "", "The path to the key to use for the HTTPS server")
}

type Register interface {
Expand Down Expand Up @@ -212,6 +220,8 @@ func (b *Builder) build() (*server, error) {
addr: b.addr,
apis: b.apis,
e: e,
cert: cert,
key: key,
mdws: b.mdws,
preMDWs: b.preMDWs,
shutdownTimeout: 30 * time.Second,
Expand All @@ -224,6 +234,8 @@ type server struct {
addr string
apis []Register
e *echo.Echo
cert string
key string
mdws []echo.MiddlewareFunc
preMDWs []echo.MiddlewareFunc
shutdownTimeout time.Duration
Expand Down Expand Up @@ -257,9 +269,16 @@ func (s *server) Execute(ctx context.Context, cancelFunc context.CancelFunc) err
serverCtx, serverCancelFunc := context.WithCancel(ctx)
go func() {
defer serverCancelFunc()
if err := s.e.Start(s.addr); err != nil {
logrus.WithError(err).Info("http server stopped")
if s.cert != "" && s.key != "" {
if err := s.e.StartTLS(s.addr, s.cert, s.key); err != nil {
logrus.WithError(err).Info("http server stopped")
}
} else {
if err := s.e.Start(s.addr); err != nil {
logrus.WithError(err).Info("http server stopped")
}
}

logrus.Debug("go routine running the http server has been stopped.")
}()
// Wait for the end of the task or cancellation
Expand Down

0 comments on commit 408edae

Please sign in to comment.