Skip to content

Commit

Permalink
Add option to read swagger spec directly from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradunsky committed Nov 20, 2018
1 parent e343254 commit ed9a245
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2016-2017 dapperdox.com
Copyright (C) 2016-2017 dapperdox.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,6 +30,7 @@ type config struct {
BindAddr string `env:"BIND_ADDR" flag:"bind-addr" flagDesc:"Bind address"`
AssetsDir string `env:"ASSETS_DIR" flag:"assets-dir" flagDesc:"Assets to serve. Effectively the document root."`
DefaultAssetsDir string `env:"DEFAULT_ASSETS_DIR" flag:"default-assets-dir" flagDesc:"Default assets."`
SpecURL string `env:"SPEC_URL" flag:"spec-url" flagDesc:"OpenAPI specification (swagger) server url"`
SpecDir string `env:"SPEC_DIR" flag:"spec-dir" flagDesc:"OpenAPI specification (swagger) directory"`
SpecFilename []string `env:"SPEC_FILENAME" flag:"spec-filename" flagDesc:"The filename of the OpenAPI specification file within the spec-dir. May be multiply defined. Defaults to spec/swagger.json"`
Theme string `env:"THEME" flag:"theme" flagDesc:"Theme to render documentation"`
Expand All @@ -56,6 +57,7 @@ func Get() (*config, error) {
cfg = &config{
BindAddr: "localhost:3123",
SpecDir: "",
SpecURL: "",
DefaultAssetsDir: "assets",
LogLevel: "info",
SiteURL: "http://localhost:3123/",
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func main() {
specs.Register(router)
spec.LoadStatusCodes()

err = spec.LoadSpecifications(cfg.BindAddr, true)
if cfg.SpecURL != "" {
err = spec.LoadSpecification(cfg.SpecURL)
} else {
err = spec.LoadSpecifications(cfg.BindAddr, true)
}
if err != nil {
logger.Errorf(nil, "Load specification error: %s", err)
os.Exit(1)
Expand Down
38 changes: 30 additions & 8 deletions spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ func LoadSpecifications(specHost string, collapse bool) error {
return nil
}

func LoadSpecification(specUrl string) error {
if APISuite == nil {
APISuite = make(map[string]*APISpecification)
}

var ok bool
var specification *APISpecification

if specification, ok = APISuite[""]; !ok {
specification = &APISpecification{}
}

var err = specification.Load(specUrl, "")
if err != nil {
return err
}

APISuite[specification.ID] = specification

return nil
}

// -----------------------------------------------------------------------------
// Load loads API specs from the supplied host (usually local!)
func (c *APISpecification) Load(specLocation string, specHost string) error {
Expand Down Expand Up @@ -386,10 +408,10 @@ func (c *APISpecification) Load(specLocation string, specHost string) error {
// If we're grouping by TAGs, then build the API at the tag level
if groupingByTag {
api = &APIGroup{
ID: TitleToKebab(name),
Name: name,
URL: u,
Info: &c.APIInfo,
ID: TitleToKebab(name),
Name: name,
URL: u,
Info: &c.APIInfo,
MethodNavigationByName: methodNavByName,
MethodSortBy: methodSortBy,
Consumes: apispec.Consumes,
Expand All @@ -407,10 +429,10 @@ func (c *APISpecification) Load(specLocation string, specHost string) error {
// If not grouping by tag, then build the API at the path level
if !groupingByTag {
api = &APIGroup{
ID: TitleToKebab(name),
Name: name,
URL: u,
Info: &c.APIInfo,
ID: TitleToKebab(name),
Name: name,
URL: u,
Info: &c.APIInfo,
MethodNavigationByName: methodNavByName,
MethodSortBy: methodSortBy,
Consumes: apispec.Consumes,
Expand Down

0 comments on commit ed9a245

Please sign in to comment.