Skip to content

Commit 408edae

Browse files
dougkirkleyDouglass Kirkley
and
Douglass Kirkley
authored
Add TLS cert and key flags for server (#260)
Signed-off-by: Douglass Kirkley <[email protected]> Co-authored-by: Douglass Kirkley <[email protected]>
1 parent 38fc076 commit 408edae

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

Diff for: echo/server.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ import (
6969
persesMiddleware "github.com/perses/common/echo/middleware"
7070
)
7171

72-
var hidePort bool
72+
var (
73+
hidePort bool
74+
// https cert for server
75+
cert string
76+
// https key for server
77+
key string
78+
)
7379

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

7886
type Register interface {
@@ -212,6 +220,8 @@ func (b *Builder) build() (*server, error) {
212220
addr: b.addr,
213221
apis: b.apis,
214222
e: e,
223+
cert: cert,
224+
key: key,
215225
mdws: b.mdws,
216226
preMDWs: b.preMDWs,
217227
shutdownTimeout: 30 * time.Second,
@@ -224,6 +234,8 @@ type server struct {
224234
addr string
225235
apis []Register
226236
e *echo.Echo
237+
cert string
238+
key string
227239
mdws []echo.MiddlewareFunc
228240
preMDWs []echo.MiddlewareFunc
229241
shutdownTimeout time.Duration
@@ -257,9 +269,16 @@ func (s *server) Execute(ctx context.Context, cancelFunc context.CancelFunc) err
257269
serverCtx, serverCancelFunc := context.WithCancel(ctx)
258270
go func() {
259271
defer serverCancelFunc()
260-
if err := s.e.Start(s.addr); err != nil {
261-
logrus.WithError(err).Info("http server stopped")
272+
if s.cert != "" && s.key != "" {
273+
if err := s.e.StartTLS(s.addr, s.cert, s.key); err != nil {
274+
logrus.WithError(err).Info("http server stopped")
275+
}
276+
} else {
277+
if err := s.e.Start(s.addr); err != nil {
278+
logrus.WithError(err).Info("http server stopped")
279+
}
262280
}
281+
263282
logrus.Debug("go routine running the http server has been stopped.")
264283
}()
265284
// Wait for the end of the task or cancellation

0 commit comments

Comments
 (0)