From 8fb2557f976d9b6cdcde54fd5937933722a30c25 Mon Sep 17 00:00:00 2001 From: Lou Knauer Date: Fri, 4 Feb 2022 08:30:50 +0100 Subject: [PATCH] Support HTTPS; Add /api/debug --- api.go | 24 +++++++++++++++++++----- metric-store.go | 8 +++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/api.go b/api.go index d7f688a..5bf93fb 100644 --- a/api.go +++ b/api.go @@ -307,12 +307,18 @@ func authentication(next http.Handler, publicKey ed25519.PublicKey) http.Handler }) } -func StartApiServer(address string, ctx context.Context) error { +func StartApiServer(ctx context.Context, address string, httpsConfig *HttpsConfig) error { r := mux.NewRouter() r.HandleFunc("/api/free", handleFree) r.HandleFunc("/api/write", handleWrite) r.HandleFunc("/api/query", handleQuery) + r.HandleFunc("/api/debug", func(rw http.ResponseWriter, r *http.Request) { + bw := bufio.NewWriter(rw) + defer bw.Flush() + + memoryStore.DebugDump(bw) + }) server := &http.Server{ Handler: r, @@ -331,10 +337,18 @@ func StartApiServer(address string, ctx context.Context) error { } go func() { - log.Printf("API http endpoint listening on '%s'\n", address) - err := server.ListenAndServe() - if err != nil && err != http.ErrServerClosed { - log.Println(err) + if httpsConfig != nil { + log.Printf("API https endpoint listening on '%s'\n", address) + err := server.ListenAndServeTLS(httpsConfig.CertFile, httpsConfig.KeyFile) + if err != nil && err != http.ErrServerClosed { + log.Println(err) + } + } else { + log.Printf("API http endpoint listening on '%s'\n", address) + err := server.ListenAndServe() + if err != nil && err != http.ErrServerClosed { + log.Println(err) + } } }() diff --git a/metric-store.go b/metric-store.go index 01ef0fd..4775442 100644 --- a/metric-store.go +++ b/metric-store.go @@ -20,12 +20,18 @@ type MetricConfig struct { Scope string `json:"scope"` } +type HttpsConfig struct { + CertFile string `json:"cert"` + KeyFile string `json:"key"` +} + type Config struct { Metrics map[string]MetricConfig `json:"metrics"` RetentionInMemory string `json:"retention-in-memory"` Nats string `json:"nats"` JwtPublicKey string `json:"jwt-public-key"` HttpApiAddress string `json:"http-api-address"` + HttpsConfig *HttpsConfig `json:"https"` Checkpoints struct { Interval string `json:"interval"` RootDir string `json:"directory"` @@ -189,7 +195,7 @@ func main() { wg.Add(1) go func() { - err := StartApiServer(conf.HttpApiAddress, ctx) + err := StartApiServer(ctx, conf.HttpApiAddress, conf.HttpsConfig) if err != nil { log.Fatal(err) }