From 5d2fa487d2f81e6eee42b10fe37b239e7a1b5209 Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Wed, 2 Apr 2025 18:52:39 -0400 Subject: [PATCH 1/7] resolve CORS access issues with endpoints --- internal/endpoints/restapi/restapi.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index a6d92bb..c5dbe18 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -15,8 +15,28 @@ import ( // https://medium.com/better-programming/building-a-simple-rest-api-in-go-with-gorilla-mux-892ceb128c6f // send text +func enableCORS(router *mux.Router) { + router.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Set CORS headers + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + // If it's a preflight OPTIONS request, exit here. + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusOK) + return + } + next.ServeHTTP(w, r) + }) + }) +} + func StartRestAPI() { router := mux.NewRouter() + // Enable CORS for the router + enableCORS(router) + router.HandleFunc("/config/{name}", getYamlHandler).Methods("GET") router.HandleFunc("/config/{name}", putYamlHandler).Methods("PUT") router.HandleFunc("/database/list", useListAllFilenames).Methods("PUT") From 082e6a60c1cc8372f6f406a8ca445e32f33c6f45 Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Fri, 4 Apr 2025 14:09:50 -0400 Subject: [PATCH 2/7] add endpoint for keyword --- internal/endpoints/restapi/restapi.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index c5dbe18..8e12dd9 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -37,6 +37,8 @@ func StartRestAPI() { // Enable CORS for the router enableCORS(router) + // New endpoint for keyword + router.HandleFunc("/config/keyword", getKeywordHandler).Methods("GET", "OPTIONS") router.HandleFunc("/config/{name}", getYamlHandler).Methods("GET") router.HandleFunc("/config/{name}", putYamlHandler).Methods("PUT") router.HandleFunc("/database/list", useListAllFilenames).Methods("PUT") From cc8327420db487aeeafb146ff267308c068b08e1 Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Fri, 4 Apr 2025 14:10:02 -0400 Subject: [PATCH 3/7] add import for config --- internal/endpoints/restapi/restapi.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index 8e12dd9..ca13c78 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -7,6 +7,7 @@ import ( "os" "strings" + "github.com/METIL-HoloAI/HoloTable-Middleware/internal/config" "github.com/METIL-HoloAI/HoloTable-Middleware/internal/database" "github.com/gorilla/mux" ) From c6ab0fed0d50cfe20476fc1682ee2156ccfc3937 Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Fri, 4 Apr 2025 14:10:17 -0400 Subject: [PATCH 4/7] update routes to be catch-all --- internal/endpoints/restapi/restapi.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index ca13c78..d1f4510 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -40,9 +40,12 @@ func StartRestAPI() { // New endpoint for keyword router.HandleFunc("/config/keyword", getKeywordHandler).Methods("GET", "OPTIONS") - router.HandleFunc("/config/{name}", getYamlHandler).Methods("GET") - router.HandleFunc("/config/{name}", putYamlHandler).Methods("PUT") - router.HandleFunc("/database/list", useListAllFilenames).Methods("PUT") + + // ({name: .*} essentially is a "catch-all route" meaning it will catch the rest of the route after "/config" + // This ensures that config files in lower directories can still be fetched, i.e. "/config/contentgen_yamls" is all captured + router.HandleFunc("/config/{name:.*}", getYamlHandler).Methods("GET", "OPTIONS") + router.HandleFunc("/config/{name:.*}", putYamlHandler).Methods("PUT", "OPTIONS") + router.HandleFunc("/database/list", useListAllFilenames).Methods("PUT", "OPTIONS") // start serv log.Fatal(http.ListenAndServe(":8000", router)) From b3b0cee4aa9636582a89b732f66544c9cc564eef Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Fri, 4 Apr 2025 14:10:32 -0400 Subject: [PATCH 5/7] add func for fetching keyword --- internal/endpoints/restapi/restapi.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index d1f4510..c0bceeb 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -51,6 +51,16 @@ func StartRestAPI() { log.Fatal(http.ListenAndServe(":8000", router)) } +func getKeywordHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + // Return the keyword from the SpeechToText configuration + _, err := w.Write([]byte(config.SpeechToText.Keyword)) + if err != nil { + log.Fatal("Failed to write response:", err) + } +} + func getYamlHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) yamlName := vars["name"] From 1261f5b6e4f6f2455ea3675daea53fe993bbe777 Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Fri, 4 Apr 2025 16:27:11 -0400 Subject: [PATCH 6/7] fix getYamlHandler for subdirectories --- internal/endpoints/restapi/restapi.go | 37 +++++++++++++++++++-------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index c0bceeb..b3ff84e 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -64,23 +64,38 @@ func getKeywordHandler(w http.ResponseWriter, r *http.Request) { func getYamlHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) yamlName := vars["name"] - yamlPath := "../../config/" + yamlName + ".yaml" - data, err := os.ReadFile(yamlPath) - if err != nil { - yamlPath = "../../config/contentgen/" + yamlName + ".yaml" - data2, err := os.ReadFile(yamlPath) - if err != nil { - http.Error(w, "Failed to read file", http.StatusInternalServerError) - return + var searchPaths []string + if strings.Contains(yamlName, "/") { + // If the client included a directory in the path, + // assume they provided the relative path from the base config folder. + searchPaths = []string{ + "config/" + yamlName + ".yaml", + } + } else { + // Otherwise, try the base config and common subdirectories. + searchPaths = []string{ + "config/" + yamlName + ".yaml", + "config/contentgen_yamls/" + yamlName + ".yaml", + "config/contentgen_workflows/" + yamlName + ".yaml", } - data = data2 } - dataString := string(data) + var data []byte + var err error + for _, path := range searchPaths { + data, err = os.ReadFile(path) + if err == nil { + break + } + } + if err != nil { + http.Error(w, "Failed to read file", http.StatusNotFound) + return + } w.Header().Set("Content-Type", "text/plain") - _, err = w.Write([]byte(dataString)) + _, err = w.Write(data) if err != nil { log.Fatal("Failed to write response:", err) } From 195bef9a2e453283b9ab859925d427ce36c768a9 Mon Sep 17 00:00:00 2001 From: Pharit Smitasin Date: Fri, 4 Apr 2025 16:33:45 -0400 Subject: [PATCH 7/7] fix putYamlHandler for yamlPaths --- internal/endpoints/restapi/restapi.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/endpoints/restapi/restapi.go b/internal/endpoints/restapi/restapi.go index b3ff84e..5b4a4a1 100644 --- a/internal/endpoints/restapi/restapi.go +++ b/internal/endpoints/restapi/restapi.go @@ -105,8 +105,9 @@ func putYamlHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) yamlName := vars["name"] yamlPaths := []string{ - "../../config/" + yamlName + ".yaml", - "../../config/contentgen/" + yamlName + ".yaml", + "config/" + yamlName + ".yaml", + "config/contentgen_yamls/" + yamlName + ".yaml", + "config/contentgen_workflows/" + yamlName + ".yaml", } body, err := io.ReadAll(r.Body)