mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-25 09:24:04 +01:00
00b56ecefd
Fixes #47
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/TwiN/gatus/v4/config"
|
|
"github.com/TwiN/gatus/v4/controller/handler"
|
|
)
|
|
|
|
var (
|
|
// server is the http.Server created by Handle.
|
|
// The only reason it exists is for testing purposes.
|
|
server *http.Server
|
|
)
|
|
|
|
// Handle creates the router and starts the server
|
|
func Handle(cfg *config.Config) {
|
|
var router http.Handler = handler.CreateRouter(cfg)
|
|
if os.Getenv("ENVIRONMENT") == "dev" {
|
|
router = handler.DevelopmentCORS(router)
|
|
}
|
|
server = &http.Server{
|
|
Addr: fmt.Sprintf("%s:%d", cfg.Web.Address, cfg.Web.Port),
|
|
Handler: router,
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 15 * time.Second,
|
|
IdleTimeout: 15 * time.Second,
|
|
}
|
|
log.Println("[controller][Handle] Listening on " + cfg.Web.SocketAddress())
|
|
if os.Getenv("ROUTER_TEST") == "true" {
|
|
return
|
|
}
|
|
log.Println("[controller][Handle]", server.ListenAndServe())
|
|
}
|
|
|
|
// Shutdown stops the server
|
|
func Shutdown() {
|
|
if server != nil {
|
|
_ = server.Shutdown(context.TODO())
|
|
server = nil
|
|
}
|
|
}
|