2020-12-30 01:22:17 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
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"
|
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
|
|
|
|
server.TLSConfig = cfg.Web.TLSConfig()
|
2021-02-01 06:37:56 +00:00
|
|
|
if os.Getenv("ROUTER_TEST") == "true" {
|
|
|
|
return
|
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
log.Println("[controller][Handle] Listening on " + cfg.Web.SocketAddress())
|
|
|
|
if server.TLSConfig != nil {
|
|
|
|
log.Println("[controller][Handle]", app.ListenTLS(cfg.Web.SocketAddress(), "", ""))
|
2023-04-22 16:12:56 +00:00
|
|
|
} else {
|
2023-07-09 00:37:41 +00:00
|
|
|
log.Println("[controller][Handle]", app.Listen(cfg.Web.SocketAddress()))
|
2023-04-22 16:12:56 +00:00
|
|
|
}
|
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
|
|
|
}
|