-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (56 loc) · 1.39 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
package main
import (
"errors"
"log"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/unrolled/render"
)
var renderer = render.New()
type ErrorResponse struct {
Error string `json:"error"`
}
func main() {
if err := startServer(); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
func startServer() error {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
r.Mount("/email", EmailRoutes())
return http.ListenAndServe(":3000", r)
}
func EmailRoutes() chi.Router {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Post("/check", checkEmail)
return r
}
func checkEmail(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
domain, err := getDomainFromEmail(email)
if err != nil {
renderer.JSON(w, http.StatusBadRequest, ErrorResponse{Error: err.Error()})
return
}
domainChecks, err := checkDomain(domain)
if err != nil {
renderer.JSON(w, http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
return
}
log.Printf("Domain checks: %+v\n", domainChecks)
renderer.JSON(w, http.StatusOK, domainChecks)
}
func getDomainFromEmail(email string) (string, error) {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return "", errors.New("Invalid email address format")
}
return parts[1], nil
}