Skip to content
Closed
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
73 changes: 73 additions & 0 deletions scalar/config.go
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
Copy link
Contributor

@coderabbitai coderabbitai bot May 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix URL field naming to follow Go conventions.

The field names ProxyUrl and RawSpecUrl don't follow Go conventions for acronyms. They should be ProxyURL and RawSpecURL.

 // Proxy to avoid CORS issues
 // Optional. Default: "https://proxy.scalar.com"
-ProxyUrl string
+ProxyURL string

 // Raw Space Url
 // Optional. Default: doc.json
-RawSpecUrl string
+RawSpecURL string

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)
In scalar/config.go at lines 48 and 52, rename the struct fields from ProxyUrl to ProxyURL and RawSpecUrl to RawSpecURL to follow Go naming conventions for acronyms. After renaming, update all references to these fields throughout scalar.go and the README.md file to ensure consistency and prevent build errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply this change

Copy link
Contributor

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]
Loading

🏁 Script executed:

#!/bin/bash
# Find all references to ProxyUrl and RawSpecUrl in the codebase
echo "=== Searching for ProxyUrl references ==="
rg "ProxyUrl" -A 2 -B 2

echo "=== Searching for RawSpecUrl references ==="
rg "RawSpecUrl" -A 2 -B 2

echo "=== List all Go files in scalar directory ==="
fd "\.go$" scalar/

echo "=== Check if README.md exists ==="
fd "README\.md" .

Length of output: 2776



🏁 Script executed:

#!/bin/bash
# Check main README.md for references
echo "=== Searching main README.md for ProxyUrl and RawSpecUrl references ==="
rg "ProxyUrl|RawSpecUrl" README.md || echo "No references found in main README.md"

# Check if there's a scalar-specific README
echo "=== Checking for scalar README ==="
fd "README\.md" scalar/ || echo "No README found in scalar directory"

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"]
Loading

The changes will rename ProxyUrl to ProxyURL and RawSpecUrl to RawSpecURL throughout the codebase to follow Go naming conventions for acronyms. This affects 4 files total with no README updates needed.


⏳ Preparing changes...

Copy link
Contributor

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:

All occurrences of the struct fields ProxyUrl and RawSpecUrl will be renamed to ProxyURL and RawSpecURL across the scalar codebase to follow Go conventions for acronyms.

Rename ProxyUrl to ProxyURL and RawSpecUrl to RawSpecURL across scalar/config.go, scalar/scalar.go, scalar/index.go, and scalar/scalar_test.go (no README.md changes required).

Will identify all references to ProxyUrl and RawSpecUrl in the repository.

We will search for all occurrences of ProxyUrl and RawSpecUrl across the repository to determine every location that must be renamed.

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:

--- a/scalar/config.go
+++ b/scalar/config.go
@@ type Config struct {
-    // Proxy to avoid CORS issues
-    // Optional.
-    ProxyUrl string
+    // Proxy to avoid CORS issues
+    // Optional. Default: ""
+    ProxyURL string

-    // Raw Space Url
-    // Optional. Default: doc.json
-    RawSpecUrl string
+    // Raw Space Url
+    // Optional. Default: doc.json
+    RawSpecURL string
@@ var configDefault = Config{
-    RawSpecUrl:       "doc.json",
+    RawSpecURL:       "doc.json",
 }

In scalar/scalar.go, update all references to RawSpecUrl to RawSpecURL:

--- a/scalar/scalar.go
+++ b/scalar/scalar.go
@@ if len(cfg.RawSpecUrl) == 0 {
-       if len(cfg.RawSpecUrl) == 0 {
-           cfg.RawSpecUrl = configDefault.RawSpecUrl
+       if len(cfg.RawSpecURL) == 0 {
+           cfg.RawSpecURL = configDefault.RawSpecURL
        }
@@
-    specURL := path.Join(scalarUIPath, cfg.RawSpecUrl)
+    specURL := path.Join(scalarUIPath, cfg.RawSpecURL)

In scalar/index.go, update the template to use ProxyURL instead of ProxyUrl:

--- a/scalar/index.go
+++ b/scalar/index.go
@@ Scalar.createApiReference('#app', {
-            {{- if .ProxyUrl}}
-            proxyUrl: "{{.ProxyUrl}}",
+            {{- if .ProxyURL}}
+            proxyUrl: "{{.ProxyURL}}",
             {{ end }}

In scalar/scalar_test.go, update test configurations to use RawSpecURL:

--- a/scalar/scalar_test.go
+++ b/scalar/scalar_test.go
@@ app.Use(New(Config{
-       RawSpecUrl: "swagger.json",
+       RawSpecURL: "swagger.json",
@@
-       RawSpecUrl: "openapi.json",
+       RawSpecURL: "openapi.json",

Run a final search to verify no leftover references in documentation:

rg --include="*.md" "ProxyUrl|RawSpecUrl" .

Since this returns no matches, no README.md updates are required.

A summary of the context that CodeRabbit is considering across the codebase:

We need to identify all references to ProxyUrl and RawSpecUrl to ensure complete renaming; though user provided shell output, integrate it into context.

  • ✅ Create PR with these edits
  • 📌 Create commit in current branch
  • 📋 Get copyable edits

Copy link
Contributor

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:

Rename struct fields ProxyUrl to ProxyURL and RawSpecUrl to RawSpecURL across the scalar package and update all code references.

Rename ProxyUrl to ProxyURL and RawSpecUrl to RawSpecURL across the scalar package and update all affected code and tests.

Rename ProxyUrl to ProxyURL and RawSpecUrl to RawSpecURL across the Scalar module to follow Go acronym conventions.

Update ProxyUrl to ProxyURL in the Go template within scalar/index.go

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:

 	// Proxy to avoid CORS issues
-	// Optional.
+	// Optional. Default: ""
-	ProxyUrl string
+	ProxyURL string

 	// Raw Space Url
 	// Optional. Default: doc.json
-	RawSpecUrl string
+	RawSpecURL string

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:

-    if len(cfg.RawSpecUrl) == 0 {
-        cfg.RawSpecUrl = configDefault.RawSpecUrl
+    if len(cfg.RawSpecURL) == 0 {
+        cfg.RawSpecURL = configDefault.RawSpecURL
     }

     scalarUIPath := path.Join(cfg.BasePath, cfg.Path)
-    specURL := path.Join(scalarUIPath, cfg.RawSpecUrl)
+    specURL := path.Join(scalarUIPath, cfg.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:

rg -n "ProxyUrl|RawSpecUrl" .
✏️ scalar/index.go

Change the conditional check from {{- if .ProxyUrl}} to {{- if .ProxyURL}}.


Update the property line from proxyUrl: "{{.ProxyUrl}}", to proxyUrl: "{{.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:

We need to identify all occurrences of ProxyUrl and RawSpecUrl to determine every file that requires updating.

Locate occurrences of the outdated .ProxyUrl reference in the Go HTML template so we know exactly which lines to update.

  • ✅ Create PR with these edits
  • 📌 Create commit in current branch
  • 📋 Get copyable edits


// 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,
}
39 changes: 39 additions & 0 deletions scalar/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module github.com/yokeTH/gofiber-scalar/scalar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be github.com/gofiber/contrib/scalar


go 1.23.8

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
)
97 changes: 97 additions & 0 deletions scalar/go.sum
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=
60 changes: 60 additions & 0 deletions scalar/index.go
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>";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this element about reporting issues

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The 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>`
1 change: 1 addition & 0 deletions scalar/scalar-version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.30.0
104 changes: 104 additions & 0 deletions scalar/scalar.go
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
specURL := path.Join(scalarUIPath, cfg.RawSpecUrl)
jsFallbackPath := path.Join(scalarUIPath, "/js/api-reference.min.js")
scalarUIPath := path.Join(cfg.BasePath, cfg.Path)
specURL := path.Join(scalarUIPath, cfg.RawSpecURL)
jsFallbackPath := path.Join(scalarUIPath, "/js/api-reference.min.js")
🤖 Prompt for AI Agents (early access)
In scalar/scalar.go around lines 54 to 55, the path construction uses outdated field names. Update the code to use the renamed fields for scalarUIPath and RawSpecUrl to reflect the current struct or variable names, ensuring the paths are constructed correctly with the new field identifiers.


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)
}
}
44 changes: 44 additions & 0 deletions scalar/scalar.min.js

Large diffs are not rendered by default.

Loading
Loading