-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloader.go
45 lines (39 loc) · 858 Bytes
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package goruda
import (
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
func LoadSwaggerFile(swaggerFile string) *openapi3.Swagger {
var err error
var data []byte
if strings.HasPrefix(swaggerFile, "http") {
resp, err := http.Get(swaggerFile)
if err != nil {
log.Fatal(err)
}
data, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
} else {
data, err = ioutil.ReadFile(swaggerFile)
if err != nil {
log.Fatal(err)
}
}
loader := openapi3.NewSwaggerLoader()
var swagger *openapi3.Swagger
if strings.HasSuffix(swaggerFile, ".yaml") || strings.HasSuffix(swaggerFile, ".yml") {
swagger, err = loader.LoadSwaggerFromYAMLData(data)
} else {
swagger, err = loader.LoadSwaggerFromData(data)
}
if err != nil {
log.Fatal(err)
}
return swagger
}