Skip to content

Commit

Permalink
fix: set swagger to produce relative urls for examples, autoselect ht…
Browse files Browse the repository at this point in the history
…tp 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 <[email protected]>

* correctly autoselect http/s

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* correctly autoselect http/s

Signed-off-by: JD <[email protected]>

* have status use status.html instead of index.html template

Signed-off-by: JD <[email protected]>

* fix typo in if statement.

Signed-off-by: JD <[email protected]>

---------

Signed-off-by: JD <[email protected]>
  • Loading branch information
5u6r054 authored Aug 2, 2024
1 parent a0a1e1d commit b212ff7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/api/handlers_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (api *API) NodeStatusPageHandler() gin.HandlerFunc {
}
}

c.HTML(http.StatusOK, "index.html", templateData)
c.HTML(http.StatusOK, "status.html", templateData)
}
}

Expand Down
54 changes: 40 additions & 14 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [email protected]
// @license.name MIT
// @license.url https://opensource.org/license/mit
setupSwaggerHandler(router)

v1 := router.Group("/api/v1")
{
Expand Down Expand Up @@ -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)
}
File renamed without changes.
64 changes: 64 additions & 0 deletions pkg/api/templates/swagger.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<style>
html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
*, *:before, *:after { box-sizing: inherit; }
body { margin:0; background: #fafafa; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
url: "doc.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
onComplete: function() {
const currentScheme = window.location.protocol.slice(0, -1);

function setScheme() {
const schemeSelect = document.querySelector('.scheme-container select');
if (schemeSelect && schemeSelect.value !== currentScheme) {
schemeSelect.value = currentScheme;
const event = new Event('change', { bubbles: true, cancelable: true });
schemeSelect.dispatchEvent(event);
}
}

// 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) {
setScheme();
}
});
});

// Start observing the document with the configured parameters
observer.observe(document.body, { childList: true, subtree: true });
}
});
}
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion pkg/workers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b212ff7

Please sign in to comment.