How swaggo/fiber-swagger works
Project Type and Purpose
• This is an open-source middleware library (Go module) for the Fiber web framework.
• Its purpose is to automatically generate and serve Swagger 2.0 API documentation.
File Structure
Top-level:
• swagger.go / swagger_test.go – middleware entry point and tests
• example/ – a self-contained example Fiber app showing how to integrate fiber-swagger
– example/main.go – application code mounting the middleware
– example/docs – generated Swagger spec (json, yaml) and Go docs package
• goreleaser.yml, ci workflows – release & CI pipelines
Extract Key Architectural Insights from README
• Build-time step: developer runs swag init (Swag CLI) to parse Go comments and generate the docs package.
• Run-time step: application imports both Fiber and fiber-swagger, mounts fiberSwagger.WrapHandler at /swagger/*, and serves the Swagger UI + json/yaml.
Main Components
A. Developer Workspace (SOURCE)
1. application.go + annotated handlers
2. fiber-swagger module (swagger.go)
3. Swag CLI (external tool)
B. Build & Generation Pipeline
1. Developer runs swag init
2. Generates docs/ directory (swagger.json, swagger.yaml, docs.go)
3. go build produces the final binary
C. Run-Time Application
1. Fiber HTTP Server
2. fiber-swagger middleware
3. Auto-generated docs package (embedded spec + UI assets)
D. External Actors & Services
1. Web Browser (Developer/Consumer)
2. HTTP requests to /swagger/*
Relationships and Data Flows
• Build-time:
Source (annotated code) → [Swag CLI] → docs package → go build → binary
• Run-time:
Browser ▶ HTTP GET /swagger/index.html
↳ Fiber server routes request to fiber-swagger handler
↳ Handler serves Swagger UI + loads swagger.json from docs
• Tests: swagger_test.go calls fiber-swagger functions to verify handler behavior
Architectural Patterns & Principles
• Middleware pattern: fiber-swagger sits in the Fiber middleware chain.
• Separation of concerns:
– Code annotations + CLI for spec generation
– HTTP middleware for spec serving
• Plug-and-play: just import and mount; no global state
Capturing Essential Elements
• Don’t forget the example/main.go and example/docs package—they illustrate intended usage.
• swagger.go (middleware core) and swagger_test.go (verifying correct hook behavior).
[Developer Workspace]
└── Annotated Go handlers + import fiber-swagger
└── Swag CLI (external)
[Build Stage]
→ swag init
└── Generated docs/ (json, yaml, doc.go)
→ go build
└── Binary
[Run Stage]
┌──────────────────────────────────────────┐
│ Fiber HTTP Server │
│ ┌──────────────┐ ┌────────────────┐ │
│ │ fiber-swagger│◀──│ docs package │ │
│ │ Middleware │ │ (swagger.json) │ │
│ └──────────────┘ └────────────────┘ │
└──────────────────────────────────────────┘
▲
│ HTTP GET /swagger/index.html
▼
[Web Browser / Consumer]
[CI/CD & Release]
• .github/workflows/ci.yml
• goreleaser.yml