Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
feat: add optional display name config property
Browse files Browse the repository at this point in the history
Allows you to overwrite the loading display name which defaults to the middleware name
  • Loading branch information
acouvreur committed Jul 23, 2022
1 parent b05d300 commit c57d93b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ testData:
| `names` | `[]string` | [] | yes (except if `name` is set) | `[TRAEFIK_HACKATHON_whoami-1, TRAEFIK_HACKATHON_whoami-2]` | The containers/services to be stopped (docker ps docker service ls) |
| `timeout` | `time.Duration` | `1m` | no | `1m30s` | The duration after which the container/service will be scaled down to 0 |
| `waitui` | `bool` | `true` | no | `true` | Serves a self-refreshing html page when the service is scaled down to 0 |
| `displayname` | `string` | `the middleware name` | no | `My App` | Serves a self-refreshing html page when the service is scaled down to 0 |
| `blockdelay` | `time.Duration` | `1m` | no | `1m30s` | When `waitui` is `false`, wait for the service to be scaled up before `blockdelay` |
| `loadingpage` | `string` | empty | no | `/etc/traefik/plugins/traefik-ondemand-plugin/custompages/loading.html` | The path in the traefik container for the **loading** page template |
| `errorpage` | `string` | empty | no | `/etc/traefik/plugins/traefik-ondemand-plugin/custompages/error.html` | The path in the traefik container for the **error** page template |
Expand Down
3 changes: 3 additions & 0 deletions ondemand.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
ErrorPage string `yaml:"errorpage"`
LoadingPage string `yaml:"loadingpage"`
WaitUi bool `yaml:"waitui"`
DisplayName string `yaml:"displayname"`
BlockDelay string `yaml:"blockdelay"`
}

Expand All @@ -27,6 +28,7 @@ func CreateConfig() *Config {
Timeout: "1m",
WaitUi: true,
BlockDelay: "1m",
DisplayName: "",
ErrorPage: "",
LoadingPage: "",
}
Expand Down Expand Up @@ -95,6 +97,7 @@ func (config *Config) getServeStrategy(requests []string, name string, next http
Name: name,
Next: next,
Timeout: timeout,
DisplayName: config.DisplayName,
ErrorPage: config.ErrorPage,
LoadingPage: config.LoadingPage,
}, nil
Expand Down
13 changes: 10 additions & 3 deletions pkg/strategy/dynamic_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ type DynamicStrategy struct {
Name string
Next http.Handler
Timeout time.Duration
DisplayName string
LoadingPage string
ErrorPage string
}

// ServeHTTP retrieve the service status
func (e *DynamicStrategy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
started := make([]bool, len(e.Requests))

displayName := e.Name
if len(e.DisplayName) > 0 {
displayName = e.DisplayName
}

notReadyCount := 0
for requestIndex, request := range e.Requests {
log.Printf("Sending request: %s", request)
Expand All @@ -28,7 +35,7 @@ func (e *DynamicStrategy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(pages.GetErrorPage(e.ErrorPage, e.Name, err.Error())))
rw.Write([]byte(pages.GetErrorPage(e.ErrorPage, displayName, err.Error())))
}

if status == "started" {
Expand All @@ -39,7 +46,7 @@ func (e *DynamicStrategy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} else {
// Error
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(pages.GetErrorPage(e.ErrorPage, e.Name, status)))
rw.Write([]byte(pages.GetErrorPage(e.ErrorPage, displayName, status)))
}
}
if notReadyCount == 0 {
Expand All @@ -48,6 +55,6 @@ func (e *DynamicStrategy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} else {
// Services still starting, notify client
rw.WriteHeader(http.StatusAccepted)
rw.Write([]byte(pages.GetLoadingPage(e.LoadingPage, e.Name, e.Timeout)))
rw.Write([]byte(pages.GetLoadingPage(e.LoadingPage, displayName, e.Timeout)))
}
}

0 comments on commit c57d93b

Please sign in to comment.