mirror of
				https://github.com/ClusterCockpit/cc-metric-store.git
				synced 2025-11-04 10:45:07 +01:00 
			
		
		
		
	Support HTTPS; Add /api/debug
This commit is contained in:
		
							
								
								
									
										16
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								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 := 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 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user