Skip to content

Commit 1bee6be

Browse files
committed
JSON endpoint (only for urls)
1 parent 08828f4 commit 1bee6be

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

cmd/server/handleJson.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ import (
99

1010
func handleJson(w http.ResponseWriter, r *http.Request, data any) {
1111

12-
b, err := json.Marshal(data)
12+
var b []byte
13+
var err error
14+
15+
if r.URL.Query().Get("pretty") != "" {
16+
b, err = json.MarshalIndent(data, "", " ")
17+
} else {
18+
b, err = json.Marshal(data)
19+
}
20+
1321
if err != nil {
1422
common.Logger.Error("json.Marshal failed", "error", err, "data", data)
1523
b = []byte("{\"success\":false,\"err\":\"json.Marshal failed\"}")

cmd/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func main() {
2828
http.HandleFunc("POST /upload.html", uploadPostHandler)
2929
http.HandleFunc("GET /url.html", urlGetHandler)
3030
http.HandleFunc("POST /url.html", urlPostHandler)
31+
http.HandleFunc("/url.json", urlJsonHandler)
3132
http.HandleFunc("GET /clipboard.html", clipboardGetHandler)
3233
http.HandleFunc("POST /clipboard.html", clipboardPostHandler)
3334

cmd/server/urlHandler.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
67
"net/http"
78

@@ -61,3 +62,68 @@ func urlPostHandler(w http.ResponseWriter, r *http.Request) {
6162
"Title": "URL Analysis Results",
6263
})
6364
}
65+
66+
func urlJsonHandler(w http.ResponseWriter, r *http.Request) {
67+
var url string
68+
69+
if r.Method == http.MethodPost {
70+
url = r.FormValue("url")
71+
} else {
72+
url = r.URL.Query().Get("url")
73+
}
74+
if url == "" {
75+
handleJson(w, r, map[string]interface{}{
76+
"success": false,
77+
"error": "url parameter missing",
78+
})
79+
return
80+
}
81+
82+
resp, err := http.Get(url)
83+
if err != nil {
84+
handleJson(w, r, map[string]interface{}{
85+
"success": false,
86+
"error": err.Error(),
87+
"source": url,
88+
})
89+
return
90+
}
91+
defer resp.Body.Close()
92+
93+
if resp.StatusCode != http.StatusOK {
94+
handleJson(w, r, map[string]interface{}{
95+
"success": false,
96+
"error": fmt.Sprintf("failed to fetch URL (http response=%d)", resp.StatusCode),
97+
"source": url,
98+
})
99+
return
100+
}
101+
102+
body, err := io.ReadAll(resp.Body)
103+
if err != nil {
104+
handleJson(w, r, map[string]interface{}{
105+
"success": false,
106+
"error": err.Error(),
107+
"source": url,
108+
})
109+
return
110+
}
111+
112+
svgInfo, svgErr := svgan.SvgCheck(common.Logger, body)
113+
if svgErr != nil {
114+
handleJson(w, r, map[string]interface{}{
115+
"success": false,
116+
"error": svgErr.Error(),
117+
"source": url,
118+
})
119+
return
120+
}
121+
122+
handleJson(w, r, map[string]interface{}{
123+
"success": true,
124+
"source": url,
125+
"size": len(body),
126+
"mime": resp.Header.Get("Content-Type"),
127+
"data": svgInfo,
128+
})
129+
}

0 commit comments

Comments
 (0)