From b212ff780715b2cccc7e4b3079d7326d346bd95b Mon Sep 17 00:00:00 2001 From: J2D3 <156010594+5u6r054@users.noreply.github.com> Date: Fri, 2 Aug 2024 13:38:32 -0700 Subject: [PATCH] fix: set swagger to produce relative urls for examples, autoselect http vs https (#472) * set swagger to produce relative urls for examples * update to config swagger * make swagger pick HTTP/S automatically * make it work at swagger/ swagger and swagger/index.html * 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> * correctly autoselect http/s * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * correctly autoselect http/s Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * have status use status.html instead of index.html template Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> * fix typo in if statement. Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --------- Signed-off-by: JD <156010594+5u6r054@users.noreply.github.com> --- pkg/api/handlers_node.go | 2 +- pkg/api/routes.go | 54 ++++++++++++---- pkg/api/templates/{index.html => status.html} | 0 pkg/api/templates/swagger.html | 64 +++++++++++++++++++ pkg/workers/handler.go | 2 +- 5 files changed, 106 insertions(+), 16 deletions(-) rename pkg/api/templates/{index.html => status.html} (100%) create mode 100644 pkg/api/templates/swagger.html 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/routes.go b/pkg/api/routes.go index 141b9eaa..9bcf4160 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -8,16 +8,19 @@ import ( "strings" "time" + "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v4" - swaggerFiles "github.com/swaggo/files" - ginSwagger "github.com/swaggo/gin-swagger" "github.com/masa-finance/masa-oracle/docs" "github.com/gin-contrib/cors" - "github.com/gin-gonic/gin" + + "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 ) //go:embed templates/*.html @@ -126,18 +129,12 @@ 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 - // @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 + setupSwaggerHandler(router) v1 := router.Group("/api/v1") { @@ -552,6 +549,35 @@ func SetupRoutes(node *masa.OracleNode) *gin.Engine { }) }) - router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.DefaultModelsExpandDepth(-1))) return router } + +func setupSwaggerHandler(router *gin.Engine) { + // 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" || c.Request.URL.Path == "/swagger/" || c.Request.URL.Path == "/swagger/index.html" { + c.File(swaggerTemplate) + return + } + + // 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) +} 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 diff --git a/pkg/api/templates/swagger.html b/pkg/api/templates/swagger.html new file mode 100644 index 00000000..55e08811 --- /dev/null +++ b/pkg/api/templates/swagger.html @@ -0,0 +1,64 @@ + + + + + Swagger UI + + + + + + +
+ + + + + \ No newline at end of file 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) {