Support HTTPS; Add /api/debug

This commit is contained in:
Lou Knauer 2022-02-04 08:30:50 +01:00
parent f38353a879
commit 8fb2557f97
2 changed files with 26 additions and 6 deletions

24
api.go
View File

@ -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)
}
}
}()

View File

@ -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)
}