Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Embed files into the binary #186

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/TwiN/gatus/v3/alerting/provider/telegram"
"github.com/TwiN/gatus/v3/alerting/provider/twilio"
"github.com/TwiN/gatus/v3/client"
"github.com/TwiN/gatus/v3/config/ui"
"github.com/TwiN/gatus/v3/config/web"
"github.com/TwiN/gatus/v3/core"
)
Expand All @@ -38,10 +37,6 @@ func TestLoadDefaultConfigurationFile(t *testing.T) {

func TestParseAndValidateConfigBytes(t *testing.T) {
file := t.TempDir() + "/test.db"
ui.StaticFolder = "../web/static"
defer func() {
ui.StaticFolder = "./web/static"
}()
config, err := parseAndValidateConfigBytes([]byte(fmt.Sprintf(`
storage:
file: %s
Expand Down
10 changes: 3 additions & 7 deletions config/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package ui
import (
"bytes"
"html/template"

"github.com/TwiN/gatus/v3/web"
)

const (
defaultTitle = "Health Dashboard | Gatus"
defaultLogo = ""
)

var (
// StaticFolder is the path to the location of the static folder from the root path of the project
// The only reason this is exposed is to allow running tests from a different path than the root path of the project
StaticFolder = "./web/static"
)

// Config is the configuration for the UI of Gatus
type Config struct {
Title string `yaml:"title"` // Title of the page
Expand All @@ -35,7 +31,7 @@ func (cfg *Config) ValidateAndSetDefaults() error {
if len(cfg.Title) == 0 {
cfg.Title = defaultTitle
}
t, err := template.ParseFiles(StaticFolder + "/index.html")
t, err := template.ParseFS(web.StaticFolder, "index.html")
if err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions config/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
)

func TestConfig_ValidateAndSetDefaults(t *testing.T) {
StaticFolder = "../../web/static"
defer func() {
StaticFolder = "./web/static"
}()
cfg := &Config{Title: ""}
if err := cfg.ValidateAndSetDefaults(); err != nil {
t.Error("expected no error, got", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (

// Handle creates the router and starts the server
func Handle(securityConfig *security.Config, webConfig *web.Config, uiConfig *ui.Config, enableMetrics bool) {
var router http.Handler = handler.CreateRouter(ui.StaticFolder, securityConfig, uiConfig, enableMetrics)
var router http.Handler = handler.CreateRouter(securityConfig, uiConfig, enableMetrics)
if os.Getenv("ENVIRONMENT") == "dev" {
router = handler.DevelopmentCORS(router)
}
Expand Down
2 changes: 1 addition & 1 deletion controller/handler/badge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestUptimeBadge(t *testing.T) {
}
watchdog.UpdateServiceStatuses(cfg.Services[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
watchdog.UpdateServiceStatuses(cfg.Services[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
router := CreateRouter("../../web/static", cfg.Security, nil, cfg.Metrics)
router := CreateRouter(cfg.Security, nil, cfg.Metrics)
type Scenario struct {
Name string
Path string
Expand Down
2 changes: 1 addition & 1 deletion controller/handler/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestResponseTimeChart(t *testing.T) {
}
watchdog.UpdateServiceStatuses(cfg.Services[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
watchdog.UpdateServiceStatuses(cfg.Services[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
router := CreateRouter("../../web/static", cfg.Security, nil, cfg.Metrics)
router := CreateRouter(cfg.Security, nil, cfg.Metrics)
type Scenario struct {
Name string
Path string
Expand Down
12 changes: 0 additions & 12 deletions controller/handler/favicon.go

This file was deleted.

33 changes: 0 additions & 33 deletions controller/handler/favicon_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions controller/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (

"github.com/TwiN/gatus/v3/config/ui"
"github.com/TwiN/gatus/v3/security"
"github.com/TwiN/gatus/v3/web"
"github.com/TwiN/health"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func CreateRouter(staticFolder string, securityConfig *security.Config, uiConfig *ui.Config, enabledMetrics bool) *mux.Router {
func CreateRouter(securityConfig *security.Config, uiConfig *ui.Config, enabledMetrics bool) *mux.Router {
router := mux.NewRouter()
if enabledMetrics {
router.Handle("/metrics", promhttp.Handler()).Methods("GET")
}
router.Handle("/health", health.Handler().WithJSON(true)).Methods("GET")
router.HandleFunc("/favicon.ico", FavIcon(staticFolder)).Methods("GET")
// Endpoints
router.HandleFunc("/api/v1/services/statuses", secureIfNecessary(securityConfig, ServiceStatuses)).Methods("GET") // No GzipHandler for this one, because we cache the content as Gzipped already
router.HandleFunc("/api/v1/services/{key}/statuses", secureIfNecessary(securityConfig, GzipHandlerFunc(ServiceStatus))).Methods("GET")
Expand All @@ -26,10 +26,10 @@ func CreateRouter(staticFolder string, securityConfig *security.Config, uiConfig
router.HandleFunc("/api/v1/services/{key}/response-times/{duration}/badge.svg", ResponseTimeBadge).Methods("GET")
router.HandleFunc("/api/v1/services/{key}/response-times/{duration}/chart.svg", ResponseTimeChart).Methods("GET")
// SPA
router.HandleFunc("/services/{service}", SinglePageApplication(staticFolder, uiConfig)).Methods("GET")
router.HandleFunc("/", SinglePageApplication(staticFolder, uiConfig)).Methods("GET")
router.HandleFunc("/services/{service}", SinglePageApplication(uiConfig)).Methods("GET")
router.HandleFunc("/", SinglePageApplication(uiConfig)).Methods("GET")
// Everything else falls back on static content
router.PathPrefix("/").Handler(GzipHandler(http.FileServer(http.Dir(staticFolder))))
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.FS(web.StaticFolder))))
return router
}

Expand Down
2 changes: 1 addition & 1 deletion controller/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestCreateRouter(t *testing.T) {
router := CreateRouter("../../web/static", nil, nil, true)
router := CreateRouter(nil, nil, true)
type Scenario struct {
Name string
Path string
Expand Down
4 changes: 2 additions & 2 deletions controller/handler/service_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestServiceStatus(t *testing.T) {
}
watchdog.UpdateServiceStatuses(cfg.Services[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
watchdog.UpdateServiceStatuses(cfg.Services[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
router := CreateRouter("../../web/static", cfg.Security, nil, cfg.Metrics)
router := CreateRouter(cfg.Security, nil, cfg.Metrics)

type Scenario struct {
Name string
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestServiceStatuses(t *testing.T) {
// Can't be bothered dealing with timezone issues on the worker that runs the automated tests
firstResult.Timestamp = time.Time{}
secondResult.Timestamp = time.Time{}
router := CreateRouter("../../web/static", nil, nil, false)
router := CreateRouter(nil, nil, false)

type Scenario struct {
Name string
Expand Down
7 changes: 3 additions & 4 deletions controller/handler/spa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import (
"net/http"

"github.com/TwiN/gatus/v3/config/ui"
"github.com/TwiN/gatus/v3/web"
)

func SinglePageApplication(staticFolder string, ui *ui.Config) http.HandlerFunc {
func SinglePageApplication(ui *ui.Config) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
t, err := template.ParseFiles(staticFolder + "/index.html")
t, err := template.ParseFS(web.StaticFolder, "index.html")
if err != nil {
log.Println("[handler][SinglePageApplication] Failed to parse template:", err.Error())
http.ServeFile(writer, request, staticFolder+"/index.html")
return
}
writer.Header().Set("Content-Type", "text/html")
err = t.Execute(writer, ui)
if err != nil {
log.Println("[handler][SinglePageApplication] Failed to parse template:", err.Error())
http.ServeFile(writer, request, staticFolder+"/index.html")
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion controller/handler/spa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestSinglePageApplication(t *testing.T) {
}
watchdog.UpdateServiceStatuses(cfg.Services[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
watchdog.UpdateServiceStatuses(cfg.Services[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
router := CreateRouter("../../web/static", cfg.Security, nil, cfg.Metrics)
router := CreateRouter(cfg.Security, nil, cfg.Metrics)
type Scenario struct {
Name string
Path string
Expand Down
10 changes: 10 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package web

import (
"embed"
"io/fs"
)

//go:embed static
var folder embed.FS
var StaticFolder, _ = fs.Sub(folder, "static")