github.com/influxdata/influxdb/v2@v2.7.6/http/debug.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/http"
     7  
     8  	"github.com/influxdata/influxdb/v2"
     9  )
    10  
    11  // Flusher flushes data from a store to reset; used for testing.
    12  type Flusher interface {
    13  	Flush(ctx context.Context)
    14  }
    15  
    16  func Debug(ctx context.Context, next http.Handler, f Flusher, service influxdb.OnboardingService) http.HandlerFunc {
    17  	return func(w http.ResponseWriter, r *http.Request) {
    18  		if r.URL.Path == "/debug/flush" {
    19  			// DebugFlush clears all services for testing.
    20  			f.Flush(ctx)
    21  			w.Header().Set("Content-Type", "text/html; charset=utf-8")
    22  			w.WriteHeader(http.StatusOK)
    23  			return
    24  		}
    25  		if r.URL.Path == "/debug/provision" {
    26  			data := &influxdb.OnboardingRequest{
    27  				User:     "dev_user",
    28  				Password: "password",
    29  				Org:      "InfluxData",
    30  				Bucket:   "project",
    31  			}
    32  			res, err := service.OnboardInitialUser(ctx, data)
    33  			if err != nil {
    34  				w.WriteHeader(http.StatusBadRequest)
    35  				w.Write([]byte(err.Error()))
    36  				return
    37  			}
    38  			body, err := json.Marshal(res)
    39  			if err != nil {
    40  				w.WriteHeader(http.StatusBadRequest)
    41  				w.Write([]byte(err.Error()))
    42  				return
    43  			}
    44  			w.Header().Set("Content-Type", "application/json")
    45  			w.WriteHeader(http.StatusOK)
    46  			w.Write(body)
    47  			return
    48  		}
    49  		next.ServeHTTP(w, r)
    50  	}
    51  }