Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"embed"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"

"regexp"
"runtime"

"strconv"
"strings"
"sync"
Expand All @@ -30,6 +35,23 @@ import (
"github.com/joho/godotenv"
)

//go:embed templates/swagger.html
var swaggerTemplateFS embed.FS

var swaggerTmpl = template.Must(
template.ParseFS(swaggerTemplateFS, "templates/swagger.html"),
)

var swaggerVersionRe = regexp.MustCompile(`^\d+\.\d+\.\d+$`)

func getSwaggerUIVersion() string {
v := os.Getenv("SWAGGER_UI_VERSION")
if swaggerVersionRe.MatchString(v) {
return v
}
return "5.11.0"
}

type PaymentContext struct {
Recipient string `json:"recipient"`
Token string `json:"token"`
Expand Down Expand Up @@ -123,26 +145,18 @@ func main() {
r.StaticFile("/openapi.yaml", "openapi.yaml")

r.GET("/docs", func(c *gin.Context) {
c.Header("Content-Type", "text/html")
c.String(200, `
<!DOCTYPE html>
<html>
<head>
<title>MicroAI Paygate Docs</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js"></script>
<script>
SwaggerUIBundle({
url: '/openapi.yaml',
dom_id: '#swagger-ui'
});
</script>
</body>
</html>
`)
data := struct {
Version string
}{
Version: getSwaggerUIVersion(),
}

c.Header("Content-Type", "text/html; charset=utf-8")

if err := swaggerTmpl.Execute(c.Writer, data); err != nil {
c.String(500, "failed to render swagger ui")
return
}
})

r.Use(cors.New(cors.Config{
Expand Down
18 changes: 18 additions & 0 deletions gateway/templates/swagger.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>MicroAI Paygate API Docs</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@{{.Version}}/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>

<script src="https://unpkg.com/swagger-ui-dist@{{.Version}}/swagger-ui-bundle.js"></script>
<script>
SwaggerUIBundle({
url: "/openapi.yaml",
dom_id: "#swagger-ui"
});
</script>
</body>
</html>
Loading