From dfa3fe1886cd178635259d8d5d39efb263337c8c Mon Sep 17 00:00:00 2001 From: "alexandre.hiltcher@etu.unice.fr" Date: Tue, 20 Oct 2020 22:05:34 +0200 Subject: [PATCH] INitial commit --- go.mod | 3 ++ main.go | 112 +++++++++++++++++++++++++++++++++++++++++++++ service/service.go | 75 ++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 go.mod create mode 100644 main.go create mode 100644 service/service.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5151824 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module githuc.com/acouvreur/traefik-ondemand-plugin + +go 1.15 diff --git a/main.go b/main.go new file mode 100644 index 0000000..5289f30 --- /dev/null +++ b/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "time" +) + +func handleRequests(w http.ResponseWriter, r *http.Request) { + + queryParams := r.URL.Query() + fmt.Printf("%+v", queryParams) + if queryParams["name"] == nil { + http.Error(w, "name is required", 400) + fmt.Fprintf(w, "%+v", "name is required") + } + if queryParams["timeout"] == nil { + http.Error(w, "timeout is required", 400) + fmt.Fprintf(w, "%+v", "name is required") + } + // 1. Check if service is up + // 2. + // IS DOWN + // 2.1 Start the service if down (async) + // 2.2 Set timeout + // IS UP + // 2.1 Reset timeout + // 3. Response + + service := GetOrCreateService("test", 10) + // service.HandleServiceState( + fmt.Fprintf(w, "%+v", service) + +} + +func main() { + http.HandleFunc("/", handleRequests) + log.Fatal(http.ListenAndServe(":10000", nil)) +} + +/// Other file + +// Status is the service status +type Status string + +const ( + UP Status = "up" + DOWN Status = "down" + UNKNOWN Status = "unknown" +) + +// Service holds all information related to a service +type Service struct { + name string + timeout uint64 + initialTimeout uint64 + status Status +} + +var services = map[string]*Service{} + +// GetOrCreateService return an existing service or create one +func GetOrCreateService(name string, timeout uint64) *Service { + if services[name] != nil { + return services[name] + } + service := &Service{name, timeout, timeout, UNKNOWN} + + services[name] = service + return service +} + +// HandleServiceState up the service if down or set timeout for downing the service +func (service *Service) HandleServiceState() { + if service.isUp() == true { + service.timeout = service.initialTimeout + go service.stopAfterTimeout() + + } else if service.isDown() { + service.start() + } else { + service.setServiceStateFromDocker() + service.HandleServiceState() + } +} + +func (service *Service) isUp() bool { + return service.status == UP +} + +func (service *Service) isDown() bool { + return service.status == DOWN +} + +func (service *Service) setServiceStateFromDocker() { + // set status form docker + status := UNKNOWN + service.status = status +} + +func (service *Service) start() { + // start service in docker + service.timeout = service.initialTimeout +} + +func (service *Service) stopAfterTimeout() { + for service.timeout > 0 { + time.Sleep(100 * time.Millisecond) + } + println("OVER MOTHER FUCKER") +} diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..2f98d8c --- /dev/null +++ b/service/service.go @@ -0,0 +1,75 @@ +package service + +import ( + "time" +) + +// Status is the service status +type Status string + +const ( + UP Status = "up" + DOWN Status = "down" + UNKNOWN Status = "unknown" +) + +// Service holds all information related to a service +type Service struct { + name string + timeout uint64 + initialTimeout uint64 + status Status +} + +var services = map[string]*Service{} + +// GetOrCreateService return an existing service or create one +func GetOrCreateService(name string, timeout uint64) *Service { + if services[name] != nil { + return services[name] + } + service := &Service{name, timeout, timeout, UNKNOWN} + + services[name] = service + return service +} + +// HandleServiceState up the service if down or set timeout for downing the service +func (service *Service) HandleServiceState() { + if service.isUp() == true { + service.timeout = service.initialTimeout + go service.stopAfterTimeout() + + } else if service.isDown() { + service.start() + } else { + service.setServiceStateFromDocker() + service.HandleServiceState() + } +} + +func (service *Service) isUp() bool { + return service.status == UP +} + +func (service *Service) isDown() bool { + return service.status == DOWN +} + +func (service *Service) setServiceStateFromDocker() { + // set status form docker + status := UNKNOWN + service.status = status +} + +func (service *Service) start() { + // start service in docker + service.timeout = service.initialTimeout +} + +func (service *Service) stopAfterTimeout() { + for service.timeout > 0 { + time.Sleep(100 * time.Millisecond) + } + println("OVER MOTHER FUCKER") +}