api.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package api
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "strconv"
  9. "sync"
  10. "syscall"
  11. "time"
  12. "github.com/gorilla/mux"
  13. "github.com/sirupsen/logrus"
  14. )
  15. type Api struct {
  16. httpServer *http.Server
  17. mux *mux.Router
  18. online bool
  19. running chan os.Signal
  20. ssl bool
  21. cert string
  22. key string
  23. certMu sync.Mutex
  24. tlscert *tls.Certificate
  25. }
  26. type ApiConfig struct {
  27. Address string
  28. Port int
  29. SSL bool
  30. Cert string
  31. Key string
  32. }
  33. func NewApi(config ApiConfig) *Api {
  34. logrus.Info(config)
  35. srv := &http.Server{
  36. Addr: config.Address + ":" + strconv.Itoa(config.Port),
  37. ReadTimeout: 5 * time.Second,
  38. WriteTimeout: 10 * time.Second,
  39. IdleTimeout: 120 * time.Second,
  40. }
  41. logrus.Warn(srv.Addr)
  42. apiWebServer := &Api{
  43. httpServer: srv,
  44. mux: mux.NewRouter(),
  45. online: false,
  46. }
  47. if config.SSL {
  48. apiWebServer.ssl = true
  49. apiWebServer.cert = config.Cert
  50. apiWebServer.key = config.Key
  51. apiWebServer.tlscert = nil
  52. }
  53. return apiWebServer
  54. }
  55. func (api *Api) AddHandler(path string, handler func(http.ResponseWriter, *http.Request), method string) {
  56. api.mux.HandleFunc(path, handler).Methods(method)
  57. }
  58. func (api *Api) Start(wg *sync.WaitGroup) error {
  59. defer wg.Done()
  60. api.httpServer.Handler = api.mux
  61. api.httpServer.TLSConfig = &tls.Config{
  62. GetCertificate: api.getCertificate,
  63. }
  64. go func() {
  65. if api.ssl {
  66. err := api.httpServer.ListenAndServeTLS(api.cert, api.key)
  67. if err != nil {
  68. logrus.Fatal(err)
  69. }
  70. } else {
  71. api.httpServer.ListenAndServe()
  72. }
  73. }()
  74. go logrus.Info("API webserver has started")
  75. api.online = true
  76. api.running = make(chan os.Signal, 1)
  77. signal.Notify(api.running, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
  78. <-api.running
  79. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  80. if err := api.httpServer.Shutdown(ctx); err != nil {
  81. go logrus.Error(err)
  82. } else {
  83. go logrus.Info("API webserver shutdown")
  84. }
  85. api.online = false
  86. return nil
  87. }
  88. func (api *Api) Stop() {
  89. if api.online {
  90. go logrus.Info("Shutting down API webserver")
  91. api.running <- syscall.SIGINT
  92. }
  93. }
  94. func (api *Api) getCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
  95. api.certMu.Lock()
  96. defer api.certMu.Unlock()
  97. cert, err := tls.LoadX509KeyPair(api.cert, api.key)
  98. if err != nil {
  99. go logrus.Error(err)
  100. return nil, err
  101. }
  102. return &cert, nil
  103. }