This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ondemand.go
126 lines (105 loc) · 3.1 KB
/
ondemand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package traefik_ondemand_plugin
import (
"context"
"fmt"
"net/http"
"time"
"github.com/acouvreur/traefik-ondemand-plugin/pkg/strategy"
)
// Config the plugin configuration
type Config struct {
Name string `yaml:"name"`
Names []string `yaml:"names"`
ServiceUrl string `yaml:"serviceurl"`
Timeout string `yaml:"timeout"`
ErrorPage string `yaml:"errorpage"`
LoadingPage string `yaml:"loadingpage"`
WaitUi bool `yaml:"waitui"`
DisplayName string `yaml:"displayname"`
BlockDelay string `yaml:"blockdelay"`
}
// CreateConfig creates a config with its default values
func CreateConfig() *Config {
return &Config{
Timeout: "1m",
WaitUi: true,
BlockDelay: "1m",
DisplayName: "",
ErrorPage: "",
LoadingPage: "",
}
}
// Ondemand holds the request for the on demand service
type Ondemand struct {
strategy strategy.Strategy
}
func buildRequest(url string, name string, timeout time.Duration) (string, error) {
request := fmt.Sprintf("%s?name=%s&timeout=%s", url, name, timeout.String())
return request, nil
}
// New function creates the configuration
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.ServiceUrl) == 0 {
return nil, fmt.Errorf("serviceurl cannot be null")
}
if len(config.Name) != 0 && len(config.Names) != 0 {
return nil, fmt.Errorf("both name and names cannot be used simultaneously")
}
var serviceNames []string
if len(config.Name) != 0 {
serviceNames = append(serviceNames, config.Name)
} else if len(config.Names) != 0 {
serviceNames = config.Names
} else {
return nil, fmt.Errorf("both name and names cannot be null")
}
timeout, err := time.ParseDuration(config.Timeout)
if err != nil {
return nil, err
}
var requests []string
for _, serviceName := range serviceNames {
request, err := buildRequest(config.ServiceUrl, serviceName, timeout)
if err != nil {
return nil, fmt.Errorf("error while building request for %s", serviceName)
}
requests = append(requests, request)
}
strategy, err := config.getServeStrategy(requests, name, next, timeout)
if err != nil {
return nil, err
}
return &Ondemand{
strategy: strategy,
}, nil
}
func (config *Config) getServeStrategy(requests []string, name string, next http.Handler, timeout time.Duration) (strategy.Strategy, error) {
if config.WaitUi {
return &strategy.DynamicStrategy{
Requests: requests,
Name: name,
Next: next,
Timeout: timeout,
DisplayName: config.DisplayName,
ErrorPage: config.ErrorPage,
LoadingPage: config.LoadingPage,
}, nil
} else {
blockDelay, err := time.ParseDuration(config.BlockDelay)
if err != nil {
return nil, err
}
return &strategy.BlockingStrategy{
Requests: requests,
Name: name,
Next: next,
Timeout: timeout,
BlockDelay: blockDelay,
BlockCheckInterval: 1 * time.Second,
}, nil
}
}
// ServeHTTP retrieve the service status
func (e *Ondemand) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
e.strategy.ServeHTTP(rw, req)
}