2020-12-30 02:22:17 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2021-01-24 10:48:07 +01:00
|
|
|
"os"
|
2020-12-30 02:22:17 +01:00
|
|
|
"time"
|
|
|
|
|
2023-07-09 02:37:41 +02:00
|
|
|
"github.com/TwiN/gatus/v5/api"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
2023-07-09 02:37:41 +02:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2020-12-30 02:22:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-07-09 02:37:41 +02:00
|
|
|
app *fiber.App
|
2020-12-30 02:22:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handle creates the router and starts the server
|
2022-07-29 02:07:53 +02:00
|
|
|
func Handle(cfg *config.Config) {
|
2023-07-09 02:37:41 +02: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 07:37:56 +01:00
|
|
|
if os.Getenv("ROUTER_TEST") == "true" {
|
|
|
|
return
|
|
|
|
}
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Println("[controller.Handle] Listening on " + cfg.Web.SocketAddress())
|
2023-07-21 01:02:34 +02:00
|
|
|
if cfg.Web.HasTLS() {
|
|
|
|
err := app.ListenTLS(cfg.Web.SocketAddress(), cfg.Web.TLS.CertificateFile, cfg.Web.TLS.PrivateKeyFile)
|
|
|
|
if err != nil {
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Fatal("[controller.Handle]", err)
|
2023-07-21 01:02:34 +02:00
|
|
|
}
|
2023-04-22 18:12:56 +02:00
|
|
|
} else {
|
2023-07-21 01:02:34 +02:00
|
|
|
err := app.Listen(cfg.Web.SocketAddress())
|
|
|
|
if err != nil {
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Fatal("[controller.Handle]", err)
|
2023-07-21 01:02:34 +02:00
|
|
|
}
|
2023-04-22 18:12:56 +02:00
|
|
|
}
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Println("[controller.Handle] Server has shut down successfully")
|
2021-02-06 02:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the server
|
|
|
|
func Shutdown() {
|
2023-07-09 02:37:41 +02:00
|
|
|
if app != nil {
|
|
|
|
_ = app.Shutdown()
|
|
|
|
app = nil
|
2021-02-06 02:45:28 +01:00
|
|
|
}
|
2020-12-30 02:22:17 +01:00
|
|
|
}
|