|
@@ -2,16 +2,16 @@ package api
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
- "fmt"
|
|
|
|
"net/http"
|
|
"net/http"
|
|
"os"
|
|
"os"
|
|
"os/signal"
|
|
"os/signal"
|
|
|
|
+ "strconv"
|
|
"sync"
|
|
"sync"
|
|
"syscall"
|
|
"syscall"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
- "github.com/sirupsen/logrus"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/mux"
|
|
|
|
+ "github.com/sirupsen/logrus"
|
|
)
|
|
)
|
|
|
|
|
|
type Api struct {
|
|
type Api struct {
|
|
@@ -19,27 +19,46 @@ type Api struct {
|
|
mux *mux.Router
|
|
mux *mux.Router
|
|
online bool
|
|
online bool
|
|
running chan os.Signal
|
|
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(serverAddress string) *Api {
|
|
|
|
|
|
+func NewApi(config ApiConfig) *Api {
|
|
|
|
+ logrus.Info(config)
|
|
srv := &http.Server{
|
|
srv := &http.Server{
|
|
- Addr: serverAddress,
|
|
|
|
|
|
+ Addr: config.Address + ":" + strconv.Itoa(config.Port),
|
|
ReadTimeout: 5 * time.Second,
|
|
ReadTimeout: 5 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ logrus.Warn(srv.Addr)
|
|
|
|
+
|
|
webserver := &Api{
|
|
webserver := &Api{
|
|
httpServer: srv,
|
|
httpServer: srv,
|
|
mux: mux.NewRouter(),
|
|
mux: mux.NewRouter(),
|
|
online: false,
|
|
online: false,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if config.SSL {
|
|
|
|
+ webserver.ssl = true
|
|
|
|
+ webserver.cert = config.Cert
|
|
|
|
+ webserver.key = config.Key
|
|
|
|
+ }
|
|
|
|
+
|
|
return webserver
|
|
return webserver
|
|
}
|
|
}
|
|
|
|
|
|
func (api *Api) AddHandler(path string, handler func(http.ResponseWriter, *http.Request), method string) {
|
|
func (api *Api) AddHandler(path string, handler func(http.ResponseWriter, *http.Request), method string) {
|
|
- fmt.Println(path)
|
|
|
|
api.mux.HandleFunc(path, handler).Methods(method)
|
|
api.mux.HandleFunc(path, handler).Methods(method)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -49,7 +68,14 @@ func (api *Api) Start(wg *sync.WaitGroup) error {
|
|
api.httpServer.Handler = api.mux
|
|
api.httpServer.Handler = api.mux
|
|
|
|
|
|
go func() {
|
|
go func() {
|
|
- api.httpServer.ListenAndServe()
|
|
|
|
|
|
+ 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")
|
|
go logrus.Info("API webserver has started")
|
|
@@ -61,9 +87,9 @@ func (api *Api) Start(wg *sync.WaitGroup) error {
|
|
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
|
|
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
|
if err := api.httpServer.Shutdown(ctx); err != nil {
|
|
if err := api.httpServer.Shutdown(ctx); err != nil {
|
|
- logrus.Error(err)
|
|
|
|
|
|
+ go logrus.Error(err)
|
|
} else {
|
|
} else {
|
|
- logrus.Info("API webserver shutdown")
|
|
|
|
|
|
+ go logrus.Info("API webserver shutdown")
|
|
}
|
|
}
|
|
api.online = false
|
|
api.online = false
|
|
return nil
|
|
return nil
|
|
@@ -71,7 +97,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 API webserver")
|
|
|
|
|
|
+ go logrus.Infof("Shutting down API webserver")
|
|
api.running <- syscall.SIGINT
|
|
api.running <- syscall.SIGINT
|
|
}
|
|
}
|
|
}
|
|
}
|