feat: add the /reset endpoint, makes getenv generic, limit scope of l
This commit is contained in:
parent
9586d3536f
commit
ad300c7759
72
main.go
72
main.go
@ -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,38 +39,48 @@ 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
|
||||||
}
|
}
|
||||||
i, err := strconv.Atoi(v)
|
var r any
|
||||||
if err != nil {
|
switch any(d).(type) {
|
||||||
log.Fatalf("Invalid Value, %s not a valid integer: %v", k, err)
|
case string:
|
||||||
|
r = v
|
||||||
|
case int:
|
||||||
|
i, err := strconv.Atoi(v)
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
r = i
|
||||||
|
default:
|
||||||
|
log.Fatalf("Invalid Value, %s not a valid", k)
|
||||||
}
|
}
|
||||||
return i
|
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 {
|
||||||
r.ParseForm()
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
jm, err := json.Marshal(r.PostForm)
|
r.ParseForm()
|
||||||
if err != nil || !serve() {
|
jm, err := json.Marshal(r.PostForm)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
if err != nil || !serve(l) {
|
||||||
fmt.Fprintln(w, "why did you do that?")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintln(w, "why did you do that?")
|
||||||
|
C.Add()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%v", string(jm))
|
||||||
C.Add()
|
C.Add()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "%v", string(jm))
|
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user