Skip to content

Commit

Permalink
Merge pull request #24 from kampka/tls-support
Browse files Browse the repository at this point in the history
Enable TLS support for web server
  • Loading branch information
warmans authored Dec 18, 2022
2 parents 9e27a2c + 9db0310 commit d63e0b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/prometheus/common/expfmt"
)

//Config is used to store the configuration of this program
// Config is used to store the configuration of this program
type Config struct {
Server struct {
Bind string
Expand All @@ -39,6 +39,8 @@ var (
targetLabelsEnabled *bool
targetLabelName *string
serverBind *string
tlsServerCert *string
tlsServerKey *string
targetScrapeTimeout *int
targets *string
insecureSkipVerifyFlag *bool
Expand All @@ -51,6 +53,9 @@ func init() {
versionFlag = boolFlag(flag.CommandLine, "version", false, "Show version and exit")
serverBind = stringFlag(flag.CommandLine, "server.bind", ":8080", "Bind the HTTP server to this address e.g. 127.0.0.1:8080 or just :8080. For unix socket use unix:/path/to/file.sock")

tlsServerCert = stringFlag(flag.CommandLine, "tls.server-cert", "", "Path to the TLS server cert for serving via HTTPS")
tlsServerKey = stringFlag(flag.CommandLine, "tls.server-key", "", "Path to the TLS server key for serving via HTTPS")

targetScrapeTimeout = intFlag(flag.CommandLine, "targets.scrape.timeout", 1000, "If a target metrics pages does not responde with this many miliseconds then timeout")
targets = stringFlag(flag.CommandLine, "targets", "", "comma separated list of targets e.g. http://localhost:8081/metrics,http://localhost:8082/metrics or url1=http://localhost:8081/metrics,url2=http://localhost:8082/metrics for custom label values")
targetLabelsEnabled = boolFlag(flag.CommandLine, "targets.label", true, "Add a label to metrics to show their origin target")
Expand Down Expand Up @@ -209,7 +214,18 @@ func main() {
}
log.Fatal(http.Serve(unixListener, mux))
} else {
log.Fatal(http.ListenAndServe(config.Server.Bind, mux))
if tlsServerCert != nil && *tlsServerCert != "" && tlsServerKey != nil && *tlsServerKey != "" {
if _, err := os.Stat(*tlsServerCert); err != nil {
log.Fatalf("Failed to load TLS server certificate: '%v'", err)
}

if _, err := os.Stat(*tlsServerKey); err != nil {
log.Fatalf("Failed to load TLS server key: '%v'", err)
}
log.Fatal(http.ListenAndServeTLS(config.Server.Bind, *tlsServerCert, *tlsServerKey, mux))
} else {
log.Fatal(http.ListenAndServe(config.Server.Bind, mux))
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ func mustReadAll(r io.Reader) string {
return string(b[:])
}

//todo: re-implement tests
//todo: re-implement tests

0 comments on commit d63e0b5

Please sign in to comment.