Skip to content
Snippets Groups Projects
http.go 982 B
Newer Older
  • Learn to ignore specific revisions
  • package gosdn
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    import (
    
    	"context"
    	"fmt"
    	"net/http"
    	"time"
    
    
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	log "github.com/sirupsen/logrus"
    )
    
    
    func stopHttpServer() error {
    	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    	defer cancel()
    	log.Info("shutting down http server")
    	return c.httpServer.Shutdown(ctx)
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func registerHttpHandler() {
    
    	defer func() {
    		if r := recover(); r != nil {
    			fmt.Println("Recovered in f", r)
    		}
    	}()
    
    	http.HandleFunc("/livez", healthCheck)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	http.HandleFunc("/readyz", readynessCheck)
    
    	http.Handle("/metrics", promhttp.Handler())
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    
    func startHttpServer() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	registerHttpHandler()
    
    	c.httpServer = &http.Server{Addr: ":8080"}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	go func() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		log.Info(c.httpServer.ListenAndServe())
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}()
    }
    
    
    func healthCheck(writer http.ResponseWriter, request *http.Request) {
    	writer.WriteHeader(http.StatusOK)
    }
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func readynessCheck(writer http.ResponseWriter, request *http.Request) {
    	writer.WriteHeader(http.StatusOK)
    }