Support for TLS on http services (#18)

Support for TLS on http services
This commit is contained in:
Alex Unger
2020-01-20 14:33:57 +01:00
committed by GitHub
2 changed files with 20 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package http
import (
"context"
"crypto/tls"
"github.com/micro/cli"
"github.com/owncloud/ocis-pkg/log"
@@ -13,6 +14,7 @@ type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
TLSConfig *tls.Config
Namespace string
Name string
Version string
@@ -82,3 +84,10 @@ func Flags(flags ...cli.Flag) Option {
o.Flags = append(o.Flags, flags...)
}
}
// TLSConfig provides a function to set the TLSConfig option.
func TLSConfig(config *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = config
}
}

View File

@@ -1,6 +1,7 @@
package http
import (
"crypto/tls"
"strings"
"time"
@@ -15,9 +16,8 @@ type Service struct {
// NewService initializes a new http service.
func NewService(opts ...Option) Service {
sopts := newOptions(opts...)
sopts.Logger.Info().
Str("transport", "http").
Str("transport", transport(sopts.TLSConfig)).
Str("addr", sopts.Address).
Msg("Starting server")
@@ -36,6 +36,7 @@ func NewService(opts ...Option) Service {
web.RegisterTTL(time.Second * 30),
web.RegisterInterval(time.Second * 10),
web.Context(sopts.Context),
web.TLSConfig(sopts.TLSConfig),
web.Flags(sopts.Flags...),
}
@@ -45,3 +46,11 @@ func NewService(opts ...Option) Service {
),
}
}
func transport(secure *tls.Config) string {
if secure != nil {
return "https"
}
return "http"
}