-
Notifications
You must be signed in to change notification settings - Fork 153
[Feature] scalar alternative to swagger ui #1283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9a06f36
09b202b
063a95f
8658b33
211c73d
3bf792d
0e3bff6
5d916ed
4694c45
9616299
63b258d
4ecfb54
d6e6ab2
ecc461f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package scalar | ||
|
|
||
| import ( | ||
| "html/template" | ||
|
|
||
| "github.com/gofiber/fiber/v2" | ||
| ) | ||
|
|
||
| // Config defines the config for middleware. | ||
| type Config struct { | ||
| // Next defines a function to skip this middleware when returned true. | ||
| // | ||
| // Optional. Default: nil | ||
| Next func(c *fiber.Ctx) bool | ||
|
|
||
| // BasePath for the UI path | ||
| // | ||
| // Optional. Default: / | ||
| BasePath string | ||
|
|
||
| // FileContent for the content of the swagger.json or swagger.yaml file. | ||
| // | ||
| // Optional. Default: nil | ||
| FileContentString string | ||
|
|
||
| // Path combines with BasePath for the full UI path | ||
| // | ||
| // Optional. Default: docs | ||
| Path string | ||
|
|
||
| // Title for the documentation site | ||
| // | ||
| // Optional. Default: Fiber API documentation | ||
| Title string | ||
|
|
||
| // CacheAge defines the max-age for the Cache-Control header in seconds. | ||
| // | ||
| // Optional. Default: 1 min (no cache) | ||
| CacheAge int | ||
|
|
||
| // Custom Scalar Style | ||
| // Ref: https://github.com/scalar/scalar/blob/main/packages/themes/src/variables.css | ||
| // Optional. Default: "" | ||
| CustomStyle template.CSS | ||
|
|
||
| // Proxy to avoid CORS issues | ||
| // Optional. | ||
| ProxyUrl string | ||
|
|
||
| // Raw Space Url | ||
| // Optional. Default: doc.json | ||
| RawSpecUrl string | ||
|
|
||
| // ForceOffline | ||
| // Optional: Default: true | ||
| ForceOffline bool | ||
|
|
||
| // Fallback scalar cache | ||
| // | ||
| // Optional. Default: 86400 (1 Days) | ||
| FallbackCacheAge int | ||
| } | ||
|
|
||
| var configDefault = Config{ | ||
| Next: nil, | ||
| BasePath: "/", | ||
| Path: "docs", | ||
| Title: "Fiber API documentation", | ||
| CacheAge: 60, | ||
| RawSpecUrl: "doc.json", | ||
| ForceOffline: true, | ||
| FallbackCacheAge: 86400, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| module github.com/yokeTH/gofiber-scalar/scalar | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be github.com/gofiber/contrib/scalar |
||
|
|
||
| go 1.23.8 | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| require ( | ||
| github.com/gofiber/fiber/v2 v2.52.8 | ||
| github.com/stretchr/testify v1.10.0 | ||
| github.com/swaggo/swag v1.16.4 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/KyleBanks/depth v1.2.1 // indirect | ||
| github.com/PuerkitoBio/purell v1.1.1 // indirect | ||
| github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect | ||
| github.com/andybalholm/brotli v1.1.0 // indirect | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/go-openapi/jsonpointer v0.19.5 // indirect | ||
| github.com/go-openapi/jsonreference v0.19.6 // indirect | ||
| github.com/go-openapi/spec v0.20.4 // indirect | ||
| github.com/go-openapi/swag v0.19.15 // indirect | ||
| github.com/google/uuid v1.6.0 // indirect | ||
| github.com/josharian/intern v1.0.0 // indirect | ||
| github.com/klauspost/compress v1.17.9 // indirect | ||
| github.com/mailru/easyjson v0.7.6 // indirect | ||
| github.com/mattn/go-colorable v0.1.13 // indirect | ||
| github.com/mattn/go-isatty v0.0.20 // indirect | ||
| github.com/mattn/go-runewidth v0.0.16 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| github.com/rivo/uniseg v0.2.0 // indirect | ||
| github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
| github.com/valyala/fasthttp v1.51.0 // indirect | ||
| github.com/valyala/tcplisten v1.0.0 // indirect | ||
| golang.org/x/net v0.38.0 // indirect | ||
| golang.org/x/sys v0.31.0 // indirect | ||
| golang.org/x/text v0.23.0 // indirect | ||
| golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect | ||
| gopkg.in/yaml.v2 v2.4.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= | ||
| github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= | ||
| 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= | ||
| github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= | ||
| github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= | ||
| github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= | ||
| github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= | ||
| github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= | ||
| github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= | ||
| github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs= | ||
| github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= | ||
| github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= | ||
| github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= | ||
| github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= | ||
| github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM= | ||
| github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= | ||
| github.com/gofiber/fiber/v2 v2.52.8 h1:xl4jJQ0BV5EJTA2aWiKw/VddRpHrKeZLF0QPUxqn0x4= | ||
| github.com/gofiber/fiber/v2 v2.52.8/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= | ||
| github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
| github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
| github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= | ||
| github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= | ||
| github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= | ||
| github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= | ||
| github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
| github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
| github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
| github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
| github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= | ||
| github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= | ||
| github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= | ||
| github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= | ||
| github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= | ||
| github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
| github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
| github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
| github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
| github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
| github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= | ||
| github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
| github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= | ||
| github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= | ||
| github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
| github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
| github.com/swaggo/swag v1.16.4 h1:clWJtd9LStiG3VeijiCfOVODP6VpHtKdQy9ELFG3s1A= | ||
| github.com/swaggo/swag v1.16.4/go.mod h1:VBsHJRsDvfYvqoiMKnsdwhNV9LEMHgEDZcyVYX0sxPg= | ||
| github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
| github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
| github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= | ||
| github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= | ||
| github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= | ||
| github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= | ||
| golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= | ||
| golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
| golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= | ||
| golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8= | ||
| golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= | ||
| golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= | ||
| golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= | ||
| golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
| golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
| golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= | ||
| golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= | ||
| golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
| golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
| golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
| golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= | ||
| golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= | ||
| golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
| golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= | ||
| golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= | ||
| gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
| gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
| gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
| gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package scalar | ||
|
|
||
| const templateHTML = ` | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <title>{{.Title}}</title> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
|
|
||
| {{- if .CustomStyle }} | ||
| <style> | ||
| :root { | ||
| {{ .CustomStyle }} | ||
| } | ||
| </style> | ||
| {{ end }} | ||
| </head> | ||
|
|
||
| <body> | ||
| <div id="app"></div> | ||
| <script> | ||
| function loadFallback() { | ||
| var script = document.createElement('script'); | ||
| script.src = '{{ .Extra.FallbackUrl }}'; | ||
| script.onload = initScalar; | ||
| document.head.appendChild(script); | ||
| } | ||
|
|
||
| {{- if .ForceOffline }} | ||
| // Force offline mode | ||
| loadFallback(); | ||
| {{- else }} | ||
| if (!navigator.onLine) { | ||
| loadFallback(); | ||
| } else { | ||
| var cdn = document.createElement('script'); | ||
| cdn.src = 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'; | ||
| cdn.onload = initScalar; | ||
| cdn.onerror = loadFallback; | ||
| document.head.appendChild(cdn); | ||
| } | ||
| {{- end }} | ||
|
|
||
| function initScalar() { | ||
| if (typeof Scalar !== 'undefined') { | ||
| Scalar.createApiReference('#app', { | ||
| content: {{.FileContentString}}, | ||
| {{- if .ProxyUrl}} | ||
| proxyUrl: "{{.ProxyUrl}}", | ||
| {{ end }} | ||
| }); | ||
| } else { | ||
| console.error("Failed to load Scalar API Reference"); | ||
| document.querySelector('#app').innerHTML = "<p>Something went wrong. Please report this bug at <a href='https://github.com/yokeTH/gofiber-scalar/issues'>https://github.com/yokeTH/gofiber-scalar/issues</a></p>"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this element about reporting issues There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or change the URL to point to github.com/gofiber/contrib |
||
| } | ||
| } | ||
| </script> | ||
| </body> | ||
| </html>` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 1.30.0 |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,104 @@ | ||||||||||||
| package scalar | ||||||||||||
|
|
||||||||||||
| import ( | ||||||||||||
| _ "embed" | ||||||||||||
| "fmt" | ||||||||||||
| "path" | ||||||||||||
| "text/template" | ||||||||||||
|
|
||||||||||||
| "github.com/gofiber/fiber/v2" | ||||||||||||
| "github.com/swaggo/swag" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| //go:embed scalar.min.js | ||||||||||||
| var embeddedJS []byte | ||||||||||||
|
|
||||||||||||
| func New(config ...Config) fiber.Handler { | ||||||||||||
| // Set default config | ||||||||||||
| cfg := configDefault | ||||||||||||
|
|
||||||||||||
| // Override config if provided | ||||||||||||
| if len(config) > 0 { | ||||||||||||
| cfg = config[0] | ||||||||||||
|
|
||||||||||||
| // Set default values | ||||||||||||
| if len(cfg.BasePath) == 0 { | ||||||||||||
| cfg.BasePath = configDefault.BasePath | ||||||||||||
| } | ||||||||||||
| if len(cfg.Path) == 0 { | ||||||||||||
| cfg.Path = configDefault.Path | ||||||||||||
| } | ||||||||||||
| if len(cfg.Title) == 0 { | ||||||||||||
| cfg.Title = configDefault.Title | ||||||||||||
| } | ||||||||||||
| if len(cfg.RawSpecUrl) == 0 { | ||||||||||||
| cfg.RawSpecUrl = configDefault.RawSpecUrl | ||||||||||||
| } | ||||||||||||
| if !cfg.ForceOffline { | ||||||||||||
| cfg.ForceOffline = configDefault.ForceOffline | ||||||||||||
| } | ||||||||||||
| if cfg.FallbackCacheAge == 0 { | ||||||||||||
| cfg.FallbackCacheAge = configDefault.FallbackCacheAge | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| rawSpec := cfg.FileContentString | ||||||||||||
| if len(rawSpec) == 0 { | ||||||||||||
| doc, err := swag.ReadDoc() | ||||||||||||
| if err != nil { | ||||||||||||
| panic(err) | ||||||||||||
| } | ||||||||||||
| rawSpec = doc | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| cfg.FileContentString = string(rawSpec) | ||||||||||||
|
|
||||||||||||
| scalarUIPath := path.Join(cfg.BasePath, cfg.Path) | ||||||||||||
| specURL := path.Join(scalarUIPath, cfg.RawSpecUrl) | ||||||||||||
| jsFallbackPath := path.Join(scalarUIPath, "/js/api-reference.min.js") | ||||||||||||
|
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update path construction to use renamed fields. Update the path construction to use the renamed fields. scalarUIPath := path.Join(cfg.BasePath, cfg.Path)
-specURL := path.Join(scalarUIPath, cfg.RawSpecUrl)
+specURL := path.Join(scalarUIPath, cfg.RawSpecURL)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents (early access) |
||||||||||||
|
|
||||||||||||
| html, err := template.New("index.html").Parse(templateHTML) | ||||||||||||
| if err != nil { | ||||||||||||
| panic(fmt.Errorf("failed to parse html template:%v", err)) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| htmlData := struct { | ||||||||||||
| Config | ||||||||||||
| Extra map[string]any | ||||||||||||
| }{ | ||||||||||||
| Config: cfg, | ||||||||||||
| Extra: map[string]any{}, | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| htmlData.Extra["FallbackUrl"] = jsFallbackPath | ||||||||||||
|
|
||||||||||||
| return func(ctx *fiber.Ctx) error { | ||||||||||||
| if cfg.Next != nil && cfg.Next(ctx) { | ||||||||||||
| return ctx.Next() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // fallback js | ||||||||||||
| if ctx.Path() == jsFallbackPath { | ||||||||||||
| ctx.Set("Cache-Control", fmt.Sprintf("public, max-age=%d", cfg.FallbackCacheAge)) | ||||||||||||
| return ctx.Send(embeddedJS) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if cfg.CacheAge > 0 { | ||||||||||||
| ctx.Set("Cache-Control", fmt.Sprintf("public, max-age=%d", cfg.CacheAge)) | ||||||||||||
| } else { | ||||||||||||
| ctx.Set("Cache-Control", "no-store") | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ctx.Path() == specURL { | ||||||||||||
| ctx.Set("Content-Type", "application/json") | ||||||||||||
| return ctx.SendString(rawSpec) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ctx.Path() != scalarUIPath && ctx.Path() != specURL { | ||||||||||||
| return ctx.Next() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| ctx.Type("html") | ||||||||||||
| return html.Execute(ctx, htmlData) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix URL field naming to follow Go conventions.
The field names
ProxyUrlandRawSpecUrldon't follow Go conventions for acronyms. They should beProxyURLandRawSpecURL.Don't forget to update all references to these fields in scalar.go and the README.md file.
Also applies to: 52-52
🤖 Prompt for AI Agents (early access)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apply this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
@gaby I'll apply this change to rename the struct fields to follow Go naming conventions for acronyms.
flowchart TB A[Task: Rename URL fields to follow Go conventions] A --> B[Search for all references to ProxyUrl and RawSpecUrl] A --> C[Update field names in scalar/config.go] A --> D[Update all references in scalar.go] A --> E[Update any references in README.md] B --> F[Find usages with rg command] C --> G[Change ProxyUrl to ProxyURL] C --> H[Change RawSpecUrl to RawSpecURL] D --> I[Update struct field access] E --> J[Update documentation references]🏁 Script executed:
Length of output: 2776
🏁 Script executed:
Length of output: 379
Perfect! I found all the references and there are no README updates needed. Now I can provide all the necessary code changes:
flowchart TB A["Task: Rename URL fields to follow Go conventions"] A --o B["Update field names in scalar/config.go"] A --o C["Update references in scalar/scalar.go"] A --o D["Update template in scalar/index.go"] A --o E["Update test files in scalar/scalar_test.go"] B --o F["ProxyUrl → ProxyURL"] B --o G["RawSpecUrl → RawSpecURL"] B --o H["Update configDefault"] C --o I["Update field access in logic"] D --o J["Update template variables"] E --o K["Update test configurations"]The changes will rename
ProxyUrltoProxyURLandRawSpecUrltoRawSpecURLthroughout the codebase to follow Go naming conventions for acronyms. This affects 4 files total with no README updates needed.⏳ Preparing changes...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A summary of the edits CodeRabbit can apply:
A plan of each step that CodeRabbit will take in order to execute the edits for each file (with inline rough draft snippets):
📄 .
In scalar/config.go, update the struct definition and default values as follows:
In scalar/scalar.go, update all references to RawSpecUrl to RawSpecURL:
In scalar/index.go, update the template to use ProxyURL instead of ProxyUrl:
In scalar/scalar_test.go, update test configurations to use RawSpecURL:
Run a final search to verify no leftover references in documentation:
Since this returns no matches, no README.md updates are required.
A summary of the context that CodeRabbit is considering across the codebase:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A summary of the edits CodeRabbit can apply:
A plan of each step that CodeRabbit will take in order to execute the edits for each file (with inline rough draft snippets):
✏️ scalar/config.go
In scalar/config.go, update the Config struct definition. Replace the ProxyUrl and RawSpecUrl declarations as follows:
Also in scalar/config.go, update the configDefault block to use RawSpecURL:
var configDefault = Config{ Next: nil, BasePath: "/", Path: "docs", Title: "Fiber API documentation", CacheAge: 60, - RawSpecUrl: "doc.json", + RawSpecURL: "doc.json", ForceOffline: true, FallbackCacheAge: 86400, }In scalar/scalar.go, update all references to RawSpecUrl to RawSpecURL:
In scalar/index.go, update the template variable ProxyUrl to ProxyURL:
Scalar.createApiReference('#app', { content: {{.FileContentString}}, - {{- if .ProxyUrl}} - proxyUrl: "{{.ProxyUrl}}", + {{- if .ProxyURL}} + proxyUrl: "{{.ProxyURL}}", {{ end }} });In scalar/scalar_test.go, rename RawSpecUrl to RawSpecURL in all Config initializations:
app := setupApp() app.Use(New(Config{ - RawSpecUrl: "swagger.json", + RawSpecURL: "swagger.json", })) BasePath: "/api", Path: "swagger", - RawSpecUrl: "openapi.json", + RawSpecURL: "openapi.json", CacheAge: 3600, }))Run a final search to ensure no old field names remain:
✏️ scalar/index.go
Change the conditional check from
{{- if .ProxyUrl}}to{{- if .ProxyURL}}.Update the property line from
proxyUrl: "{{.ProxyUrl}}",toproxyUrl: "{{.ProxyURL}}",.Save the file and run
go build(or appropriate template compilation) to confirm the template compiles and references the correct field name.A summary of the context that CodeRabbit is considering across the codebase: