-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
76 lines (61 loc) · 1.7 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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/eser/sanitizer-svc/email"
)
type SanitizeResponse struct {
IsValid bool `json:"isValid"`
Sanitized string `json:"sanitized"`
IsDisposable bool `json:"isDisposable"`
}
func sanitizeHandler(disposableList map[string]struct{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Only allow GET requests
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Get email from query parameters
emailParam := r.URL.Query().Get("email")
if emailParam == "" {
http.Error(w, "Email parameter is required", http.StatusBadRequest)
return
}
isValid := email.Validate(emailParam)
var sanitized string
var isDisposable bool
if isValid {
sanitized = email.Sanitize(emailParam)
isDisposable = email.IsDisposable(disposableList, sanitized)
}
response := SanitizeResponse{
IsValid: isValid,
Sanitized: sanitized,
IsDisposable: isDisposable,
}
// Set content type header
w.Header().Set("Content-Type", "application/json")
// Encode and send response
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Error encoding response", http.StatusInternalServerError)
return
}
}
}
func main() {
disposableList, err := email.LoadDisposableList("disposable_email_blocklist.conf")
if err != nil {
log.Fatal(err)
}
// Create a new ServeMux
mux := http.NewServeMux()
// Register routes
mux.HandleFunc("/sanitize", sanitizeHandler(disposableList))
// Start server
log.Println("Server starting on :8080")
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Fatal(err)
}
}