Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLS cert and key flags for server #260

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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