2020-12-30 01:22:17 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2021-01-24 09:48:07 +00:00
|
|
|
"os"
|
2020-12-30 01:22:17 +00:00
|
|
|
"time"
|
|
|
|
|
2023-07-09 00:37:41 +00:00
|
|
|
"github.com/TwiN/gatus/v5/api"
|
2022-12-06 06:41:09 +00:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
2024-11-14 04:54:00 +00:00
|
|
|
"github.com/TwiN/logr"
|
2023-07-09 00:37:41 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2020-12-30 01:22:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-07-09 00:37:41 +00:00
|
|
|
app *fiber.App
|
2020-12-30 01:22:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handle creates the router and starts the server
|
2022-07-29 00:07:53 +00:00
|
|
|
func Handle(cfg *config.Config) {
|
2023-07-09 00:37:41 +00:00
|
|
|
api := api.New(cfg)
|
|
|
|
app = api.Router()
|
|
|
|
server := app.Server()
|
|
|
|
server.ReadTimeout = 15 * time.Second
|
|
|
|
server.WriteTimeout = 15 * time.Second
|
|
|
|
server.IdleTimeout = 15 * time.Second
|
2021-02-01 06:37:56 +00:00
|
|
|
if os.Getenv("ROUTER_TEST") == "true" {
|
|
|
|
return
|
|
|
|
}
|
2024-11-14 04:54:00 +00:00
|
|
|
logr.Info("[controller.Handle] Listening on " + cfg.Web.SocketAddress())
|
2023-07-20 23:02:34 +00:00
|
|
|
if cfg.Web.HasTLS() {
|
|
|
|
err := app.ListenTLS(cfg.Web.SocketAddress(), cfg.Web.TLS.CertificateFile, cfg.Web.TLS.PrivateKeyFile)
|
|
|
|
if err != nil {
|
2024-11-14 04:54:00 +00:00
|
|
|
logr.Fatalf("[controller.Handle] %s", err.Error())
|
2023-07-20 23:02:34 +00:00
|
|
|
}
|
2023-04-22 16:12:56 +00:00
|
|
|
} else {
|
2023-07-20 23:02:34 +00:00
|
|
|
err := app.Listen(cfg.Web.SocketAddress())
|
|
|
|
if err != nil {
|
2024-11-14 04:54:00 +00:00
|
|
|
logr.Fatalf("[controller.Handle] %s", err.Error())
|
2023-07-20 23:02:34 +00:00
|
|
|
}
|
2023-04-22 16:12:56 +00:00
|
|
|
}
|
2024-11-14 04:54:00 +00:00
|
|
|
logr.Info("[controller.Handle] Server has shut down successfully")
|
2021-02-06 01:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the server
|
|
|
|
func Shutdown() {
|
2023-07-09 00:37:41 +00:00
|
|
|
if app != nil {
|
|
|
|
_ = app.Shutdown()
|
|
|
|
app = nil
|
2021-02-06 01:45:28 +00:00
|
|
|
}
|
2020-12-30 01:22:17 +00:00
|
|
|
}
|