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