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 @@ + + + +
+ +