-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
233 lines (220 loc) · 7.4 KB
/
main.go
File metadata and controls
233 lines (220 loc) · 7.4 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
type jsonPOSTInput struct {
Email string `json:"email"`
Status string `json:"status"`
Client string `json:"clientID"`
}
type jsonPUTInput struct {
Email []string `json:"emails"`
Status string `json:"status"`
Client string `json:"clientID"`
}
func getEmail(db *sql.DB, email string, client string) (isIn bool, status string, created time.Time) {
row := db.QueryRow(`SELECT email, status, created FROM blacklist WHERE email like ? and client like ?;`, email, client)
switch err := row.Scan(&email, &status, &created); err {
case sql.ErrNoRows:
isIn = false
case nil:
isIn = true
default:
isIn = false
panic(err)
}
return
}
func getEmails(db *sql.DB, client string) (emails []string, statusArray []string, createdArray []time.Time) {
var email string
var status string
var created time.Time
rows, err := db.Query(`SELECT email, status, created FROM blacklist WHERE client = ?;`, client)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(&email, &status, &created)
if err != nil {
panic(err)
}
emails = append(emails, email)
statusArray = append(statusArray, status)
createdArray = append(createdArray, created)
fmt.Println(email, status, created)
}
err = rows.Err()
if err != nil {
panic(err)
}
return
}
func resolveGet(requestURL []string, client string, db *sql.DB, w http.ResponseWriter) {
if len(requestURL) > 2 {
// email address is specified
email := requestURL[2]
re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
if re.MatchString(email) {
isIn, status, created := getEmail(db, email, client)
if isIn {
w.Write([]byte(`{"status": "` + status + `", "email": "` + email + `", "created": "` + created.String() + `"}`))
} else {
email := "@" + strings.Split(email, "@")[1]
isIn, status, created := getEmail(db, email, client)
if isIn {
w.Write([]byte(`{"status": "` + status + `", "email": "` + email + `", "created": "` + created.String() + `"}`))
} else {
w.Write([]byte(`{"status": "not in blacklist", "email": "` + email + `"}`))
}
}
} else {
w.Write([]byte(`{"status": "not in blacklist", "email": "` + email + `"}`))
}
} else {
// email address is not specified all emails in response
emails, status, created := getEmails(db, client)
outJSON := `{"emails":[`
for index := range emails {
outJSON += `{"status": "` + status[index] + `", "email": "` + emails[index] + `", "created": "` + created[index].String() + `"},`
}
if outJSON[len(outJSON)-1:] == "," {
outJSON = strings.TrimSuffix(outJSON, ",")
}
outJSON += `]}`
w.Write([]byte(outJSON))
}
}
func resolvePost(body []byte, postInput jsonPOSTInput, db *sql.DB, w http.ResponseWriter) {
blckIn, _ := db.Prepare("INSERT INTO blacklist(email,status,client,created) VALUES(?,?,?,?)")
if len(body) > 3 {
er := json.Unmarshal(body, &postInput)
if er != nil {
log.Fatal(er)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"code": "400", "status": "Bad request", "message": "Failed to add email to black list, data input is not correct."}}`))
return
}
email := postInput.Email
client := postInput.Client
isIn, _, created := getEmail(db, email, client)
if isIn {
w.Write([]byte(`{"status": "Email was already added to blacklist", "email": "` + email + `", "created": "` + created.String() + `"}`))
} else {
time := time.Now()
status := postInput.Status
client := postInput.Client
blckIn.Exec(email, status, client, time)
w.Write([]byte(`{"status": "Email was added to blacklist", "email": "` + email + `"}`))
}
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"code": "400", "status": "Bad request", "message": "Failed to add email to black list, data input is not correct."}}`))
return
}
}
func resolvePut(body []byte, postInput jsonPUTInput, db *sql.DB, w http.ResponseWriter) {
blckIn, _ := db.Prepare("INSERT INTO blacklist(email,status,client,created) VALUES(?,?,?,?)")
if len(body) > 3 {
er := json.Unmarshal(body, &postInput)
if er != nil {
log.Fatal(er)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"code": "400", "status": "Bad request", "message": "Failed to add email to black list, data input is not correct."}}`))
return
}
outJSON := `{"items": [`
email := postInput.Email
client := postInput.Client
for index := range email {
isIn, _, _ := getEmail(db, email[index], client)
if isIn {
outJSON += `{ "email": "` + email[index] + `", "status": "Email was already added to blacklist"},`
} else {
time := time.Now()
status := postInput.Status
client := postInput.Client
blckIn.Exec(email[index], status, client, time)
outJSON += `{"email": "` + email[index] + `","status": "Email was added to blacklist"},`
}
}
if outJSON[len(outJSON)-1:] == "," {
outJSON = strings.TrimSuffix(outJSON, ",")
}
outJSON += `]}`
w.Write([]byte(outJSON))
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"code": "400", "status": "Bad request", "message": "Failed to add email to black list, data input is not correct."}}`))
return
}
}
func resolveDelete(requestURL []string, client string, db *sql.DB, w http.ResponseWriter) {
delblck, _ := db.Prepare(`DELETE FROM blacklist WHERE email like ? and client like ?;`)
email := requestURL[2]
isIn, _, _ := getEmail(db, email, client)
if isIn {
delblck.Exec(email, client)
w.Write([]byte(`{"status": "deleted", "email": "` + email + `"}`))
} else {
w.Write([]byte(`{"status": "Email is not in blacklist", "email": "` + email + `"}`))
}
}
func main() {
db, err := sql.Open("mysql", "user@blacklist557:Admmin147@tcp(blacklist557.mysql.database.azure.com)/blacklist?parseTime=true")
if err != nil {
log.Fatal(err)
}
db.SetMaxOpenConns(10)
var postInput jsonPOSTInput
var putInput jsonPUTInput
//main function
APIRoot := func(w http.ResponseWriter, req *http.Request) {
var client string
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error": {"code": "500", "status": "Error", "message": "Request failed."}}`))
return
} else {
requestURL := strings.Split(req.URL.Path, "/")
if requestURL[1] == "blacklist" {
if len(req.Header["Clientid"]) > 0 {
client = req.Header["Clientid"][0]
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"code": "400", "status": "Bad request", "message": "Client ID missing, data input is not correct."}}`))
return
}
switch req.Method {
case "GET":
resolveGet(requestURL, client, db, w)
case "POST":
resolvePost(body, postInput, db, w)
case "PUT":
resolvePut(body, putInput, db, w)
case "DELETE":
resolveDelete(requestURL, client, db, w)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(`{"error": {"code": "405", "status": "Forbiden", "message": "Method not allowed!"}}`))
}
} else {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(`{"error": {"code": "403", "status": "Forbiden", "message": "Access denied!"}}`))
}
}
}
http.HandleFunc("/", APIRoot)
http.ListenAndServe(":80", nil)
}