2020-12-30 02:22:17 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2021-02-06 02:45:28 +01:00
|
|
|
"context"
|
2020-12-30 02:22:17 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-01-24 10:48:07 +01:00
|
|
|
"os"
|
2020-12-30 02:22:17 +01:00
|
|
|
"time"
|
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
|
|
|
"github.com/TwiN/gatus/v5/controller/handler"
|
2020-12-30 02:22:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-01 07:37:56 +01:00
|
|
|
// server is the http.Server created by Handle.
|
|
|
|
// The only reason it exists is for testing purposes.
|
|
|
|
server *http.Server
|
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) {
|
2022-10-10 03:33:31 +02:00
|
|
|
var router http.Handler = handler.CreateRouter(cfg)
|
2021-01-24 10:48:07 +01:00
|
|
|
if os.Getenv("ENVIRONMENT") == "dev" {
|
2021-09-13 00:39:09 +02:00
|
|
|
router = handler.DevelopmentCORS(router)
|
2021-01-24 10:48:07 +01:00
|
|
|
}
|
2021-02-01 07:37:56 +01:00
|
|
|
server = &http.Server{
|
2022-07-29 02:07:53 +02:00
|
|
|
Addr: fmt.Sprintf("%s:%d", cfg.Web.Address, cfg.Web.Port),
|
2020-12-30 02:22:17 +01:00
|
|
|
Handler: router,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
IdleTimeout: 15 * time.Second,
|
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
log.Println("[controller][Handle] Listening on " + cfg.Web.SocketAddress())
|
2021-02-01 07:37:56 +01:00
|
|
|
if os.Getenv("ROUTER_TEST") == "true" {
|
|
|
|
return
|
|
|
|
}
|
2021-02-06 02:45:28 +01:00
|
|
|
log.Println("[controller][Handle]", server.ListenAndServe())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the server
|
|
|
|
func Shutdown() {
|
|
|
|
if server != nil {
|
|
|
|
_ = server.Shutdown(context.TODO())
|
|
|
|
server = nil
|
|
|
|
}
|
2020-12-30 02:22:17 +01:00
|
|
|
}
|