feat: add the /reset endpoint, makes getenv generic, limit scope of l

This commit is contained in:
Tom Andrade 2023-01-20 15:33:29 +01:00
parent 9586d3536f
commit ad300c7759
Signed by: wolvie
GPG Key ID: 31AAB07872E82669

54
main.go
View File

@ -13,10 +13,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
var ( var C counter
l int
C counter
)
type counter struct { type counter struct {
mu sync.Mutex mu sync.Mutex
@ -42,26 +39,35 @@ func (c *counter) Reset() {
c.mu.Unlock() c.mu.Unlock()
} }
func getenv(k string, d int) int { func getenv[D ~string | int](k string, d D) D {
v := os.Getenv(k) v := os.Getenv(k)
if len(v) == 0 { if len(v) == 0 {
return d return d
} }
var r any
switch any(d).(type) {
case string:
r = v
case int:
i, err := strconv.Atoi(v) i, err := strconv.Atoi(v)
if err != nil { if err != nil {
log.Fatalf("Invalid Value, %s not a valid integer: %v", k, err)
} }
return i r = i
default:
log.Fatalf("Invalid Value, %s not a valid", k)
}
return r.(D)
} }
func serve() bool { func serve(l int) bool {
return C.Get() < l return C.Get() < l
} }
func handler(w http.ResponseWriter, r *http.Request) { func handler(l int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
jm, err := json.Marshal(r.PostForm) jm, err := json.Marshal(r.PostForm)
if err != nil || !serve() { if err != nil || !serve(l) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "why did you do that?") fmt.Fprintln(w, "why did you do that?")
C.Add() C.Add()
@ -69,11 +75,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
} }
fmt.Fprintf(w, "%v", string(jm)) fmt.Fprintf(w, "%v", string(jm))
C.Add() C.Add()
}
} }
func httpHealth() http.HandlerFunc { func httpHealth(l int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if !serve() { if !serve(l) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"status":"FAIL","requests":"%v"}`, C.Get()) fmt.Fprintf(w, `{"status":"FAIL","requests":"%v"}`, C.Get())
return return
@ -82,13 +89,30 @@ func httpHealth() http.HandlerFunc {
} }
} }
func reset(rt string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
t := r.FormValue("TOKEN")
if len(t) == 0 || t != rt {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Bad request, invalid token")
return
}
C.Reset()
fmt.Fprintf(w, `{"status":"ok","requests":"%v"}`, C.Get())
}
}
func main() { func main() {
l = getenv("MAX_REQUESTS", 500) l := getenv("MAX_REQUESTS", 500)
t := getenv("TOKEN", "token")
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", handler). r.HandleFunc("/", handler(l)).
Methods("POST") Methods("POST")
r.HandleFunc("/healthz", httpHealth()). r.HandleFunc("/healthz", httpHealth(l)).
Methods("GET") Methods("GET")
r.HandleFunc("/reset", reset(t)).
Methods("PUT")
logger := handlers.LoggingHandler(os.Stdout, r) logger := handlers.LoggingHandler(os.Stdout, r)
log.Fatal(http.ListenAndServe(":8080", logger)) log.Fatal(http.ListenAndServe(":8080", logger))
} }