Skip to content

Commit

Permalink
Updated handler to allow for dynamic timeout response
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Jul 5, 2023
1 parent f783819 commit ae55c92
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package main
import (
"bytes"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/gorilla/mux"
)

var (
Expand All @@ -28,8 +27,18 @@ func main() {
r.HandleFunc("/opensearch", opensearchHandler)
r.HandleFunc("/", handleReq)
http.Handle("/", r)
timeoutHandler := http.TimeoutHandler(r, time.Second*3, "Incompatible driver and service")
log.Fatal(http.ListenAndServe(":3000", timeoutHandler))

log.Fatal(http.ListenAndServe(":3000", timeoutHandler(r)))
}

func timeoutHandler(m *mux.Router) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
driver := strings.ReplaceAll(r.URL.Path, "/", "")
service := r.URL.Query().Get("service")
incompatibleError := fmt.Sprintf("%s is not a compatible driver with service: %s", driver, service)
timeoutHandler := http.TimeoutHandler(m, 3*time.Second, incompatibleError)
timeoutHandler.ServeHTTP(w, r)
}
}

func handleReq(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -68,18 +77,6 @@ func cleanRoute(basePath string) (string, string) {
return localService, lagoonService
}

//func verifyDriverService(r *http.Request) (string, error) {
// driver := strings.ReplaceAll(r.URL.Path, "/", "")
// serviceVersion := r.URL.Query().Get("service")
// regexp := regexp.MustCompile(`(\w*)-`)
// service := regexp.FindStringSubmatch(serviceVersion)
// if driver != service[1] {
// incompatibleError := fmt.Sprintf("%s is not a compatible driver with service: %s", driver, service[1])
// return "", errors.New(incompatibleError)
// }
// return serviceVersion, nil
//}

// getEnv get key environment variable if exist otherwise return defalutValue
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
Expand Down

0 comments on commit ae55c92

Please sign in to comment.