2020-12-29 20:22:17 -05:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2021-02-05 20:45:28 -05:00
|
|
|
"context"
|
2020-12-29 20:22:17 -05:00
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2021-01-24 04:48:07 -05:00
|
|
|
"os"
|
2020-12-29 20:22:17 -05:00
|
|
|
"time"
|
|
|
|
|
|
2021-10-07 21:28:04 -04:00
|
|
|
"github.com/TwiN/gatus/v3/config/ui"
|
|
|
|
|
"github.com/TwiN/gatus/v3/config/web"
|
|
|
|
|
"github.com/TwiN/gatus/v3/controller/handler"
|
|
|
|
|
"github.com/TwiN/gatus/v3/security"
|
2020-12-29 20:22:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2021-02-01 01:37:56 -05:00
|
|
|
// server is the http.Server created by Handle.
|
|
|
|
|
// The only reason it exists is for testing purposes.
|
|
|
|
|
server *http.Server
|
2020-12-29 20:22:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Handle creates the router and starts the server
|
2021-09-22 00:47:51 -04:00
|
|
|
func Handle(securityConfig *security.Config, webConfig *web.Config, uiConfig *ui.Config, enableMetrics bool) {
|
|
|
|
|
var router http.Handler = handler.CreateRouter(ui.StaticFolder, securityConfig, uiConfig, enableMetrics)
|
2021-01-24 04:48:07 -05:00
|
|
|
if os.Getenv("ENVIRONMENT") == "dev" {
|
2021-09-12 18:39:09 -04:00
|
|
|
router = handler.DevelopmentCORS(router)
|
2021-01-24 04:48:07 -05:00
|
|
|
}
|
2021-02-01 01:37:56 -05:00
|
|
|
server = &http.Server{
|
2021-05-18 22:29:15 -04:00
|
|
|
Addr: fmt.Sprintf("%s:%d", webConfig.Address, webConfig.Port),
|
2020-12-29 20:22:17 -05:00
|
|
|
Handler: router,
|
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
|
IdleTimeout: 15 * time.Second,
|
|
|
|
|
}
|
2021-05-18 22:29:15 -04:00
|
|
|
log.Println("[controller][Handle] Listening on " + webConfig.SocketAddress())
|
2021-02-01 01:37:56 -05:00
|
|
|
if os.Getenv("ROUTER_TEST") == "true" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-02-05 20:45:28 -05:00
|
|
|
log.Println("[controller][Handle]", server.ListenAndServe())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shutdown stops the server
|
|
|
|
|
func Shutdown() {
|
|
|
|
|
if server != nil {
|
|
|
|
|
_ = server.Shutdown(context.TODO())
|
|
|
|
|
server = nil
|
|
|
|
|
}
|
2020-12-29 20:22:17 -05:00
|
|
|
}
|