Files
sablier/pkg/strategy/dynamic_strategy.go
Alexis Couvreur b4f5eebaac feat: add blocking request (#12)
* feat: add blocking strategy

* docs: add examples for blocking strategy

* ci: run go tests recursively

* perf: wait for BlockCheckInterval for each request

* fix: use camel case instead of snake case for yaml config

* docs: add loading and error page customization as a feature

* fix: use errorPage

* fix: return json instead of html page

* set json key

* update development config

* docs: add comment about custom loading pages

* ci: add beta release
2021-12-11 15:46:56 +01:00

47 lines
1.1 KiB
Go

package strategy
import (
"log"
"net/http"
"time"
"github.com/acouvreur/traefik-ondemand-plugin/pkg/pages"
)
type DynamicStrategy struct {
Request string
Name string
Next http.Handler
Timeout time.Duration
LoadingPage string
ErrorPage string
}
// ServeHTTP retrieve the service status
func (e *DynamicStrategy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
log.Printf("Sending request: %s", e.Request)
status, err := getServiceStatus(e.Request)
log.Printf("Status: %s", status)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(pages.GetErrorPage(e.ErrorPage, e.Name, err.Error())))
}
if status == "started" {
// Service started forward request
e.Next.ServeHTTP(rw, req)
} else if status == "starting" {
// Service starting, notify client
rw.WriteHeader(http.StatusAccepted)
rw.Write([]byte(pages.GetLoadingPage(e.LoadingPage, e.Name, e.Timeout)))
} else {
// Error
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(pages.GetErrorPage(e.ErrorPage, e.Name, status)))
}
}