/
api_doc.go
49 lines (36 loc) · 891 Bytes
/
api_doc.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
package routes
import (
"net/http"
"regexp"
"strings"
"github.com/KejawenLab/bima/v4/middlewares"
"google.golang.org/grpc"
)
const ApiDocPath = "/api/docs"
type ApiDoc struct {
Debug bool
}
func (a *ApiDoc) Path() string {
var path strings.Builder
path.WriteString(ApiDocPath)
path.WriteString("/{path}")
return path.String()
}
func (a *ApiDoc) Method() string {
return http.MethodGet
}
func (a *ApiDoc) SetClient(client *grpc.ClientConn) {}
func (a *ApiDoc) Middlewares() []middlewares.Middleware {
return nil
}
func (a *ApiDoc) Handle(w http.ResponseWriter, r *http.Request, _ map[string]string) {
if !a.Debug {
w.Write([]byte("Api doc not available"))
return
}
var path strings.Builder
path.WriteString(ApiDocPath)
path.WriteString("/")
regex := regexp.MustCompile(path.String())
http.ServeFile(w, r, regex.ReplaceAllString(r.URL.Path, "swaggers/"))
}