From 1c4956eeeb85b743530c5842f35675ad570360b7 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 11:25:59 -0700 Subject: [PATCH 01/37] set swagger to produce relative urls for examples --- pkg/api/routes.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 141b9eaa..ca7f3fcd 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -552,6 +552,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { }) }) - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.DefaultModelsExpandDepth(-1))) + url := ginSwagger.URL("") // Use an empty string for the URL + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url, ginSwagger.DefaultModelsExpandDepth(-1))) return router } From 8ed940e523caec0e79a6b62aad70a0993a3972c1 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:34:04 -0700 Subject: [PATCH 02/37] update to config swagger --- pkg/api/routes.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index ca7f3fcd..587c5c43 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -126,6 +126,9 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { templ := template.Must(template.ParseFS(htmlTemplates, "templates/*.html")) router.SetHTMLTemplate(templ) + // Update Swagger info + docs.SwaggerInfo.Host = "" // Leave this empty for relative URLs + docs.SwaggerInfo.BasePath = "/api/v1" docs.SwaggerInfo.Schemes = []string{"http", "https"} // @BasePath /api/v1 @@ -552,7 +555,6 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { }) }) - url := ginSwagger.URL("") // Use an empty string for the URL - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url, ginSwagger.DefaultModelsExpandDepth(-1))) + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) return router } From c423371de661b2988cb1b27b9d4ab3fe432c97e9 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:45:38 -0700 Subject: [PATCH 03/37] make swagger pick HTTP/S automatically --- pkg/api/routes.go | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 587c5c43..856359a5 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -8,14 +8,17 @@ import ( "strings" "time" + "encoding/json" + + "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v4" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" + "github.com/swaggo/swag" "github.com/masa-finance/masa-oracle/docs" "github.com/gin-contrib/cors" - "github.com/gin-gonic/gin" masa "github.com/masa-finance/masa-oracle/pkg" ) @@ -129,7 +132,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { // Update Swagger info docs.SwaggerInfo.Host = "" // Leave this empty for relative URLs docs.SwaggerInfo.BasePath = "/api/v1" - docs.SwaggerInfo.Schemes = []string{"http", "https"} + docs.SwaggerInfo.Schemes = []string{"https", "http"} // Note the order: HTTPS first // @BasePath /api/v1 // @Title Masa API @@ -142,6 +145,8 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { // @license.name MIT // @license.url https://opensource.org/license/mit + setupSwaggerHandler(router) + v1 := router.Group("/api/v1") { @@ -555,6 +560,37 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { }) }) - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) return router } + +func setupSwaggerHandler(router *gin.Engine) { + url := ginSwagger.URL("swagger/doc.json") + router.GET("/swagger/*any", func(c *gin.Context) { + if c.Request.URL.Path == "/swagger/doc.json" { + doc, err := swag.ReadDoc() + if err != nil { + c.JSON(500, gin.H{"error": "Unable to read Swagger doc"}) + return + } + + var swaggerSpec map[string]interface{} + if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { + c.JSON(500, gin.H{"error": "Unable to parse Swagger doc"}) + return + } + + // Determine the scheme + scheme := "http" + if c.Request.TLS != nil || strings.HasPrefix(c.Request.Host, "test.api.masa.ai") { + scheme = "https" + } + + // Update the schemes in the Swagger spec + swaggerSpec["schemes"] = []string{scheme} + + c.JSON(200, swaggerSpec) + return + } + ginSwagger.WrapHandler(swaggerFiles.Handler, url)(c) + }) +} From eb09e4a97e3ea2a5e9d2da6b9dadc22c271b3181 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:50:35 -0700 Subject: [PATCH 04/37] make it work at swagger/ swagger and swagger/index.html --- pkg/api/routes.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 856359a5..322e928c 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -566,6 +566,11 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { func setupSwaggerHandler(router *gin.Engine) { url := ginSwagger.URL("swagger/doc.json") router.GET("/swagger/*any", func(c *gin.Context) { + if c.Request.URL.Path == "/swagger" || c.Request.URL.Path == "/swagger/" { + c.Redirect(http.StatusMovedPermanently, "/swagger/index.html") + return + } + if c.Request.URL.Path == "/swagger/doc.json" { doc, err := swag.ReadDoc() if err != nil { From 3001e7186e971efa683f1c9fa89f8b8b96aa53ef Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:57:18 -0700 Subject: [PATCH 05/37] make it work at swagger/ swagger and swagger/index.html but don't change URL in browser window from whichever manner it was accessed Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 322e928c..063143fa 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -565,12 +565,11 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { func setupSwaggerHandler(router *gin.Engine) { url := ginSwagger.URL("swagger/doc.json") + router.GET("/swagger", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) + }) router.GET("/swagger/*any", func(c *gin.Context) { - if c.Request.URL.Path == "/swagger" || c.Request.URL.Path == "/swagger/" { - c.Redirect(http.StatusMovedPermanently, "/swagger/index.html") - return - } - if c.Request.URL.Path == "/swagger/doc.json" { doc, err := swag.ReadDoc() if err != nil { @@ -586,7 +585,7 @@ func setupSwaggerHandler(router *gin.Engine) { // Determine the scheme scheme := "http" - if c.Request.TLS != nil || strings.HasPrefix(c.Request.Host, "test.api.masa.ai") { + if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { scheme = "https" } From feff85336f48ed6952f95144758ab3fb4fbc98fa Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:05:30 -0700 Subject: [PATCH 06/37] correctly autoselect http/s --- pkg/api/routes.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 063143fa..c926ea88 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -132,7 +132,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { // Update Swagger info docs.SwaggerInfo.Host = "" // Leave this empty for relative URLs docs.SwaggerInfo.BasePath = "/api/v1" - docs.SwaggerInfo.Schemes = []string{"https", "http"} // Note the order: HTTPS first + docs.SwaggerInfo.Schemes = []string{"http", "https"} // Include both schemes // @BasePath /api/v1 // @Title Masa API @@ -564,7 +564,6 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - url := ginSwagger.URL("swagger/doc.json") router.GET("/swagger", func(c *gin.Context) { c.Request.URL.Path = "/swagger/index.html" router.HandleContext(c) @@ -595,6 +594,6 @@ func setupSwaggerHandler(router *gin.Engine) { c.JSON(200, swaggerSpec) return } - ginSwagger.WrapHandler(swaggerFiles.Handler, url)(c) + ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("doc.json"))(c) }) } From 640b92c854f861a08fba412a6cec82a03848bcc5 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:10:33 -0700 Subject: [PATCH 07/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index c926ea88..1b51995b 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -132,18 +132,17 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { // Update Swagger info docs.SwaggerInfo.Host = "" // Leave this empty for relative URLs docs.SwaggerInfo.BasePath = "/api/v1" - docs.SwaggerInfo.Schemes = []string{"http", "https"} // Include both schemes - - // @BasePath /api/v1 - // @Title Masa API - // @Description The Worlds Personal Data Network Masa Oracle Node API - // @Host https://api.masa.ai - // @Version 0.5.0 - // @contact.name Masa API Support - // @contact.url https://masa.ai - // @contact.email support@masa.ai - // @license.name MIT - // @license.url https://opensource.org/license/mit + // Remove the Schemes setting from here, as we'll set it dynamically + + // Handle both /swagger and /swagger/ without redirecting + router.GET("/swagger", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) + }) + router.GET("/swagger/", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) + }) setupSwaggerHandler(router) @@ -572,13 +571,13 @@ func setupSwaggerHandler(router *gin.Engine) { if c.Request.URL.Path == "/swagger/doc.json" { doc, err := swag.ReadDoc() if err != nil { - c.JSON(500, gin.H{"error": "Unable to read Swagger doc"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) return } var swaggerSpec map[string]interface{} if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { - c.JSON(500, gin.H{"error": "Unable to parse Swagger doc"}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) return } @@ -591,7 +590,14 @@ func setupSwaggerHandler(router *gin.Engine) { // Update the schemes in the Swagger spec swaggerSpec["schemes"] = []string{scheme} - c.JSON(200, swaggerSpec) + // Ensure we're sending valid JSON + c.Header("Content-Type", "application/json") + jsonData, err := json.Marshal(swaggerSpec) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate Swagger JSON"}) + return + } + c.Data(http.StatusOK, "application/json", jsonData) return } ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("doc.json"))(c) From 106187e4a0ed6b321808d0110b83485fda32358e Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:13:51 -0700 Subject: [PATCH 08/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 1b51995b..fdd8d397 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -134,16 +134,6 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { docs.SwaggerInfo.BasePath = "/api/v1" // Remove the Schemes setting from here, as we'll set it dynamically - // Handle both /swagger and /swagger/ without redirecting - router.GET("/swagger", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) - }) - router.GET("/swagger/", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) - }) - setupSwaggerHandler(router) v1 := router.Group("/api/v1") @@ -563,7 +553,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - router.GET("/swagger", func(c *gin.Context) { + router.GET("/swagger", "/swagger/", func(c *gin.Context) { c.Request.URL.Path = "/swagger/index.html" router.HandleContext(c) }) From cbfcc5278354bcbc11185eb3fee0b13a17a696d5 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:16:27 -0700 Subject: [PATCH 09/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 81 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index fdd8d397..259c9600 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -553,43 +553,50 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - router.GET("/swagger", "/swagger/", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) - }) - router.GET("/swagger/*any", func(c *gin.Context) { - if c.Request.URL.Path == "/swagger/doc.json" { - doc, err := swag.ReadDoc() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) - return - } - - var swaggerSpec map[string]interface{} - if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) - return - } - - // Determine the scheme - scheme := "http" - if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { - scheme = "https" - } - - // Update the schemes in the Swagger spec - swaggerSpec["schemes"] = []string{scheme} - - // Ensure we're sending valid JSON - c.Header("Content-Type", "application/json") - jsonData, err := json.Marshal(swaggerSpec) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate Swagger JSON"}) + swaggerGroup := router.Group("/swagger") + { + swaggerGroup.GET("", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) + }) + swaggerGroup.GET("/", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) + }) + swaggerGroup.GET("/*any", func(c *gin.Context) { + if c.Request.URL.Path == "/swagger/doc.json" { + doc, err := swag.ReadDoc() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) + return + } + + var swaggerSpec map[string]interface{} + if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) + return + } + + // Determine the scheme + scheme := "http" + if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { + scheme = "https" + } + + // Update the schemes in the Swagger spec + swaggerSpec["schemes"] = []string{scheme} + + // Ensure we're sending valid JSON + c.Header("Content-Type", "application/json") + jsonData, err := json.Marshal(swaggerSpec) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate Swagger JSON"}) + return + } + c.Data(http.StatusOK, "application/json", jsonData) return } - c.Data(http.StatusOK, "application/json", jsonData) - return - } - ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("doc.json"))(c) - }) + ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("doc.json"))(c) + }) + } } From 7334d563f22722059a23ba20847de80382285bc7 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:18:37 -0700 Subject: [PATCH 10/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 91 ++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 259c9600..fbc3df2b 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -553,50 +553,51 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - swaggerGroup := router.Group("/swagger") - { - swaggerGroup.GET("", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) - }) - swaggerGroup.GET("/", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) - }) - swaggerGroup.GET("/*any", func(c *gin.Context) { - if c.Request.URL.Path == "/swagger/doc.json" { - doc, err := swag.ReadDoc() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) - return - } - - var swaggerSpec map[string]interface{} - if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) - return - } - - // Determine the scheme - scheme := "http" - if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { - scheme = "https" - } - - // Update the schemes in the Swagger spec - swaggerSpec["schemes"] = []string{scheme} - - // Ensure we're sending valid JSON - c.Header("Content-Type", "application/json") - jsonData, err := json.Marshal(swaggerSpec) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate Swagger JSON"}) - return - } - c.Data(http.StatusOK, "application/json", jsonData) - return - } - ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("doc.json"))(c) - }) + // Handle /swagger and /swagger/ + router.GET("/swagger", swaggerIndexHandler) + router.GET("/swagger/", swaggerIndexHandler) + + // Handle /swagger/doc.json + router.GET("/swagger/doc.json", swaggerJSONHandler) + + // Handle all other /swagger/* routes + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) +} + +func swaggerIndexHandler(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + c.Request.URL.RawPath = "/swagger/index.html" + ginSwagger.WrapHandler(swaggerFiles.Handler)(c) +} + +func swaggerJSONHandler(c *gin.Context) { + doc, err := swag.ReadDoc() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) + return + } + + var swaggerSpec map[string]interface{} + if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) + return + } + + // Determine the scheme + scheme := "http" + if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { + scheme = "https" + } + + // Update the schemes in the Swagger spec + swaggerSpec["schemes"] = []string{scheme} + + // Ensure we're sending valid JSON + c.Header("Content-Type", "application/json") + jsonData, err := json.Marshal(swaggerSpec) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate Swagger JSON"}) + return } + c.Data(http.StatusOK, "application/json", jsonData) } From 9a8477a5b5badf3e1d2bca6c1ec22b59a1f86edc Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:22:00 -0700 Subject: [PATCH 11/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index fbc3df2b..1ee407d2 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -553,24 +553,18 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - // Handle /swagger and /swagger/ - router.GET("/swagger", swaggerIndexHandler) - router.GET("/swagger/", swaggerIndexHandler) - - // Handle /swagger/doc.json - router.GET("/swagger/doc.json", swaggerJSONHandler) - - // Handle all other /swagger/* routes - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) -} - -func swaggerIndexHandler(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - c.Request.URL.RawPath = "/swagger/index.html" - ginSwagger.WrapHandler(swaggerFiles.Handler)(c) + router.GET("/swagger/*any", func(c *gin.Context) { + if c.Param("any") == "" || c.Param("any") == "/" { + c.Request.URL.Path = "/swagger/index.html" + } else if c.Param("any") == "/doc.json" { + handleSwaggerJSON(c) + return + } + ginSwagger.WrapHandler(swaggerFiles.Handler)(c) + }) } -func swaggerJSONHandler(c *gin.Context) { +func handleSwaggerJSON(c *gin.Context) { doc, err := swag.ReadDoc() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) From 83b32783a99dad6db4c4f2b95da667ba959a8fc7 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:27:06 -0700 Subject: [PATCH 12/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 63 +++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 1ee407d2..e8c2749c 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -553,45 +553,38 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - router.GET("/swagger/*any", func(c *gin.Context) { - if c.Param("any") == "" || c.Param("any") == "/" { - c.Request.URL.Path = "/swagger/index.html" - } else if c.Param("any") == "/doc.json" { - handleSwaggerJSON(c) - return - } - ginSwagger.WrapHandler(swaggerFiles.Handler)(c) + router.GET("/swagger", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) }) -} + router.GET("/swagger/", func(c *gin.Context) { + c.Request.URL.Path = "/swagger/index.html" + router.HandleContext(c) + }) + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) -func handleSwaggerJSON(c *gin.Context) { - doc, err := swag.ReadDoc() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) - return - } + router.GET("/swagger/doc.json", func(c *gin.Context) { + doc, err := swag.ReadDoc() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) + return + } - var swaggerSpec map[string]interface{} - if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) - return - } + var swaggerSpec map[string]interface{} + if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) + return + } - // Determine the scheme - scheme := "http" - if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { - scheme = "https" - } + // Determine the scheme + scheme := "http" + if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { + scheme = "https" + } - // Update the schemes in the Swagger spec - swaggerSpec["schemes"] = []string{scheme} + // Update the schemes in the Swagger spec + swaggerSpec["schemes"] = []string{scheme} - // Ensure we're sending valid JSON - c.Header("Content-Type", "application/json") - jsonData, err := json.Marshal(swaggerSpec) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate Swagger JSON"}) - return - } - c.Data(http.StatusOK, "application/json", jsonData) + c.JSON(http.StatusOK, swaggerSpec) + }) } From e2a00d3f128a2c64e3354d8485b10027503941ae Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:30:55 -0700 Subject: [PATCH 13/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index e8c2749c..e356939b 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -561,8 +561,6 @@ func setupSwaggerHandler(router *gin.Engine) { c.Request.URL.Path = "/swagger/index.html" router.HandleContext(c) }) - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) - router.GET("/swagger/doc.json", func(c *gin.Context) { doc, err := swag.ReadDoc() if err != nil { @@ -587,4 +585,5 @@ func setupSwaggerHandler(router *gin.Engine) { c.JSON(http.StatusOK, swaggerSpec) }) + router.GET("/swagger/*filepath", ginSwagger.WrapHandler(swaggerFiles.Handler)) } From 8d5da53219593b51fb8dcae624c04e6b80d5cfa7 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:35:06 -0700 Subject: [PATCH 14/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index e356939b..685f00a5 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -553,6 +553,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { + // Specific routes first router.GET("/swagger", func(c *gin.Context) { c.Request.URL.Path = "/swagger/index.html" router.HandleContext(c) @@ -585,5 +586,7 @@ func setupSwaggerHandler(router *gin.Engine) { c.JSON(http.StatusOK, swaggerSpec) }) - router.GET("/swagger/*filepath", ginSwagger.WrapHandler(swaggerFiles.Handler)) + + // Catchall route last + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) } From 9a35dc718a50cf7e1e4e47e080eac2452e5f0eff Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:38:07 -0700 Subject: [PATCH 15/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 685f00a5..15d4d90d 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -12,8 +12,6 @@ import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v4" - swaggerFiles "github.com/swaggo/files" - ginSwagger "github.com/swaggo/gin-swagger" "github.com/swaggo/swag" "github.com/masa-finance/masa-oracle/docs" @@ -553,7 +551,6 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - // Specific routes first router.GET("/swagger", func(c *gin.Context) { c.Request.URL.Path = "/swagger/index.html" router.HandleContext(c) @@ -586,7 +583,4 @@ func setupSwaggerHandler(router *gin.Engine) { c.JSON(http.StatusOK, swaggerSpec) }) - - // Catchall route last - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) } From e467e34347244836e2967383df016e79cd7d3869 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:40:18 -0700 Subject: [PATCH 16/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 15d4d90d..fe18338b 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -583,4 +583,11 @@ func setupSwaggerHandler(router *gin.Engine) { c.JSON(http.StatusOK, swaggerSpec) }) + + // Add specific routes for Swagger UI files + router.GET("/swagger/swagger-ui.css", ginSwagger.WrapHandler(swaggerFiles.Handler)) + router.GET("/swagger/swagger-ui-bundle.js", ginSwagger.WrapHandler(swaggerFiles.Handler)) + router.GET("/swagger/swagger-ui-standalone-preset.js", ginSwagger.WrapHandler(swaggerFiles.Handler)) + router.GET("/swagger/favicon-32x32.png", ginSwagger.WrapHandler(swaggerFiles.Handler)) + router.GET("/swagger/index.html", ginSwagger.WrapHandler(swaggerFiles.Handler)) } From 0a97014070da29564ac8081d0cda6c82b279e501 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:42:44 -0700 Subject: [PATCH 17/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index fe18338b..f4c43e8a 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -19,6 +19,8 @@ import ( "github.com/gin-contrib/cors" masa "github.com/masa-finance/masa-oracle/pkg" + swaggerFiles "github.com/swaggo/files" // swagger embed files + ginSwagger "github.com/swaggo/gin-swagger" // ginSwagger middleware ) //go:embed templates/*.html From 0fe15ab2e6930c0843a493bcf171cf6c66b316ad Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:50:11 -0700 Subject: [PATCH 18/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index f4c43e8a..fd88434e 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -553,14 +553,16 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { + // Serve swagger index router.GET("/swagger", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) + c.Redirect(http.StatusMovedPermanently, "/swagger/") }) router.GET("/swagger/", func(c *gin.Context) { c.Request.URL.Path = "/swagger/index.html" - router.HandleContext(c) + ginSwagger.WrapHandler(swaggerFiles.Handler)(c) }) + + // Serve swagger JSON router.GET("/swagger/doc.json", func(c *gin.Context) { doc, err := swag.ReadDoc() if err != nil { @@ -586,10 +588,6 @@ func setupSwaggerHandler(router *gin.Engine) { c.JSON(http.StatusOK, swaggerSpec) }) - // Add specific routes for Swagger UI files - router.GET("/swagger/swagger-ui.css", ginSwagger.WrapHandler(swaggerFiles.Handler)) - router.GET("/swagger/swagger-ui-bundle.js", ginSwagger.WrapHandler(swaggerFiles.Handler)) - router.GET("/swagger/swagger-ui-standalone-preset.js", ginSwagger.WrapHandler(swaggerFiles.Handler)) - router.GET("/swagger/favicon-32x32.png", ginSwagger.WrapHandler(swaggerFiles.Handler)) - router.GET("/swagger/index.html", ginSwagger.WrapHandler(swaggerFiles.Handler)) + // Serve Swagger UI files + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) } From 58448822476c6e79c6e9388060fcc0f3da76fc07 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:55:32 -0700 Subject: [PATCH 19/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 45 ++++----------------------------------------- 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index fd88434e..10a8ff97 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -8,11 +8,8 @@ import ( "strings" "time" - "encoding/json" - "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v4" - "github.com/swaggo/swag" "github.com/masa-finance/masa-oracle/docs" @@ -132,7 +129,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { // Update Swagger info docs.SwaggerInfo.Host = "" // Leave this empty for relative URLs docs.SwaggerInfo.BasePath = "/api/v1" - // Remove the Schemes setting from here, as we'll set it dynamically + docs.SwaggerInfo.Schemes = []string{"http", "https"} setupSwaggerHandler(router) @@ -553,41 +550,7 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - // Serve swagger index - router.GET("/swagger", func(c *gin.Context) { - c.Redirect(http.StatusMovedPermanently, "/swagger/") - }) - router.GET("/swagger/", func(c *gin.Context) { - c.Request.URL.Path = "/swagger/index.html" - ginSwagger.WrapHandler(swaggerFiles.Handler)(c) - }) - - // Serve swagger JSON - router.GET("/swagger/doc.json", func(c *gin.Context) { - doc, err := swag.ReadDoc() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to read Swagger doc"}) - return - } - - var swaggerSpec map[string]interface{} - if err := json.Unmarshal([]byte(doc), &swaggerSpec); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to parse Swagger doc"}) - return - } - - // Determine the scheme - scheme := "http" - if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" { - scheme = "https" - } - - // Update the schemes in the Swagger spec - swaggerSpec["schemes"] = []string{scheme} - - c.JSON(http.StatusOK, swaggerSpec) - }) - - // Serve Swagger UI files - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + // Serve swagger documentation + url := ginSwagger.URL("/swagger/doc.json") // The url pointing to API definition + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) } From b8e0c1aa349ebe4df2481c02680816494a91ca87 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:18:44 -0700 Subject: [PATCH 20/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 23 +++++++++++++-- pkg/api/templates/swagger.html | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 pkg/api/templates/swagger.html diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 10a8ff97..8a899606 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -15,6 +15,9 @@ import ( "github.com/gin-contrib/cors" + "path/filepath" + "runtime" + masa "github.com/masa-finance/masa-oracle/pkg" swaggerFiles "github.com/swaggo/files" // swagger embed files ginSwagger "github.com/swaggo/gin-swagger" // ginSwagger middleware @@ -550,7 +553,21 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { } func setupSwaggerHandler(router *gin.Engine) { - // Serve swagger documentation - url := ginSwagger.URL("/swagger/doc.json") // The url pointing to API definition - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) + // Get the current file's directory + _, currentFile, _, _ := runtime.Caller(0) + currentDir := filepath.Dir(currentFile) + + // Construct the path to the swagger.html file + swaggerTemplate := filepath.Join(currentDir, "templates", "swagger.html") + + // Create a custom handler that serves our HTML file + customHandler := func(c *gin.Context) { + if c.Request.URL.Path == "/swagger/index.html" { + c.File(swaggerTemplate) + return + } + swaggerFiles.Handler.ServeHTTP(c.Writer, c.Request) + } + + router.GET("/swagger/*any", ginSwagger.WrapHandler(http.HandlerFunc(customHandler))) } diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html new file mode 100644 index 00000000..4628f645 --- /dev/null +++ b/pkg/api/templates/swagger.html @@ -0,0 +1,53 @@ + + + + + + Swagger UI + + + + + + +
+ + + + + From 75e4c71033274a79eb57e3c61c3fb6f4b874a31d Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:20:54 -0700 Subject: [PATCH 21/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 8a899606..4054e222 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -566,8 +566,8 @@ func setupSwaggerHandler(router *gin.Engine) { c.File(swaggerTemplate) return } - swaggerFiles.Handler.ServeHTTP(c.Writer, c.Request) + ginSwagger.WrapHandler(swaggerFiles.Handler)(c) } - router.GET("/swagger/*any", ginSwagger.WrapHandler(http.HandlerFunc(customHandler))) + router.GET("/swagger/*any", customHandler) } From 8be02ea785d83baa6fa799abc9443e13afa4f124 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:23:50 -0700 Subject: [PATCH 22/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/routes.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 4054e222..9bcf4160 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -562,12 +562,22 @@ func setupSwaggerHandler(router *gin.Engine) { // Create a custom handler that serves our HTML file customHandler := func(c *gin.Context) { - if c.Request.URL.Path == "/swagger/index.html" { + if c.Request.URL.Path == "/swagger" || c.Request.URL.Path == "/swagger/" || c.Request.URL.Path == "/swagger/index.html" { c.File(swaggerTemplate) return } - ginSwagger.WrapHandler(swaggerFiles.Handler)(c) + + // For other swagger-related paths, use the default handler + if strings.HasPrefix(c.Request.URL.Path, "/swagger/") { + ginSwagger.WrapHandler(swaggerFiles.Handler)(c) + return + } + + // If it's not a swagger path, pass it to the next handler + c.Next() } + // Use our custom handler for all /swagger paths + router.GET("/swagger", customHandler) router.GET("/swagger/*any", customHandler) } From 556f30827b9a5ef8eaa3200046342b5c6915b3b5 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:37:10 -0700 Subject: [PATCH 23/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 39 ++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 4628f645..06cb3ae4 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -41,13 +41,48 @@ for (let i = 0; i < schemeSelect.options.length; i++) { if (schemeSelect.options[i].value === currentScheme) { schemeSelect.selectedIndex = i; - schemeSelect.dispatchEvent(new Event('change')); + + // Trigger change event + const event = new Event('change'); + schemeSelect.dispatchEvent(event); + + // Force Swagger UI to update all URLs + if (ui && ui.specActions && ui.specActions.changeScheme) { + ui.specActions.changeScheme(currentScheme); + } + break; } } } + + // Update all operation links + const updateOperationUrls = function() { + const operationUrlInputs = document.querySelectorAll('.opblock-summary-control .opblock-summary-path a'); + operationUrlInputs.forEach(function(input) { + const url = new URL(input.href); + url.protocol = window.location.protocol; + input.href = url.toString(); + }); + }; + + // Initial update + updateOperationUrls(); + + // Set up a MutationObserver to watch for changes in the Swagger UI + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { + updateOperationUrls(); + } + }); + }); + + // Start observing the document with the configured parameters + observer.observe(document.body, { childList: true, subtree: true }); + }, 1000); // Wait for Swagger UI to fully load } - + \ No newline at end of file From 31d7a6a4d092a92eb21bacdad4f76f42cca4204f Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:45:56 -0700 Subject: [PATCH 24/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 72 +++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 06cb3ae4..ef5ab1f9 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -33,55 +33,65 @@ layout: "StandaloneLayout" }); - // Custom script to set the correct scheme - setTimeout(function() { - const currentScheme = window.location.protocol.slice(0, -1); + const currentScheme = window.location.protocol.slice(0, -1); + + function updateScheme() { const schemeSelect = document.querySelector('.scheme-container select'); if (schemeSelect) { for (let i = 0; i < schemeSelect.options.length; i++) { if (schemeSelect.options[i].value === currentScheme) { schemeSelect.selectedIndex = i; - - // Trigger change event - const event = new Event('change'); - schemeSelect.dispatchEvent(event); - - // Force Swagger UI to update all URLs - if (ui && ui.specActions && ui.specActions.changeScheme) { - ui.specActions.changeScheme(currentScheme); - } - break; } } } - // Update all operation links - const updateOperationUrls = function() { - const operationUrlInputs = document.querySelectorAll('.opblock-summary-control .opblock-summary-path a'); - operationUrlInputs.forEach(function(input) { + if (ui && ui.specActions && ui.specActions.changeScheme) { + ui.specActions.changeScheme(currentScheme); + } + + updateOperationUrls(); + } + + function updateOperationUrls() { + const operationUrlInputs = document.querySelectorAll('.opblock-summary-control .opblock-summary-path a, .try-out__btn'); + operationUrlInputs.forEach(function(input) { + if (input.href) { const url = new URL(input.href); url.protocol = window.location.protocol; input.href = url.toString(); - }); - }; + } + }); + } - // Initial update - updateOperationUrls(); + // Initial update + setTimeout(updateScheme, 100); - // Set up a MutationObserver to watch for changes in the Swagger UI - const observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { - updateOperationUrls(); - } - }); + // Set up a MutationObserver to watch for changes in the Swagger UI + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { + updateScheme(); + } }); + }); + + // Start observing the document with the configured parameters + observer.observe(document.body, { childList: true, subtree: true }); - // Start observing the document with the configured parameters - observer.observe(document.body, { childList: true, subtree: true }); + // Override the changeScheme function + const originalChangeScheme = ui.specActions.changeScheme; + ui.specActions.changeScheme = function(scheme) { + originalChangeScheme(scheme); + setTimeout(updateOperationUrls, 0); + }; - }, 1000); // Wait for Swagger UI to fully load + // Add event listener for scheme changes + document.addEventListener('change', function(event) { + if (event.target.matches('.scheme-container select')) { + setTimeout(updateOperationUrls, 0); + } + }); } From 39b5963cec99eb01ad7eb49509911d0f3fde69e2 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:50:53 -0700 Subject: [PATCH 25/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index ef5ab1f9..7076e4b1 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -62,6 +62,14 @@ input.href = url.toString(); } }); + + // Update generated examples + const exampleBlocks = document.querySelectorAll('.highlight-code'); + exampleBlocks.forEach(function(block) { + const code = block.textContent; + const updatedCode = code.replace(/('|")http:\/\//g, '$1' + window.location.protocol + '//'); + block.textContent = updatedCode; + }); } // Initial update @@ -92,6 +100,15 @@ setTimeout(updateOperationUrls, 0); } }); + + // Override the requestSnippetGenerator_curl function + if (ui.fn && ui.fn.requestSnippetGenerator_curl) { + const originalCurlGenerator = ui.fn.requestSnippetGenerator_curl; + ui.fn.requestSnippetGenerator_curl = function(...args) { + let snippet = originalCurlGenerator.apply(this, args); + return snippet.replace(/('|")http:\/\//g, '$1' + window.location.protocol + '//'); + }; + } } From 69731085364050d70200dfc382cd2b971b65c107 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:58:18 -0700 Subject: [PATCH 26/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 56 ++++++---------------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 7076e4b1..568bd0d0 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -35,7 +35,7 @@ const currentScheme = window.location.protocol.slice(0, -1); - function updateScheme() { + function updateUrls() { const schemeSelect = document.querySelector('.scheme-container select'); if (schemeSelect) { for (let i = 0; i < schemeSelect.options.length; i++) { @@ -46,69 +46,33 @@ } } - if (ui && ui.specActions && ui.specActions.changeScheme) { - ui.specActions.changeScheme(currentScheme); - } - - updateOperationUrls(); - } - - function updateOperationUrls() { - const operationUrlInputs = document.querySelectorAll('.opblock-summary-control .opblock-summary-path a, .try-out__btn'); - operationUrlInputs.forEach(function(input) { - if (input.href) { - const url = new URL(input.href); - url.protocol = window.location.protocol; - input.href = url.toString(); + const allUrls = document.querySelectorAll('a[href^="http://"], a[href^="https://"], .highlight-code'); + allUrls.forEach(function(element) { + if (element.href) { + element.href = element.href.replace(/^https?:/, window.location.protocol); + } else if (element.textContent) { + element.textContent = element.textContent.replace(/(https?:\/\/)/g, window.location.protocol + '//'); } }); - - // Update generated examples - const exampleBlocks = document.querySelectorAll('.highlight-code'); - exampleBlocks.forEach(function(block) { - const code = block.textContent; - const updatedCode = code.replace(/('|")http:\/\//g, '$1' + window.location.protocol + '//'); - block.textContent = updatedCode; - }); } // Initial update - setTimeout(updateScheme, 100); + setTimeout(updateUrls, 100); // Set up a MutationObserver to watch for changes in the Swagger UI const observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { - updateScheme(); - } - }); + updateUrls(); }); // Start observing the document with the configured parameters observer.observe(document.body, { childList: true, subtree: true }); - // Override the changeScheme function - const originalChangeScheme = ui.specActions.changeScheme; - ui.specActions.changeScheme = function(scheme) { - originalChangeScheme(scheme); - setTimeout(updateOperationUrls, 0); - }; - // Add event listener for scheme changes document.addEventListener('change', function(event) { if (event.target.matches('.scheme-container select')) { - setTimeout(updateOperationUrls, 0); + setTimeout(updateUrls, 0); } }); - - // Override the requestSnippetGenerator_curl function - if (ui.fn && ui.fn.requestSnippetGenerator_curl) { - const originalCurlGenerator = ui.fn.requestSnippetGenerator_curl; - ui.fn.requestSnippetGenerator_curl = function(...args) { - let snippet = originalCurlGenerator.apply(this, args); - return snippet.replace(/('|")http:\/\//g, '$1' + window.location.protocol + '//'); - }; - } } From 5a8486340434ba58f16bb6276572ab887e3dcca2 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:05:27 -0700 Subject: [PATCH 27/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 37 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 568bd0d0..2527cd57 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -41,6 +41,7 @@ for (let i = 0; i < schemeSelect.options.length; i++) { if (schemeSelect.options[i].value === currentScheme) { schemeSelect.selectedIndex = i; + schemeSelect.dispatchEvent(new Event('change')); break; } } @@ -56,22 +57,34 @@ }); } - // Initial update - setTimeout(updateUrls, 100); + function waitForSwaggerUIReady(callback) { + if (document.querySelector('.scheme-container')) { + callback(); + } else { + setTimeout(function() { + waitForSwaggerUIReady(callback); + }, 100); + } + } - // Set up a MutationObserver to watch for changes in the Swagger UI - const observer = new MutationObserver(function(mutations) { + // Initial update + waitForSwaggerUIReady(function() { updateUrls(); - }); + + // Set up a MutationObserver to watch for changes in the Swagger UI + const observer = new MutationObserver(function(mutations) { + updateUrls(); + }); - // Start observing the document with the configured parameters - observer.observe(document.body, { childList: true, subtree: true }); + // Start observing the document with the configured parameters + observer.observe(document.body, { childList: true, subtree: true }); - // Add event listener for scheme changes - document.addEventListener('change', function(event) { - if (event.target.matches('.scheme-container select')) { - setTimeout(updateUrls, 0); - } + // Add event listener for scheme changes + document.addEventListener('change', function(event) { + if (event.target.matches('.scheme-container select')) { + setTimeout(updateUrls, 0); + } + }); }); } From 0fd8949974f5c4f5403f4b81cae29eff1ed748db Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:14:22 -0700 Subject: [PATCH 28/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 2527cd57..b706f62a 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -19,6 +19,23 @@ From 2f48773b34bbe3bee3c9e0c682f28673c2af0666 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:00:13 -0700 Subject: [PATCH 30/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 42 +++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 4a8c2356..826074a4 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -34,31 +34,31 @@ ], layout: "StandaloneLayout", onComplete: function() { - // Set the correct scheme - ui.specActions.changeScheme(currentScheme); + // Function to update the scheme + function updateScheme() { + const schemeSelect = document.querySelector('.scheme-container select'); + if (schemeSelect) { + schemeSelect.value = currentScheme; + schemeSelect.dispatchEvent(new Event('change')); + } + } - // Force an update of the spec - ui.specActions.updateJsonSpec(ui.specSelectors.specJson().toJS()); - } - }); + // Initial update + updateScheme(); - // Intercept changes to the scheme select - const schemeContainer = document.querySelector('.scheme-container'); - if (schemeContainer) { - const observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - if (mutation.type === 'childList') { - const select = schemeContainer.querySelector('select'); - if (select) { - select.value = currentScheme; - select.dispatchEvent(new Event('change')); - observer.disconnect(); + // Set up a MutationObserver to watch for changes in the Swagger UI + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { + updateScheme(); } - } + }); }); - }); - observer.observe(schemeContainer, { childList: true, subtree: true }); - } + + // Start observing the document with the configured parameters + observer.observe(document.body, { childList: true, subtree: true }); + } + }); } From 3da4e43cc013b2ebd239ad84761878d1b3ae82f1 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:04:46 -0700 Subject: [PATCH 31/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 826074a4..8fc7c451 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -1,4 +1,3 @@ - @@ -39,6 +38,7 @@ const schemeSelect = document.querySelector('.scheme-container select'); if (schemeSelect) { schemeSelect.value = currentScheme; + // Trigger Swagger UI's internal scheme change mechanism schemeSelect.dispatchEvent(new Event('change')); } } @@ -57,6 +57,13 @@ // Start observing the document with the configured parameters observer.observe(document.body, { childList: true, subtree: true }); + + // Override Swagger UI's setScheme function to ensure it always uses the current scheme + const originalSetScheme = ui.specActions.setScheme; + ui.specActions.setScheme = function(scheme) { + originalSetScheme(currentScheme); + updateScheme(); + }; } }); } From a2a330097915bbd5d455502d666349e1cf72a9c0 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:08:08 -0700 Subject: [PATCH 32/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 41 +++++++++------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 8fc7c451..5c629343 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -33,39 +33,18 @@ ], layout: "StandaloneLayout", onComplete: function() { - // Function to update the scheme - function updateScheme() { - const schemeSelect = document.querySelector('.scheme-container select'); - if (schemeSelect) { - schemeSelect.value = currentScheme; - // Trigger Swagger UI's internal scheme change mechanism - schemeSelect.dispatchEvent(new Event('change')); - } - } - - // Initial update - updateScheme(); - - // Set up a MutationObserver to watch for changes in the Swagger UI - const observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { - updateScheme(); - } - }); - }); - - // Start observing the document with the configured parameters - observer.observe(document.body, { childList: true, subtree: true }); - - // Override Swagger UI's setScheme function to ensure it always uses the current scheme - const originalSetScheme = ui.specActions.setScheme; - ui.specActions.setScheme = function(scheme) { - originalSetScheme(currentScheme); - updateScheme(); - }; + // Force the scheme to match the current protocol + ui.specActions.changeScheme(currentScheme); } }); + + // Override the execute request function + const originalExecuteRequest = ui.specActions.executeRequest; + ui.specActions.executeRequest = function(req) { + // Ensure the URL uses the correct scheme + req.url = req.url.replace(/^(https?:)/, currentScheme + ':'); + return originalExecuteRequest(req); + }; } From c17797acf38f68c2162556cd28356f3a790271a1 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:11:56 -0700 Subject: [PATCH 33/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 5c629343..27a9634a 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -18,8 +18,6 @@ From ef7de1e752ea47a94714bdde5b6c630006f34f63 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:15:10 -0700 Subject: [PATCH 34/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index 27a9634a..eebb7ded 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -31,12 +31,29 @@ ], layout: "StandaloneLayout", onComplete: function() { - const currentScheme = window.location.protocol.slice(0, -1); - const schemeSelect = document.querySelector('.scheme-container select'); - if (schemeSelect) { - schemeSelect.value = currentScheme; - schemeSelect.dispatchEvent(new Event('change')); - } + setTimeout(function() { + const currentScheme = window.location.protocol.slice(0, -1); + const schemeSelect = document.querySelector('.scheme-container select'); + if (schemeSelect) { + schemeSelect.value = currentScheme; + schemeSelect.dispatchEvent(new Event('change')); + } + }, 100); // Small delay to ensure UI is fully rendered + + // Add this after the setTimeout in the onComplete function + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { + const schemeSelect = document.querySelector('.scheme-container select'); + if (schemeSelect && schemeSelect.value !== currentScheme) { + schemeSelect.value = currentScheme; + schemeSelect.dispatchEvent(new Event('change')); + } + } + }); + }); + + observer.observe(document.body, { childList: true, subtree: true }); } }); } From 959dbdbb16d12fb1ba6fb96216dc41e2f5b6e55d Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:19:57 -0700 Subject: [PATCH 35/37] correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/templates/swagger.html | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html index eebb7ded..55e08811 100644 --- a/pkg/api/templates/swagger.html +++ b/pkg/api/templates/swagger.html @@ -31,28 +31,30 @@ ], layout: "StandaloneLayout", onComplete: function() { - setTimeout(function() { - const currentScheme = window.location.protocol.slice(0, -1); + const currentScheme = window.location.protocol.slice(0, -1); + + function setScheme() { const schemeSelect = document.querySelector('.scheme-container select'); - if (schemeSelect) { + if (schemeSelect && schemeSelect.value !== currentScheme) { schemeSelect.value = currentScheme; - schemeSelect.dispatchEvent(new Event('change')); + const event = new Event('change', { bubbles: true, cancelable: true }); + schemeSelect.dispatchEvent(event); } - }, 100); // Small delay to ensure UI is fully rendered + } - // Add this after the setTimeout in the onComplete function + // Initial set + setScheme(); + + // Set up a MutationObserver to watch for changes in the Swagger UI const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { - const schemeSelect = document.querySelector('.scheme-container select'); - if (schemeSelect && schemeSelect.value !== currentScheme) { - schemeSelect.value = currentScheme; - schemeSelect.dispatchEvent(new Event('change')); - } + setScheme(); } }); }); + // Start observing the document with the configured parameters observer.observe(document.body, { childList: true, subtree: true }); } }); From c4719eea7d345c815995a797ba8581b4e5f70fcb Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:38:16 -0700 Subject: [PATCH 36/37] have status use status.html instead of index.html template Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/handlers_node.go | 2 +- pkg/api/templates/{index.html => status.html} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename pkg/api/templates/{index.html => status.html} (100%) diff --git a/pkg/api/handlers_node.go b/pkg/api/handlers_node.go index 0d91b312..3f3dfed4 100644 --- a/pkg/api/handlers_node.go +++ b/pkg/api/handlers_node.go @@ -424,7 +424,7 @@ func (api *API) NodeStatusPageHandler() gin.HandlerFunc { } } - c.HTML(http.StatusOK, "index.html", templateData) + c.HTML(http.StatusOK, "status.html", templateData) } } diff --git a/pkg/api/templates/index.html b/pkg/api/templates/status.html similarity index 100% rename from pkg/api/templates/index.html rename to pkg/api/templates/status.html From 42aa90eac8c46c554abe365a90df759c6ffa7e14 Mon Sep 17 00:00:00 2001 From: JD <156010594+5u6r054@users.noreply.github.com> Date: Fri, 2 Aug 2024 13:35:19 -0700 Subject: [PATCH 37/37] fix typo in if statement. Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/workers/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/workers/handler.go b/pkg/workers/handler.go index 3c208c91..f641c891 100644 --- a/pkg/workers/handler.go +++ b/pkg/workers/handler.go @@ -187,7 +187,7 @@ func (a *Worker) HandleWork(ctx actor.Context, m *messages.Work, node *masa.Orac return } cfg := config.GetInstance() - if cfg.TwitterScraper || cfg.DiscordScraper || cfg.TwitterScraper || cfg.WebScraper { + if cfg.TwitterScraper || cfg.DiscordScraper || cfg.TelegramScraper || cfg.WebScraper { ctx.Respond(&messages.Response{RequestId: workData["request_id"], Value: string(jsn)}) } for _, pid := range getPeers(node) {