2024-05-14 13:07:09 +00:00
|
|
|
package swag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Name is a unique name be used to register swag instance.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
const Name = "swagger"
|
|
|
|
|
|
|
|
var (
|
|
|
|
swaggerMu sync.RWMutex
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
swags map[string]Swagger
|
2024-05-14 13:07:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Swagger is an interface to read swagger document.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
type Swagger interface {
|
|
|
|
ReadDoc() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers swagger for given name.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
func Register(name string, swagger Swagger) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
swaggerMu.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
defer swaggerMu.Unlock()
|
|
|
|
|
|
|
|
if swagger == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
panic("swagger is nil")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if swags == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
swags = make(map[string]Swagger)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := swags[name]; ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
panic("Register called twice for swag: " + name)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
swags[name] = swagger
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSwagger returns the swagger instance for given name.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// If not found, returns nil.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
func GetSwagger(name string) Swagger {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
swaggerMu.RLock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
defer swaggerMu.RUnlock()
|
|
|
|
|
|
|
|
return swags[name]
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDoc reads swagger document. An optional name parameter can be passed to read a specific document.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// The default name is "swagger".
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
func ReadDoc(optionalName ...string) (string, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
swaggerMu.RLock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
defer swaggerMu.RUnlock()
|
|
|
|
|
|
|
|
if swags == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", errors.New("no swag has yet been registered")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := Name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if len(optionalName) != 0 && optionalName[0] != "" {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
name = optionalName[0]
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
swag, ok := swags[name]
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if !ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", fmt.Errorf("no swag named \"%s\" was registered", name)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return swag.ReadDoc(), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|