-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (97 loc) · 2.65 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
urlparser "parser/urlParser"
"strings"
"github.com/rs/cors"
)
func main() {
// add cors allowed all
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodGet, http.MethodPost},
AllowedHeaders: []string{"*"},
})
mux := http.NewServeMux()
mux.HandleFunc("/parse", parseHandler)
handler := c.Handler(mux)
fmt.Println("Server started on port 8080...")
http.ListenAndServe(":8080", handler)
}
func parseHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
return
}
link := strings.TrimSpace(string(body))
if link == "" {
http.Error(w, "Empty link provided", http.StatusBadRequest)
return
}
parsedURL, err := url.ParseRequestURI(link)
if err != nil || !parsedURL.IsAbs() {
http.Error(w, "Invalid URL provided", http.StatusBadRequest)
return
}
resp, err := http.Get(link)
if err != nil {
http.Error(w, fmt.Sprintf("Error fetching URL: %s", err.Error()), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Received non-200 status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}
urls, err := urlparser.ParseHTML(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Error parsing HTML: %s", err.Error()), http.StatusInternalServerError)
return
}
cleanedURLs := make([]map[string]interface{}, 0)
for _, url := range urls {
cleanedURL := make(map[string]interface{})
if url.Href != "" {
if !strings.HasPrefix(url.Href, "http") {
// Check if Href is relative
domain := parsedURL.Scheme + "://" + parsedURL.Host
if strings.HasPrefix(url.Href, "/") {
// Check if Href starts with a slash
url.Href = domain + url.Href
} else {
url.Href = domain + "/" + url.Href
}
}
cleanedURL["Href"] = url.Href
}
if url.Content != "" {
cleanedURL["Content"] = url.Content
}
if len(url.Images) > 0 {
cleanedURL["Images"] = url.Images
}
if len(url.Emails) > 0 {
cleanedURL["Emails"] = url.Emails
}
if len(cleanedURL) > 0 {
cleanedURLs = append(cleanedURLs, cleanedURL)
}
}
jsonResponse, err := json.Marshal(cleanedURLs)
if err != nil {
http.Error(w, "Error creating JSON response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonResponse)
}