package api import ( "context" "net/http" "os" "os/signal" "strconv" "sync" "syscall" "time" "github.com/gorilla/mux" "github.com/sirupsen/logrus" ) type Api struct { httpServer *http.Server mux *mux.Router online bool running chan os.Signal ssl bool cert string key string } type ApiConfig struct { Address string Port int SSL bool Cert string Key string } func NewApi(config ApiConfig) *Api { logrus.Info(config) srv := &http.Server{ Addr: config.Address + ":" + strconv.Itoa(config.Port), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, } logrus.Warn(srv.Addr) webserver := &Api{ httpServer: srv, mux: mux.NewRouter(), online: false, } if config.SSL { webserver.ssl = true webserver.cert = config.Cert webserver.key = config.Key } return webserver } func (api *Api) AddHandler(path string, handler func(http.ResponseWriter, *http.Request), method string) { api.mux.HandleFunc(path, handler).Methods(method) } func (api *Api) Start(wg *sync.WaitGroup) error { defer wg.Done() api.httpServer.Handler = api.mux go func() { if api.ssl { err := api.httpServer.ListenAndServeTLS(api.cert, api.key) if err != nil { logrus.Fatal(err) } } else { api.httpServer.ListenAndServe() } }() go logrus.Info("API webserver has started") api.online = true api.running = make(chan os.Signal, 1) signal.Notify(api.running, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) <-api.running ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) if err := api.httpServer.Shutdown(ctx); err != nil { go logrus.Error(err) } else { go logrus.Info("API webserver shutdown") } api.online = false return nil } func (api *Api) Stop() { if api.online { go logrus.Infof("Shutting down API webserver") api.running <- syscall.SIGINT } }