github.com/profzone/eden-framework@v1.0.10/pkg/courier/swagger/swagger.go (about)

     1  package swagger
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"strings"
     9  
    10  	"github.com/profzone/eden-framework/pkg/courier"
    11  	"github.com/profzone/eden-framework/pkg/courier/httpx"
    12  	"github.com/profzone/eden-framework/pkg/courier/transport_http"
    13  )
    14  
    15  func getSwaggerJSON() []byte {
    16  	data, err := ioutil.ReadFile("./api/openapi.json")
    17  	if err != nil {
    18  		return data
    19  	}
    20  	return data
    21  }
    22  
    23  var SwaggerRouter = courier.NewRouter()
    24  
    25  func init() {
    26  	SwaggerRouter.Register(courier.NewRouter(Swagger{}))
    27  	SwaggerRouter.Register(courier.NewRouter(SwaggerDoc{}))
    28  	SwaggerRouter.Register(courier.NewRouter(SwaggerUIBundle{}))
    29  }
    30  
    31  type Swagger struct {
    32  	httpx.MethodGet
    33  }
    34  
    35  func (s Swagger) Output(c context.Context) (interface{}, error) {
    36  	json := &JSONBytes{}
    37  	json.Write(getSwaggerJSON())
    38  	return json, nil
    39  }
    40  
    41  // swagger:strfmt json
    42  type JSONBytes struct {
    43  	bytes.Buffer
    44  }
    45  
    46  func (JSONBytes) ContentType() string {
    47  	return httpx.MIME_JSON
    48  }
    49  
    50  type SwaggerDoc struct {
    51  	httpx.MethodGet
    52  }
    53  
    54  func (SwaggerDoc) Path() string {
    55  	return "/doc"
    56  }
    57  
    58  func (s SwaggerDoc) Output(c context.Context) (interface{}, error) {
    59  	html := &httpx.HTML{}
    60  	u := transport_http.GetRequest(c).URL
    61  
    62  	rootPath := strings.TrimSuffix(u.Path, s.Path())
    63  	bundlePath := rootPath + "/swagger-ui-bundle.js"
    64  	apiPath := rootPath
    65  	if len(apiPath) == 0 {
    66  		apiPath = "/"
    67  	}
    68  
    69  	html.WriteString(`<!-- HTML for static distribution bundle build -->
    70  <!DOCTYPE html>
    71  <html lang="en">
    72    <head>
    73      <meta charset="UTF-8">
    74      <title>Swagger UI</title>
    75      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.4/swagger-ui.css" >
    76      <style>
    77        html
    78        {
    79          box-sizing: border-box;
    80          overflow: -moz-scrollbars-vertical;
    81          overflow-y: scroll;
    82        }
    83  
    84        *,
    85        *:before,
    86        *:after
    87        {
    88          box-sizing: inherit;
    89        }
    90  
    91        body
    92        {
    93          margin:0;
    94          background: #fafafa;
    95        }
    96      </style>
    97    </head>
    98  
    99    <body>
   100      <div id="swagger-ui"></div>
   101      <script src="` + bundlePath + `"> </script>
   102      <script src="//cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.17.4/swagger-ui-standalone-preset.js"> </script>
   103      <script>
   104      window.onload = function() {
   105        // Build a system
   106        var ui = SwaggerUIBundle({
   107          url: "` + apiPath + `",
   108          dom_id: '#swagger-ui',
   109          deepLinking: true,
   110          presets: [
   111            SwaggerUIBundle.presets.apis,
   112            SwaggerUIStandalonePreset
   113          ],
   114          plugins: [
   115            SwaggerUIBundle.plugins.DownloadUrl
   116          ],
   117          layout: "StandaloneLayout"
   118        })
   119  
   120        window.ui = ui
   121      }
   122    </script>
   123    </body>
   124  </html>`)
   125  
   126  	return html, nil
   127  }
   128  
   129  type SwaggerUIBundle struct {
   130  	httpx.MethodGet
   131  }
   132  
   133  func (SwaggerUIBundle) Path() string {
   134  	return "/swagger-ui-bundle.js"
   135  }
   136  
   137  func (s SwaggerUIBundle) Output(c context.Context) (interface{}, error) {
   138  	html := &httpx.HTML{}
   139  
   140  	b, err := Asset("swagger-ui-bundle.js")
   141  	if err != nil {
   142  		return nil, fmt.Errorf("asset: %v", err)
   143  	}
   144  
   145  	_, err = html.Write(b)
   146  	if err != nil {
   147  		return nil, fmt.Errorf("html write: %v", err)
   148  	}
   149  
   150  	return html, nil
   151  }