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

16
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 := mux.NewRouter()
r.HandleFunc("/api/free", handleFree) r.HandleFunc("/api/free", handleFree)
r.HandleFunc("/api/write", handleWrite) r.HandleFunc("/api/write", handleWrite)
r.HandleFunc("/api/query", handleQuery) 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{ server := &http.Server{
Handler: r, Handler: r,
@ -331,11 +337,19 @@ func StartApiServer(address string, ctx context.Context) error {
} }
go func() { go func() {
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) log.Printf("API http endpoint listening on '%s'\n", address)
err := server.ListenAndServe() err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed { if err != nil && err != http.ErrServerClosed {
log.Println(err) log.Println(err)
} }
}
}() }()
for { for {

View File

@ -20,12 +20,18 @@ type MetricConfig struct {
Scope string `json:"scope"` Scope string `json:"scope"`
} }
type HttpsConfig struct {
CertFile string `json:"cert"`
KeyFile string `json:"key"`
}
type Config struct { type Config struct {
Metrics map[string]MetricConfig `json:"metrics"` Metrics map[string]MetricConfig `json:"metrics"`
RetentionInMemory string `json:"retention-in-memory"` RetentionInMemory string `json:"retention-in-memory"`
Nats string `json:"nats"` Nats string `json:"nats"`
JwtPublicKey string `json:"jwt-public-key"` JwtPublicKey string `json:"jwt-public-key"`
HttpApiAddress string `json:"http-api-address"` HttpApiAddress string `json:"http-api-address"`
HttpsConfig *HttpsConfig `json:"https"`
Checkpoints struct { Checkpoints struct {
Interval string `json:"interval"` Interval string `json:"interval"`
RootDir string `json:"directory"` RootDir string `json:"directory"`
@ -189,7 +195,7 @@ func main() {
wg.Add(1) wg.Add(1)
go func() { go func() {
err := StartApiServer(conf.HttpApiAddress, ctx) err := StartApiServer(ctx, conf.HttpApiAddress, conf.HttpsConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }