-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (40 loc) · 1.12 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
package main
import (
"encoding/json"
"fmt"
"main/util"
http "github.com/useflyent/fhttp"
)
func main() {
http.HandleFunc("/api/detect", detectHandler)
fmt.Println("Server is running on port 5000")
http.ListenAndServe(":5000", nil)
}
func detectHandler(w http.ResponseWriter, r *http.Request) {
// Handle CORS
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
var payload struct {
URL string `json:"url"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
if payload.URL == "" {
http.Error(w, "URL parameter is missing", http.StatusBadRequest)
return
}
services, err := util.ParseWebsite(payload.URL)
if err != nil {
http.Error(w, fmt.Sprintf("Error parsing website: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(services)
}