diff --git a/go.mod b/go.mod index e5c650d..1524fb0 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.supernets.org/perp/gopay go 1.22.3 require ( + github.com/MarceloPetrucio/go-scalar-api-reference v0.0.0-20240521013641-ce5d2efe0e06 github.com/gin-gonic/gin v1.10.0 github.com/pelletier/go-toml/v2 v2.2.2 github.com/rs/zerolog v1.33.0 diff --git a/go.sum b/go.sum index 4dc6288..1669af5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/MarceloPetrucio/go-scalar-api-reference v0.0.0-20240521013641-ce5d2efe0e06 h1:W4Yar1SUsPmmA51qoIRb174uDO/Xt3C48MB1YX9Y3vM= +github.com/MarceloPetrucio/go-scalar-api-reference v0.0.0-20240521013641-ce5d2efe0e06/go.mod h1:/wotfjM8I3m8NuIHPz3S8k+CCYH80EqDT8ZeNLqMQm0= github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= diff --git a/internal/router/api/v1/spec.go b/internal/router/api/v1/spec.go new file mode 100644 index 0000000..87c45b9 --- /dev/null +++ b/internal/router/api/v1/spec.go @@ -0,0 +1,26 @@ +package v1 + +import ( + "git.supernets.org/perp/gopay/internal/context" + "github.com/MarceloPetrucio/go-scalar-api-reference" + "github.com/rs/zerolog/log" +) + +// Handle spec reference +func Spec(ctx *context.Context) { + // Parse documentation + content, err := scalar.ApiReferenceHTML(&scalar.Options{ + SpecURL: "docs/v1_swagger.json", + CustomOptions: scalar.CustomOptions{ + PageTitle: "GoPay API documentation", + }, + HideDownloadButton: true, + DarkMode: true, + }) + if err != nil { + log.Err(err).Msg("Could not parse swagger with scalar") + return + } + + ctx.Data(200, "text/html; charset=utf-8", []byte(content)) +} diff --git a/internal/router/api/v1/v1.go b/internal/router/api/v1/v1.go index 6db3059..d674092 100644 --- a/internal/router/api/v1/v1.go +++ b/internal/router/api/v1/v1.go @@ -14,4 +14,7 @@ import "git.supernets.org/perp/gopay/internal/context" // Register v1 routes func Register(ctx *context.Context) { + v1 := ctx.Group("v1") + v1.GET("scalar", ctx.API(Spec)) + v1.GET("swagger", ctx.API(Spec)) }