123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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
- }
- }
|