Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to read swagger spec directly from URL #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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