-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.go
137 lines (115 loc) · 3.24 KB
/
manager.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
127
128
129
130
131
132
133
134
135
136
137
package traefik_container_manager
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const defaultTimeoutSeconds = 60 * 5 // 5 minutes
const defaultServiceUrl = "http://manager:10000/api" // Default URL is a container called manager serving on port 10000
var netClient = &http.Client{
Timeout: time.Second * 2,
}
// Config the plugin configuration
type Config struct {
Name string
ServiceUrl string
Timeout uint64
}
// CreateConfig creates a config with its default values
func CreateConfig() *Config {
return &Config{
ServiceUrl: defaultServiceUrl,
Timeout: defaultTimeoutSeconds,
}
}
// Manager holds the request for the container
type Manager struct {
name string
next http.Handler
request string
serviceUrl string
timeout uint64
}
func buildRequest(baseUrl string, name string, timeout uint64, host, path string) (string, error) {
// TODO: Check url validity
request := fmt.Sprintf("%s?name=%s&timeout=%d&host=%s&path=%s", baseUrl, name, timeout, url.QueryEscape(host), url.QueryEscape(path))
fmt.Println(timeout)
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.Name) == 0 {
return nil, fmt.Errorf("name cannot be null")
}
request, err := buildRequest(config.ServiceUrl, config.Name, config.Timeout, "", "")
if err != nil {
return nil, fmt.Errorf("error while building request")
}
return &Manager{
next: next,
name: config.Name,
request: request,
serviceUrl: config.ServiceUrl,
timeout: config.Timeout,
}, nil
}
// ServeHTTP retrieve the service status
func (e *Manager) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if e.name == "generic-container-manager" {
fmt.Printf("Request object: %+v", req)
host := req.Host
if host == "" {
host = req.URL.Host
}
path := req.URL.Path
if strings.Contains(path, ".") {
if s := strings.Split(path, "/"); len(s) > 2 {
path = strings.Join(s[:len(s)-1], "")
} else {
e.next.ServeHTTP(rw, req)
return
}
}
e.request, _ = buildRequest(e.serviceUrl, e.name, e.timeout, host, path)
fmt.Println("Request set to ", e.request)
}
starting := false
var status string
var err error
for status, err = getServiceStatus(e.request); err == nil && status == "starting"; status, err = getServiceStatus(e.request) {
starting = true
}
if starting {
time.Sleep(1 * time.Second)
http.Redirect(rw, req, req.URL.Path, http.StatusTemporaryRedirect)
}
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
}
if status == "started" {
// Service started forward request
e.next.ServeHTTP(rw, req)
} else {
// Error
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("Unexpected status answer from Manager service"))
}
}
func getServiceStatus(request string) (string, error) {
// This request starts up the service if it is stopped
resp, err := netClient.Get(request)
if err != nil {
return "error", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "parsing error", err
}
return strings.TrimSuffix(string(body), "\n"), nil
}