Skip to content

Commit c2336cd

Browse files
committed
Split into multiple packages and improve error handling
1 parent d701856 commit c2336cd

File tree

3 files changed

+127
-65
lines changed

3 files changed

+127
-65
lines changed

domain/domain.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package domain
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
)
9+
10+
type Domain struct {
11+
domains map[string]bool
12+
}
13+
14+
func NewDomain() (*Domain, error) {
15+
var err error
16+
17+
domainsJsonFile, err := os.Open("domains.json")
18+
19+
if err != nil {
20+
return nil, fmt.Errorf("an error occurred while opening the domains file: %v", err)
21+
}
22+
23+
byteValue, err := ioutil.ReadAll(domainsJsonFile)
24+
25+
if err != nil {
26+
return nil, fmt.Errorf("an error occurred while reading the domains file: %v", err)
27+
}
28+
29+
var domainsSlice []string
30+
31+
err = json.Unmarshal(byteValue, &domainsSlice)
32+
33+
if err != nil {
34+
return nil, fmt.Errorf("an error occurred while parsing the domains file: %v", err)
35+
}
36+
37+
domains := make(map[string]bool)
38+
39+
for _, domain := range domainsSlice {
40+
domains[domain] = true
41+
}
42+
43+
return &Domain{
44+
domains: domains,
45+
}, nil
46+
}
47+
48+
func (d *Domain) GetIsDisposable(emailOrDomain string) bool {
49+
return d.domains[emailOrDomain]
50+
}

http/server.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package http
2+
3+
import (
4+
"encoding/json"
5+
"github.com/julienschmidt/httprouter"
6+
"log"
7+
"net/http"
8+
"strings"
9+
)
10+
11+
type domain interface {
12+
GetIsDisposable(string) bool
13+
}
14+
15+
type Server struct {
16+
domain domain
17+
}
18+
19+
type ErrorResponse struct {
20+
Detail string `json:"detail"`
21+
}
22+
23+
type Response struct {
24+
IsDisposable bool `json:"isDisposable"`
25+
}
26+
27+
func NewServer(d domain) Server {
28+
return Server{
29+
domain: d,
30+
}
31+
}
32+
33+
func (s *Server) index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
34+
queryParams := r.URL.Query()
35+
email := queryParams.Get("email")
36+
37+
w.Header().Set("Content-Type", "application/json")
38+
39+
if email == "" {
40+
errorResponse, _ := json.Marshal(ErrorResponse{
41+
Detail: "email query parameter should be set.",
42+
})
43+
44+
w.WriteHeader(400)
45+
w.Write(errorResponse)
46+
47+
return
48+
}
49+
50+
parts := strings.Split(email, "@")
51+
emailDomain := parts[len(parts)-1]
52+
53+
isDisposable := s.domain.GetIsDisposable(emailDomain)
54+
55+
jsonBytes, _ := json.Marshal(Response{
56+
IsDisposable: isDisposable,
57+
})
58+
59+
w.Write(jsonBytes)
60+
}
61+
62+
func (s *Server) Run() {
63+
router := httprouter.New()
64+
65+
router.GET("/", s.index)
66+
67+
log.Println("Starting to listen on 0.0.0.0:80...")
68+
log.Fatal(http.ListenAndServe(":80", router))
69+
}

main.go

+8-65
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,19 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"github.com/julienschmidt/httprouter"
6-
"io/ioutil"
4+
"github.com/th0th/is-email-disposable/domain"
5+
"github.com/th0th/is-email-disposable/http"
76
"log"
8-
"net/http"
9-
"os"
10-
"strings"
117
)
128

13-
type ErrorResponse struct {
14-
Detail string `json:"detail"`
15-
}
16-
17-
type Response struct {
18-
IsDisposable bool `json:"isDisposable"`
19-
}
20-
21-
var domains []string
22-
23-
func handler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
24-
queryParams := r.URL.Query()
25-
26-
email := queryParams.Get("email")
27-
28-
w.Header().Set("Content-Type", "application/json")
29-
30-
if email == "" {
31-
errorResponse, _ := json.Marshal(ErrorResponse{
32-
Detail: "email query parameter should be set.",
33-
})
34-
35-
w.WriteHeader(400)
36-
w.Write(errorResponse)
37-
38-
return
39-
}
40-
41-
parts := strings.Split(email, "@")
42-
domain := parts[len(parts)-1]
43-
44-
isDisposable := Find(domains, domain)
45-
46-
jsonBytes, _ := json.Marshal(Response{
47-
IsDisposable: isDisposable,
48-
})
49-
50-
w.Write(jsonBytes)
51-
}
52-
539
func main() {
54-
domainsJsonFile, _ := os.Open("domains.json")
55-
56-
byteValue, _ := ioutil.ReadAll(domainsJsonFile)
10+
d, err := domain.NewDomain()
5711

58-
_ = json.Unmarshal(byteValue, &domains)
59-
60-
router := httprouter.New()
61-
62-
router.GET("/", handler)
63-
64-
log.Println("Starting to listen on 0.0.0.0:80...")
65-
log.Fatal(http.ListenAndServe(":80", router))
66-
}
67-
68-
func Find(slice []string, val string) bool {
69-
for _, item := range slice {
70-
if item == val {
71-
return true
72-
}
12+
if err != nil {
13+
log.Panic(err)
7314
}
7415

75-
return false
16+
server := http.NewServer(d)
17+
18+
server.Run()
7619
}

0 commit comments

Comments
 (0)