Skip to content

Commit ed9a245

Browse files
committed
Add option to read swagger spec directly from URL
1 parent e343254 commit ed9a245

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

config/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2016-2017 dapperdox.com
2+
Copyright (C) 2016-2017 dapperdox.com
33
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -30,6 +30,7 @@ type config struct {
3030
BindAddr string `env:"BIND_ADDR" flag:"bind-addr" flagDesc:"Bind address"`
3131
AssetsDir string `env:"ASSETS_DIR" flag:"assets-dir" flagDesc:"Assets to serve. Effectively the document root."`
3232
DefaultAssetsDir string `env:"DEFAULT_ASSETS_DIR" flag:"default-assets-dir" flagDesc:"Default assets."`
33+
SpecURL string `env:"SPEC_URL" flag:"spec-url" flagDesc:"OpenAPI specification (swagger) server url"`
3334
SpecDir string `env:"SPEC_DIR" flag:"spec-dir" flagDesc:"OpenAPI specification (swagger) directory"`
3435
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"`
3536
Theme string `env:"THEME" flag:"theme" flagDesc:"Theme to render documentation"`
@@ -56,6 +57,7 @@ func Get() (*config, error) {
5657
cfg = &config{
5758
BindAddr: "localhost:3123",
5859
SpecDir: "",
60+
SpecURL: "",
5961
DefaultAssetsDir: "assets",
6062
LogLevel: "info",
6163
SiteURL: "http://localhost:3123/",

main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ func main() {
9494
specs.Register(router)
9595
spec.LoadStatusCodes()
9696

97-
err = spec.LoadSpecifications(cfg.BindAddr, true)
97+
if cfg.SpecURL != "" {
98+
err = spec.LoadSpecification(cfg.SpecURL)
99+
} else {
100+
err = spec.LoadSpecifications(cfg.BindAddr, true)
101+
}
98102
if err != nil {
99103
logger.Errorf(nil, "Load specification error: %s", err)
100104
os.Exit(1)

spec/spec.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ func LoadSpecifications(specHost string, collapse bool) error {
293293
return nil
294294
}
295295

296+
func LoadSpecification(specUrl string) error {
297+
if APISuite == nil {
298+
APISuite = make(map[string]*APISpecification)
299+
}
300+
301+
var ok bool
302+
var specification *APISpecification
303+
304+
if specification, ok = APISuite[""]; !ok {
305+
specification = &APISpecification{}
306+
}
307+
308+
var err = specification.Load(specUrl, "")
309+
if err != nil {
310+
return err
311+
}
312+
313+
APISuite[specification.ID] = specification
314+
315+
return nil
316+
}
317+
296318
// -----------------------------------------------------------------------------
297319
// Load loads API specs from the supplied host (usually local!)
298320
func (c *APISpecification) Load(specLocation string, specHost string) error {
@@ -386,10 +408,10 @@ func (c *APISpecification) Load(specLocation string, specHost string) error {
386408
// If we're grouping by TAGs, then build the API at the tag level
387409
if groupingByTag {
388410
api = &APIGroup{
389-
ID: TitleToKebab(name),
390-
Name: name,
391-
URL: u,
392-
Info: &c.APIInfo,
411+
ID: TitleToKebab(name),
412+
Name: name,
413+
URL: u,
414+
Info: &c.APIInfo,
393415
MethodNavigationByName: methodNavByName,
394416
MethodSortBy: methodSortBy,
395417
Consumes: apispec.Consumes,
@@ -407,10 +429,10 @@ func (c *APISpecification) Load(specLocation string, specHost string) error {
407429
// If not grouping by tag, then build the API at the path level
408430
if !groupingByTag {
409431
api = &APIGroup{
410-
ID: TitleToKebab(name),
411-
Name: name,
412-
URL: u,
413-
Info: &c.APIInfo,
432+
ID: TitleToKebab(name),
433+
Name: name,
434+
URL: u,
435+
Info: &c.APIInfo,
414436
MethodNavigationByName: methodNavByName,
415437
MethodSortBy: methodSortBy,
416438
Consumes: apispec.Consumes,

0 commit comments

Comments
 (0)