cff632f5 by astaxie

beego: swagger EnableDocs

1 parent 4990d888
...@@ -383,6 +383,9 @@ func initBeforeHttpRun() { ...@@ -383,6 +383,9 @@ func initBeforeHttpRun() {
383 for u, _ := range StaticDir { 383 for u, _ := range StaticDir {
384 Get(u+"/*", serverStaticRouter) 384 Get(u+"/*", serverStaticRouter)
385 } 385 }
386 if EnableDocs {
387 Get("/docs/*", serverDocs)
388 }
386 } 389 }
387 390
388 // this function is for test package init 391 // this function is for test package init
......
...@@ -71,6 +71,7 @@ var ( ...@@ -71,6 +71,7 @@ var (
71 FlashName string // name of the flash variable found in response header and cookie 71 FlashName string // name of the flash variable found in response header and cookie
72 FlashSeperator string // used to seperate flash key:value 72 FlashSeperator string // used to seperate flash key:value
73 AppConfigProvider string // config provider 73 AppConfigProvider string // config provider
74 EnableDocs bool // enable generate docs & server docs API Swagger
74 ) 75 )
75 76
76 func init() { 77 func init() {
...@@ -361,6 +362,10 @@ func ParseConfig() (err error) { ...@@ -361,6 +362,10 @@ func ParseConfig() (err error) {
361 if adminhttpport, err := getConfig("int", "AdminHttpPort"); err == nil { 362 if adminhttpport, err := getConfig("int", "AdminHttpPort"); err == nil {
362 AdminHttpPort = adminhttpport.(int) 363 AdminHttpPort = adminhttpport.(int)
363 } 364 }
365
366 if enabledocs, err := getConfig("bool", "EnableDocs"); err == nil {
367 EnableDocs = enabledocs.(bool)
368 }
364 } 369 }
365 return nil 370 return nil
366 } 371 }
......
1 package beego
2
3 import (
4 "encoding/json"
5
6 "github.com/astaxie/beego/context"
7 )
8
9 var GlobalDocApi map[string]interface{}
10
11 func init() {
12 if EnableDocs {
13 GlobalDocApi = make(map[string]interface{})
14 }
15 }
16
17 func serverDocs(ctx *context.Context) {
18 var obj interface{}
19 if splat := ctx.Input.Param(":splat"); splat == "" {
20 obj = GlobalDocApi["Root"]
21 } else {
22 if v, ok := GlobalDocApi[splat]; ok {
23 obj = v
24 }
25 }
26 if obj != nil {
27 bt, err := json.Marshal(obj)
28 if err != nil {
29 ctx.Output.SetStatus(504)
30 return
31 }
32 ctx.Output.Header("Content-Type", "application/json;charset=UTF-8")
33 ctx.Output.Header("Access-Control-Allow-Origin", "*")
34 ctx.Output.Body(bt)
35 return
36 }
37 ctx.Output.SetStatus(404)
38 }
1 package beego
2
3 const swaggerVersion = "1.2"
4
5 type ResourceListing struct {
6 ApiVersion string `json:"apiVersion"`
7 SwaggerVersion string `json:"swaggerVersion"` // e.g 1.2
8 // BasePath string `json:"basePath"` obsolete in 1.1
9 Apis []ApiRef `json:"apis"`
10 Infos Infomation `json:"info"`
11 }
12
13 type ApiRef struct {
14 Path string `json:"path"` // relative or absolute, must start with /
15 Description string `json:"description"`
16 }
17
18 type Infomation struct {
19 Title string `json:"title,omitempty"`
20 Description string `json:"description,omitempty"`
21 Contact string `json:"contact,omitempty"`
22 TermsOfServiceUrl string `json:"termsOfServiceUrl,omitempty"`
23 License string `json:"license,omitempty"`
24 LicenseUrl string `json:"licenseUrl,omitempty"`
25 }
26
27 // https://github.com/wordnik/swagger-core/blob/scala_2.10-1.3-RC3/schemas/api-declaration-schema.json
28 type ApiDeclaration struct {
29 ApiVersion string `json:"apiVersion"`
30 SwaggerVersion string `json:"swaggerVersion"`
31 BasePath string `json:"basePath"`
32 ResourcePath string `json:"resourcePath"` // must start with /
33 Consumes []string `json:"consumes,omitempty"`
34 Produces []string `json:"produces,omitempty"`
35 Apis []Api `json:"apis,omitempty"`
36 Models map[string]Model `json:"models,omitempty"`
37 }
38
39 type Api struct {
40 Path string `json:"path"` // relative or absolute, must start with /
41 Description string `json:"description"`
42 Operations []Operation `json:"operations,omitempty"`
43 }
44
45 type Operation struct {
46 HttpMethod string `json:"httpMethod"`
47 Nickname string `json:"nickname"`
48 Type string `json:"type"` // in 1.1 = DataType
49 // ResponseClass string `json:"responseClass"` obsolete in 1.2
50 Summary string `json:"summary,omitempty"`
51 Notes string `json:"notes,omitempty"`
52 Parameters []Parameter `json:"parameters,omitempty"`
53 ResponseMessages []ResponseMessage `json:"responseMessages,omitempty"` // optional
54 Consumes []string `json:"consumes,omitempty"`
55 Produces []string `json:"produces,omitempty"`
56 Authorizations []Authorization `json:"authorizations,omitempty"`
57 Protocols []Protocol `json:"protocols,omitempty"`
58 }
59
60 type Protocol struct {
61 }
62
63 type ResponseMessage struct {
64 Code int `json:"code"`
65 Message string `json:"message"`
66 ResponseModel string `json:"responseModel"`
67 }
68
69 type Parameter struct {
70 ParamType string `json:"paramType"` // path,query,body,header,form
71 Name string `json:"name"`
72 Description string `json:"description"`
73 DataType string `json:"dataType"` // 1.2 needed?
74 Type string `json:"type"` // integer
75 Format string `json:"format"` // int64
76 AllowMultiple bool `json:"allowMultiple"`
77 Required bool `json:"required"`
78 Minimum int `json:"minimum"`
79 Maximum int `json:"maximum"`
80 }
81
82 type ErrorResponse struct {
83 Code int `json:"code"`
84 Reason string `json:"reason"`
85 }
86
87 type Model struct {
88 Id string `json:"id"`
89 Required []string `json:"required,omitempty"`
90 Properties map[string]ModelProperty `json:"properties"`
91 }
92
93 type ModelProperty struct {
94 Type string `json:"type"`
95 Description string `json:"description"`
96 Items map[string]string `json:"items,omitempty"`
97 Format string `json:"format"`
98 }
99
100 // https://github.com/wordnik/swagger-core/wiki/authorizations
101 type Authorization struct {
102 LocalOAuth OAuth `json:"local-oauth"`
103 ApiKey ApiKey `json:"apiKey"`
104 }
105
106 // https://github.com/wordnik/swagger-core/wiki/authorizations
107 type OAuth struct {
108 Type string `json:"type"` // e.g. oauth2
109 Scopes []string `json:"scopes"` // e.g. PUBLIC
110 GrantTypes map[string]GrantType `json:"grantTypes"`
111 }
112
113 // https://github.com/wordnik/swagger-core/wiki/authorizations
114 type GrantType struct {
115 LoginEndpoint Endpoint `json:"loginEndpoint"`
116 TokenName string `json:"tokenName"` // e.g. access_code
117 TokenRequestEndpoint Endpoint `json:"tokenRequestEndpoint"`
118 TokenEndpoint Endpoint `json:"tokenEndpoint"`
119 }
120
121 // https://github.com/wordnik/swagger-core/wiki/authorizations
122 type Endpoint struct {
123 Url string `json:"url"`
124 ClientIdName string `json:"clientIdName"`
125 ClientSecretName string `json:"clientSecretName"`
126 TokenName string `json:"tokenName"`
127 }
128
129 // https://github.com/wordnik/swagger-core/wiki/authorizations
130 type ApiKey struct {
131 Type string `json:"type"` // e.g. apiKey
132 PassAs string `json:"passAs"` // e.g. header
133 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!