-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
116 lines (100 loc) · 3.07 KB
/
handlers.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
114
115
116
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/buaazp/fasthttprouter"
"github.com/lab259/cors"
"github.com/valyala/fasthttp"
)
// Service is an http server that handles REST requests.
type Service struct {
db *DBConnection
Handler fasthttp.RequestHandler
}
// NewService creates a new instance of a Service.
func NewService(db *DBConnection) Service {
service := Service{db: db}
router := fasthttprouter.New()
router.GET("/domains/:domain", service.getDomain)
router.GET("/domains", service.getPreviousDomains)
service.Handler = cors.Default().Handler(router.Handler)
return service
}
func (s *Service) getDomain(ctx *fasthttp.RequestCtx) {
domainName, ok := ctx.UserValue("domain").(string)
if !ok {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
if isDomain := validateDomainName(domainName); !isDomain {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetBodyString("Invalid domain name received.")
return
}
if domainExists := validateDomainExists(domainName); !domainExists {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetBodyString(fmt.Sprintf("Domain %v doesn't exist.", domainName))
return
}
domain, _ := s.db.getDomain(domainName)
if domain.Name == "" {
// Get the domain's info and save to DB
domain, _ = getDomainData(domainName)
s.db.createDomain(domain)
} else if time.Since(domain.LastChecked) > time.Hour {
// Get the current state of the domain's servers
newDomain, _ := getDomainData(domainName)
newDomain.PreviousSSLGrade = domain.SSLGrade
// Check if servers have changed
hasChanged := domain.IsDown != newDomain.IsDown
oldServers := make(map[string]Server)
newServers := make(map[string]Server)
for _, server := range domain.Servers {
oldServers[server.Address] = server
}
for _, server := range newDomain.Servers {
newServers[server.Address] = server
}
// Check if old server is same as current or no longer exists
for _, server := range domain.Servers {
newServer, ok := newServers[server.Address]
if !ok {
s.db.deleteServer(server.Address)
hasChanged = true
} else if newServer != server {
s.db.updateServer(newServer)
hasChanged = true
}
}
// Check if new server didn't exist
for _, server := range newDomain.Servers {
_, ok := oldServers[server.Address]
if !ok {
s.db.createServer(server, domain.Name)
hasChanged = true
}
}
newDomain.ServersChanged = hasChanged
//Update domain's record in DB
domain = newDomain
fmt.Println(domain)
s.db.updateDomain(domain)
}
json.NewEncoder(ctx).Encode(&domain)
ctx.SetContentType("application/JSON")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func (s *Service) getPreviousDomains(ctx *fasthttp.RequestCtx) {
var response DomainsResponse
domains, _ := s.db.getDomains()
for _, domain := range domains {
var domainResponse DomainResponse
domainResponse.Info = domain
domainResponse.Name = domain.Name
response.Items = append(response.Items, domainResponse)
}
json.NewEncoder(ctx).Encode(&response)
ctx.SetContentType("application/JSON")
ctx.SetStatusCode(fasthttp.StatusOK)
}