-
Notifications
You must be signed in to change notification settings - Fork 4
/
doc.go
183 lines (155 loc) · 3.91 KB
/
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package jsonschema
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/caddyserver/caddy/v2"
)
func loadDoc() error {
if err := loadRootDoc(); err != nil {
return err
}
if err := fetchAllDocumentedModules(); err != nil {
return err
}
if err := fetchAllModuleDocs(); err != nil {
return err
}
return nil
}
// loadRootDoc loads the documentation for the root config structure.
func loadRootDoc() error {
b, err := fetchConfigDoc("")
if err != nil {
return err
}
if err := json.Unmarshal(b, &rootDocAPIResp); err != nil {
return err
}
if rootDocAPIResp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code %d from caddyserver.com", rootDocAPIResp.StatusCode)
}
return nil
}
// fetchAllDocumentedModules() fetches and populate flatCaddyDocMap
// with all available documented modules.
func fetchAllDocumentedModules() error {
// website json doc has repeated modules
// prevent cycle
visited := map[string]struct{}{}
// top level namespaces
for _, namespace := range rootDocAPIResp.Result.Namespaces[""] {
b, err := fetchNamespaceDoc(namespace.Name)
if err != nil {
fmt.Println("error fetching namespace", namespace.Name, ":", err)
continue
}
var tmp DocAPIResp
if err := json.Unmarshal(b, &tmp); err != nil {
fmt.Println("error unmarshaling namespace", namespace.Name, ":", err)
continue
}
if tmp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code %d from caddyserver.com", rootDocAPIResp.StatusCode)
}
flatCaddyDocMap[namespace.Name] = &tmp
// sub namespaces
for ns, list := range tmp.Result.Namespaces {
if ns == "" {
// avoid top level
continue
}
for _, m := range list {
modulePath := m.Name
if ns != "" {
modulePath = ns + "." + m.Name
}
// check if visited
if _, ok := visited[modulePath]; ok {
continue
}
// mark visited
visited[modulePath] = struct{}{}
flatCaddyDocMap[modulePath] = nil
}
}
}
return nil
}
// fetchJSONModuleDocs fetches docs for all available modules.
func fetchAllModuleDocs() error {
for ns, doc := range flatCaddyDocMap {
if doc != nil {
continue
}
b, err := fetchNamespaceDoc(ns)
if err != nil {
fmt.Println("error fetching namespace", ns, ":", err)
continue
}
var tmp DocAPIResp
if err := json.Unmarshal(b, &tmp); err != nil {
fmt.Println("error unmarshaling namespace", ns, ":", err)
continue
}
flatCaddyDocMap[ns] = &tmp
}
return nil
}
// fetchNamespaceDoc fetches the JSON doc for namespace
func fetchNamespaceDoc(namespace string) ([]byte, error) {
return fetchConfigDoc("apps/" + namespace)
}
// fetchConfigDoc fetchs the JSON doc for config. e.g. admin, logging
func fetchConfigDoc(config string) ([]byte, error) {
// try local cache first
cache, err := cacheFile(config)
if err == nil {
b, err := ioutil.ReadFile(cache)
if err == nil {
return b, nil
}
}
apiURL := "https://caddyserver.com/api/docs/config/" + config
label := config
if config == "" {
label = "root config"
}
if err == errCacheDisabled {
log.Println("discarding cache for", label+".")
} else {
log.Println("cached docs not found for", label+".")
}
log.Println("fetching", apiURL, "...")
resp, err := http.Get(apiURL)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body.Close()
// cache file
defer ioutil.WriteFile(cache, b, 0600)
log.Println()
return b, nil
}
var errCacheDisabled = errors.New("cache disabled")
// cacheFile returns the filesystem path to cached API doc
// for namespace
func cacheFile(namespace string) (string, error) {
if config.DiscardCache {
return "", errCacheDisabled
}
fileName := "docs.json"
dir := filepath.Join(caddy.AppDataDir(), "json_schema", namespace)
if err := os.MkdirAll(dir, 0700); err != nil {
return "", err
}
return filepath.Join(dir, fileName), nil
}