-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.go
116 lines (95 loc) · 1.97 KB
/
convert.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
package main
import (
"io"
"log"
"net/http"
"github.com/jsightapi/jsight-schema-core/fs"
"github.com/jsightapi/jsight-api-core/kit"
)
func convertJSight(w http.ResponseWriter, r *http.Request) {
to := r.FormValue("to")
format := r.FormValue("format")
log.Printf("%s %s %s %s", r.Method, r.URL.Path, to, format)
if getBoolEnv("JSIGHT_SERVER_CORS") {
cors(w)
}
wr := httpResponseWriter{writer: w}
switch r.Method {
case http.MethodOptions:
case http.MethodPost:
convertJSightPOST(wr, r)
return
default:
wr.errorStr("HTTP POST request required")
return
}
}
func convertJSightPOST(wr httpResponseWriter, r *http.Request) {
to := r.FormValue("to")
format := r.FormValue("format")
body, err := io.ReadAll(r.Body)
if err != nil {
wr.error(err)
return
}
jAPI, jErr := kit.NewJApiFromFile(fs.NewFile("root", body))
if getBoolEnv("JSIGHT_SERVER_STATISTICS") {
clientID := r.Header.Get("X-Browser-UUID")
clientIP := getIP(r)
sendDatagram(clientID, clientIP, len(body), jAPI, jErr)
}
if jErr != nil {
wr.error(jErr)
return
}
switch to {
case "jdoc-2.0":
switch format {
case "json", "":
writeJDocJSON(wr, jAPI)
return
default:
wr.errorStr("not supported format")
return
}
case "openapi-3.0.3":
switch format {
case "json", "":
writeOpenapiJSON(wr, jAPI)
return
case "yaml":
writeOpenapiYAML(wr, jAPI)
return
default:
wr.errorStr("not supported format")
return
}
default:
wr.errorStr(`you must specify the "to" parameter`)
return
}
}
func writeJDocJSON(wr httpResponseWriter, jAPI kit.JApi) {
resp, err := jdocJSON(jAPI)
if err != nil {
wr.error(err)
return
}
wr.jdocJSON(resp)
}
func writeOpenapiJSON(wr httpResponseWriter, jAPI kit.JApi) {
resp, err := openapiJSON(jAPI)
if err != nil {
wr.error(err)
return
}
wr.json(resp)
}
func writeOpenapiYAML(wr httpResponseWriter, jAPI kit.JApi) {
resp, err := openapiYAML(jAPI)
if err != nil {
wr.error(err)
return
}
wr.yaml(resp)
}