Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Support for TLS on http services (#18)
Browse files Browse the repository at this point in the history
Support for TLS on http services
  • Loading branch information
refs authored Jan 20, 2020
2 parents 3dd614f + 1a7ad9f commit 5e5def3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions service/http/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"crypto/tls"

"github.com/micro/cli"
"github.com/owncloud/ocis-pkg/log"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
13 changes: 11 additions & 2 deletions service/http/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"crypto/tls"
"strings"
"time"

Expand All @@ -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")

Expand All @@ -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...),
}

Expand All @@ -45,3 +46,11 @@ func NewService(opts ...Option) Service {
),
}
}

func transport(secure *tls.Config) string {
if secure != nil {
return "https"
}

return "http"
}

0 comments on commit 5e5def3

Please sign in to comment.