/
doc_controller.go
65 lines (49 loc) · 1.36 KB
/
doc_controller.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2017 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package controller
import (
"net/http"
server "github.com/euskadi31/go-server"
"github.com/hyperscale/hyperpic/cmd/hyperpic/app/asset"
"github.com/hyperscale/hyperpic/pkg/hyperpic/memfs"
)
type docController struct {
}
// NewDocController func
func NewDocController() server.Controller {
return &docController{}
}
// Mount endpoints
func (c docController) Mount(r *server.Router) {
r.AddRouteFunc("/docs/swagger.yaml", c.getSwaggerHandler).Methods(http.MethodGet)
r.AddRouteFunc("/docs/", c.getDocHandler).Methods(http.MethodGet)
}
// GET /docs/swagger.yaml
func (c docController) getSwaggerHandler(w http.ResponseWriter, r *http.Request) {
name := "docs/swagger.yaml"
w.Header().Set("Content-Type", "application/x-yaml")
body, _ := asset.Asset(name)
info, _ := asset.AssetInfo(name)
http.ServeContent(
w,
r,
info.Name(),
info.ModTime(),
memfs.NewBuffer(&body),
)
}
// GET /docs/
func (c docController) getDocHandler(w http.ResponseWriter, r *http.Request) {
name := "docs/index.html"
w.Header().Set("Content-Type", "text/html; charset=utf-8")
body, _ := asset.Asset(name)
info, _ := asset.AssetInfo(name)
http.ServeContent(
w,
r,
info.Name(),
info.ModTime(),
memfs.NewBuffer(&body),
)
}