|
| 1 | +package main |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2014 The Kubernetes Authors. |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +// A simple server that is alive for 10 seconds, then reports unhealthy for |
| 17 | +// the rest of its (hopefully) short existence. |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "log" |
| 22 | + "net/http" |
| 23 | + "net/url" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + started := time.Now() |
| 29 | + http.HandleFunc("/started", func(w http.ResponseWriter, r *http.Request) { |
| 30 | + w.WriteHeader(200) |
| 31 | + data := (time.Since(started)).String() |
| 32 | + w.Write([]byte(data)) |
| 33 | + }) |
| 34 | + http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { |
| 35 | + duration := time.Since(started) |
| 36 | + if duration.Seconds() < 30 || duration.Seconds() > 60 { |
| 37 | + w.WriteHeader(500) |
| 38 | + w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds()))) |
| 39 | + } else { |
| 40 | + w.WriteHeader(200) |
| 41 | + w.Write([]byte(fmt.Sprintf("ok: %v", duration.Seconds()))) |
| 42 | + } |
| 43 | + }) |
| 44 | + http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { |
| 45 | + loc, err := url.QueryUnescape(r.URL.Query().Get("loc")) |
| 46 | + if err != nil { |
| 47 | + http.Error(w, fmt.Sprintf("invalid redirect: %q", r.URL.Query().Get("loc")), http.StatusBadRequest) |
| 48 | + return |
| 49 | + } |
| 50 | + http.Redirect(w, r, loc, http.StatusFound) |
| 51 | + }) |
| 52 | + log.Fatal(http.ListenAndServe(":8080", nil)) |
| 53 | +} |
0 commit comments