Skip to content

Commit

Permalink
Merge pull request #2 from NfonAG/add-ssl-support
Browse files Browse the repository at this point in the history
added tls support
  • Loading branch information
rrreeeyyy authored Sep 13, 2018
2 parents 119e321 + fc6670b commit a9411b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func start(config *config.Config) {
Handler: http.DefaultServeMux,
}

err = server.ServeHTTPAndHandleSignal(lsn, *srv, *config.ShutDownTimeout)
err = server.ServeHTTPAndHandleSignal(lsn, *srv, *config.ShutDownTimeout, *config.TLSConfig)
if err != nil {
errorLogger.Fatal(err)
}
Expand Down
6 changes: 6 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
# SERVER_STARTER:/path/to/socket
listen: "0.0.0.0:9099"

# enable TLS (optional)

tls:
certfile: "/path/to/cert.crt"
keyfile: "/path/to/keyfile.key"

# access_log (optional)
access_log:
format: "ltsv"
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ const (

type Config struct {
Listen *string `yaml:"listen" validate:"required"`
TLSConfig *TLSConfig `yaml:"tls"`
ShutDownTimeout *time.Duration `yaml:"shutdown_timeout"`
ExporterConfigs map[string]ExporterConfig `yaml:"exporters" validate:"required,dive"`
AccessLogConfig *AccessLogConfig `yaml:"access_log"`
ErrorLogConfig *ErrorLogConfig `yaml:"error_log"`
}

type TLSConfig struct {
CertFile *string `yaml:"certfile" validate:"required"`
KeyFile *string `yaml:"keyfile" validate:"required"`
}

type AccessLogConfig struct {
Format *string `yaml:"format" validate:"required"`
Path *string `yaml:"path"`
Expand Down
16 changes: 12 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"os/signal"
"syscall"
"time"

"github.com/rrreeeyyy/exporter_proxy/config"
)

func ServeHTTPAndHandleSignal(listener net.Listener, server http.Server, timeout time.Duration) error {
go func() {
server.Serve(listener)
}()
func ServeHTTPAndHandleSignal(listener net.Listener, server http.Server, timeout time.Duration, tlsconfig config.TLSConfig) error {
if &tlsconfig != nil {
go func() {
server.ServeTLS(listener, *tlsconfig.CertFile, *tlsconfig.KeyFile)
}()
} else {
go func() {
server.Serve(listener)
}()
}

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM)
Expand Down

0 comments on commit a9411b3

Please sign in to comment.