/
http.go
115 lines (102 loc) · 2.27 KB
/
http.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
func serveDocs(ctx context.Context, addr string) error {
fmt.Println("serving api docs")
var specBuf *bytes.Buffer
specBuf, err := OpenAPIYAML()
if err != nil {
return err
}
go watchSpecFiles(ctx, specBuf, &err)
s := &http.Server{
Addr: addr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/qri_api.yaml":
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
w.Write(specBuf.Bytes())
default:
w.Write([]byte(redocTemplate))
}
}),
}
return s.ListenAndServe()
}
const redocTemplate = `<!DOCTYPE html>
<html>
<head>
<title>ReDoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='./qri_api.yaml'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
</body>
</html>`
func watchSpecFiles(ctx context.Context, specBuf *bytes.Buffer, yamlErr *error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
buf, yamlE := OpenAPIYAML()
*specBuf = *buf
*yamlErr = yamlE
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
case <-ctx.Done():
done <- true
}
}
}()
// add doc template
err = watcher.Add("./api_doc_template.yaml")
if err != nil {
log.Fatal(err)
}
// watch everything in the lib directory
files, err := ioutil.ReadDir("../lib")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
path := filepath.Join("../lib", f.Name())
watcher.Add(path)
}
<-done
}