Thisnthat hace 5 años
padre
commit
667ef83bd1
Se han modificado 1 ficheros con 11 adiciones y 3 borrados
  1. 11 3
      api.go

+ 11 - 3
api.go

@@ -14,35 +14,43 @@ import (
 
 type Api struct {
 	httpServer *http.Server
+	mux        *http.ServeMux
 	online     bool
 	running    chan os.Signal
 }
 
-func NewApi(mux *http.ServeMux, serverAddress string) *Api {
+func NewApi(serverAddress string) *Api {
 	srv := &http.Server{
 		Addr:         serverAddress,
 		ReadTimeout:  5 * time.Second,
 		WriteTimeout: 10 * time.Second,
 		IdleTimeout:  120 * time.Second,
-		Handler:      mux,
 	}
 
 	webserver := &Api{
 		httpServer: srv,
+		mux:        http.NewServeMux(),
 		online:     false,
 	}
 
 	return webserver
 }
 
+func (api *Api) AddHandler(path string, handler func(http.ResponseWriter, *http.Request)) {
+	api.mux.HandleFunc(path, handler)
+}
+
 func (api *Api) Start(wg *sync.WaitGroup) error {
 	defer wg.Done()
 
+	api.httpServer.Handler = api.mux
+
 	go func() {
 		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
@@ -60,7 +68,7 @@ func (api *Api) Start(wg *sync.WaitGroup) error {
 
 func (api *Api) Stop() {
 	if api.online {
-		logrus.Infof("Shutting down Bot")
+		logrus.Infof("Shutting down API webserver")
 		api.running <- syscall.SIGINT
 	}
 }