### Full Configuration Example for API Docs in Go
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
This Go code provides a comprehensive example of configuring Scalar Go's API documentation generation. It includes options for CDN, theme, layout, proxy settings, authentication, path routing, metadata, and more. This allows for maximum customization to meet complex integration needs.
```go
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
app.Use(recover.New())
app.Use(cors.New())
app.Get("/api/documentation", func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "./docs/openapi.yaml",
CDN: "https://cdn.jsdelivr.net/npm/@scalar/api-reference",
Theme: scalar.ThemeDeepSpace,
Layout: scalar.LayoutModern,
Proxy: "https://proxy.example.com",
IsEditable: false,
ShowSidebar: true,
HideModels: false,
HideDownloadButton: false,
DarkMode: true,
SearchHotKey: "k",
MetaData: `{"version": "v1", "environment": "production"}`,
HiddenClients: []string{"curl", "wget"},
Authentication: "bearer",
PathRouting: "/api/v1",
BaseServerURL: "https://api.example.com",
WithDefaultFonts: true,
CustomOptions: scalar.CustomOptions{
PageTitle: "Production API Documentation",
},
})
if err != nil {
log.Printf("Documentation generation error: %v", err)
return c.Status(500).JSON(fiber.Map{
"error": "Failed to generate API documentation",
})
}
c.Type("html")
return c.SendString(htmlContent)
})
// Sample API endpoint
app.Get("/api/hello", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"message": "Hello World",
"status": "success",
})
})
log.Fatal(app.Listen(":3000"))
}
```
--------------------------------
### Install scalar-go Package
Source: https://github.com/watchakorn-18k/scalar-go/blob/main/README.md
Installs the latest version of the scalar-go package using the Go get command. This is the first step to include the package in your project.
```bash
go get -u github.com/watchakorn-18k/scalar-go@latest
```
--------------------------------
### Generate HTML API Docs from Local File (Fiber)
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Generates API reference HTML documentation using Scalar Go from a local Swagger/OpenAPI file. This example integrates with the Fiber web framework, serving the generated HTML at the '/api/docs' endpoint. It demonstrates configuring options like SpecURL, PageTitle, DarkMode, and Layout.
```go
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
// Serve API documentation from a local file
app.Use("/api/docs", func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "./docs/swagger.yaml",
CustomOptions: scalar.CustomOptions{
PageTitle: "My API Documentation",
},
DarkMode: true,
Layout: scalar.LayoutModern,
})
if err != nil {
log.Printf("Error generating API reference: %v", err)
return c.Status(500).SendString("Failed to generate documentation")
}
c.Type("html")
return c.SendString(htmlContent)
})
log.Fatal(app.Listen(":3000"))
}
```
--------------------------------
### Generate HTML API Docs from Remote URL (HTTP)
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Generates API reference HTML documentation using Scalar Go by fetching the OpenAPI specification from a remote HTTP/HTTPS URL. This approach is suitable when specifications are hosted elsewhere. The example uses Go's standard http package to serve the documentation. It showcases options like SpecURL, PageTitle, Theme, ShowSidebar, and DarkMode.
```go
package main
import (
"log"
"net/http"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
http.HandleFunc("/docs", func(w http.ResponseWriter, r *http.Request) {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "https://petstore3.swagger.io/api/v3/openapi.json",
CustomOptions: scalar.CustomOptions{
PageTitle: "Petstore API",
},
Theme: scalar.ThemePurple,
ShowSidebar: true,
DarkMode: false,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(htmlContent))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
--------------------------------
### Theme Configuration Options
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Shows how to configure the visual appearance of the API documentation using predefined themes provided by Scalar.
```APIDOC
## Theme Configuration
### Description
This example demonstrates how to apply different visual themes to the API reference documentation by iterating through available `scalar.ThemeId` options and setting them in the `scalar.Options`.
### Method
GET
### Endpoint
/docs/{themeName}
### Parameters
#### Path Parameters
- **themeName** (string) - Required - The name of the theme to apply (e.g., 'default', 'moon', 'purple').
#### Query Parameters
None
#### Request Body
None
### Request Example
```go
package main
import (
"fmt"
"log"
"github.com/gofiber/fiber/v2"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
themes := []scalar.ThemeId{
scalar.ThemeDefault,
scalar.ThemeAlternate,
scalar.ThemeMoon,
scalar.ThemePurple,
scalar.ThemeSolarized,
scalar.ThemeBluePlanet,
scalar.ThemeDeepSpace,
scalar.ThemeSaturn,
scalar.ThemeKepler,
scalar.ThemeMars,
}
for i, theme := range themes {
themeName := string(theme)
app.Get(fmt.Sprintf("/docs/%s", themeName), func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "./swagger.yaml",
Theme: theme,
CustomOptions: scalar.CustomOptions{
PageTitle: fmt.Sprintf("API Docs - %s Theme", themeName),
},
})
if err != nil {
return err
}
c.Type("html")
return c.SendString(htmlContent)
})
}
log.Fatal(app.Listen(":3000"))
}
```
### Response
#### Success Response (200)
- **html** (string) - The HTML content of the API reference documentation with the specified theme applied.
#### Response Example
```html
API Docs - moon Theme
```
```
--------------------------------
### Apply Multiple Themes to API Docs in Go
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Illustrates how to iterate through various predefined themes provided by scalar-go and generate API reference documentation for each. This allows easy switching between different visual styles for the documentation interface within a single application.
```go
package main
import (
"fmt"
"log"
"github.com/gofiber/fiber/v2"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
// Example with multiple theme options
themes := []scalar.ThemeId{
scalar.ThemeDefault,
scalar.ThemeAlternate,
scalar.ThemeMoon,
scalar.ThemePurple,
scalar.ThemeSolarized,
scalar.ThemeBluePlanet,
scalar.ThemeDeepSpace,
scalar.ThemeSaturn,
scalar.ThemeKepler,
scalar.ThemeMars,
}
for i, theme := range themes {
themeName := string(theme)
app.Get(fmt.Sprintf("/docs/%s", themeName), func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "./swagger.yaml",
Theme: theme,
CustomOptions: scalar.CustomOptions{
PageTitle: fmt.Sprintf("API Docs - %s Theme", themeName),
},
})
if err != nil {
return err
}
c.Type("html")
return c.SendString(htmlContent)
})
}
log.Fatal(app.Listen(":3000"))
}
```
--------------------------------
### Configure Default Options in Go
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Shows how to initialize the `scalar.Options` struct with minimal settings and then use `scalar.DefaultOptions` to apply sensible default values for unprovided fields. This ensures the configuration is complete before generating documentation.
```go
package main
import (
"fmt"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
// Create minimal options
opts := scalar.Options{
SpecURL: "./swagger.yaml",
}
// Apply defaults
configuredOpts := scalar.DefaultOptions(opts)
fmt.Printf("CDN: %s\n", configuredOpts.CDN)
// Output: CDN: https://cdn.jsdelivr.net/npm/@scalar/api-reference
fmt.Printf("Layout: %s\n", configuredOpts.Layout)
// Output: Layout: modern
}
```
--------------------------------
### Default Options Configuration
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Explains how to use `DefaultOptions` to apply sensible default configuration values to the `Options` struct, ensuring required fields are populated.
```APIDOC
## Default Options
### Description
The `DefaultOptions` function populates missing fields in the `scalar.Options` struct with sensible default values, simplifying configuration.
### Method
N/A (Function Call)
### Endpoint
N/A
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```go
package main
import (
"fmt"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
// Create minimal options
opts := scalar.Options{
SpecURL: "./swagger.yaml",
}
// Apply defaults
configuredOpts := scalar.DefaultOptions(opts)
fmt.Printf("CDN: %s\n", configuredOpts.CDN)
// Output: CDN: https://cdn.jsdelivr.net/npm/@scalar/api-reference
fmt.Printf("Layout: %s\n", configuredOpts.Layout)
// Output: Layout: modern
}
```
### Response
#### Success Response
- **configuredOpts** (scalar.Options) - The Options struct with default values applied.
#### Response Example
N/A
```
--------------------------------
### Inline OpenAPI Specification
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Demonstrates how to provide an OpenAPI specification directly as a map or string within your Go code to generate API reference documentation.
```APIDOC
## GET /api/docs
### Description
This endpoint serves the API reference documentation generated from an inline OpenAPI specification.
### Method
GET
### Endpoint
/api/docs
### Parameters
None
### Request Example
None
### Response
#### Success Response (200)
- **html** (string) - The HTML content of the API reference documentation.
#### Response Example
```html
Inline API Docs
```
```
--------------------------------
### Embed OpenAPI Spec in Go with Fiber
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
Demonstrates how to provide an OpenAPI specification as a map directly within Go code and serve it as an HTML API reference using the fiber framework. This method is useful for embedding documentation without external files or for dynamic generation.
```go
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
// Define OpenAPI spec inline
specContent := map[string]interface{}{
"openapi": "3.1.0",
"info": map[string]interface{}{
"title": "Inline API",
"version": "1.0.0",
},
"paths": map[string]interface{}{
"/users": map[string]interface{}{
"get": map[string]interface{}{
"summary": "Get all users",
"responses": map[string]interface{}{
"200": map[string]interface{}{
"description": "Success",
"content": map[string]interface{}{
"application/json": map[string]interface{}{
"example": map[string]interface{}{
"users": []string{"alice", "bob"},
},
},
},
},
},
},
},
},
}
app.Get("/api/docs", func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecContent: specContent,
CustomOptions: scalar.CustomOptions{
PageTitle: "Inline API Docs",
},
Theme: scalar.ThemeMoon,
Layout: scalar.LayoutClassic,
})
if err != nil {
return err
}
c.Type("html")
return c.SendString(htmlContent)
})
log.Fatal(app.Listen(":3000"))
}
```
--------------------------------
### Customize API Docs Appearance with Custom CSS in Go
Source: https://context7.com/watchakorn-18k/scalar-go/llms.txt
This Go snippet demonstrates how to inject custom CSS to style the generated API documentation. It allows overriding default styles for elements like background and accent colors, enabling branding consistency. This approach is useful when predefined themes are insufficient for specific design requirements.
```go
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
customCSS := `
.light-mode {
--scalar-color-accent: #ff6b35;
--scalar-background-1: #f8f9fa;
}
.dark-mode {
--scalar-color-accent: #ff6b35;
--scalar-background-1: #1a1a1a;
}
`
app.Get("/api/docs", func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "./swagger.yaml",
CustomOptions: scalar.CustomOptions{
PageTitle: "Branded API Documentation",
},
CustomCss: customCSS,
DarkMode: true,
ShowSidebar: true,
WithDefaultFonts: false,
HideDownloadButton: false,
HideModels: false,
IsEditable: false,
})
if err != nil {
return c.Status(500).SendString(err.Error())
}
c.Type("html")
return c.SendString(htmlContent)
})
log.Fatal(app.Listen(":3000"))
}
```
--------------------------------
### Generate API Reference HTML in Fiber App
Source: https://github.com/watchakorn-18k/scalar-go/blob/main/README.md
Generates an API reference HTML document using scalar.ApiReferenceHTML within a Fiber web application. It configures options like SpecURL, PageTitle, and DarkMode, then serves the generated HTML. This demonstrates integrating the scalar-go package into a Go web server.
```go
package main
import (
"log"
"simple_api/middlewares"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/watchakorn-18k/scalar-go"
)
func main() {
app := fiber.New()
middlewares.Logger(app)
app.Use("/api/docs", func(c *fiber.Ctx) error {
htmlContent, err := scalar.ApiReferenceHTML(&scalar.Options{
SpecURL: "./docs/swagger.yaml",
CustomOptions: scalar.CustomOptions{
PageTitle: "Simple API",
},
DarkMode: true,
})
if err != nil {
return err
}
c.Type("html")
return c.SendString(htmlContent)
})
app.Use(recover.New())
app.Use(cors.New())
api := app.Group("/api/")
api.Get("/hello", func(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
"message": "Hello World",
})
})
log.Fatal(app.Listen(":3000"))
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.