/
main.go
188 lines (154 loc) · 5.31 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
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
package main
import (
"html/template"
"io"
"net/http"
"os"
//"./sendemail"
"./consuela"
"log"
//"./api"
"fmt"
//"mime"
"github.com/dchest/uniuri"
"github.com/gorilla/sessions"
"github.com/gorilla/context"
)
var token = uniuri.New()
var store = sessions.NewCookieStore([]byte(token))
//Compile templates on start
var templates = template.Must(template.ParseFiles("tmpl/auth.html","tmpl/email.html", "tmpl/upload.html"))
//Display the named template
func display(w http.ResponseWriter, tmpl string, data interface{}) {
templates.ExecuteTemplate(w, tmpl+".html", data)
}
func credentialHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
//GET displays the upload form.
case "GET":
display(w, "auth", nil)
//POST takes the uploaded file(s) and saves it to disk.
case "POST":
fmt.Println(r)
// pass list to consuela to remove bad addresses
username := r.FormValue("username")
password := r.FormValue("password")
uri := "https://api.sendgrid.com/api/bounces.count.json?api_user=" + username + "&api_key=" + password
response, err := http.StatusText(uri)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp)
// existing session: Get() always returns a session, even if empty.
session, _ := store.Get(r, "session")
// Set some session values.
session.Values["username"] = username
session.Values["password"] = password
// Save it.
session.Save(r, w)
//Check if correct creds
// set unique token
w.Header().Set("StatusCode", "202")
w.Header().Set("Content Type", "application/json")
w.Header().Set("Body", "“message”: “success”")
w.Header().Set("Body", token)
fmt.Println(w)
//display success message.
display(w, "auth", "Authenticated!")
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func emailHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
//GET displays the upload form.
case "GET":
display(w, "email", nil)
//POST takes the uploaded file(s) and saves it to disk.
case "POST":
// existing session: Get() always returns a session, even if empty.
session, _ := store.Get(r, "session")
// pass list to consuela to remove bad addresses
email := r.FormValue("email")
emailToken := r.FormValue("token")
if emailToken == token {
session.Values["email"] = email
w.Header().Set("StatusCode", "202")
w.Header().Set("Content Type", "application/json")
w.Header().Set("Body", "“message”: “success”")
} else {
w.Header().Set("StatusCode", "401")
w.Header().Set("Content Type", "application/json")
}
fmt.Println("email: " + email)
//display success message.
display(w, "email", "Email added successfully.")
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
//This is where the action happens.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
//GET displays the upload form.
case "GET":
display(w, "upload", nil)
//POST takes the uploaded file(s) and saves it to disk.
case "POST":
// existing session: Get() always returns a session, even if empty.
session, _ := store.Get(r, "session")
//get the multipart reader for the request.
reader, err := r.MultipartReader()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//copy each part to destination.
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
// fmt.Println(form.Value("username"))
//if part.FileName() is empty, skip this iteration.
if part.FileName() == "" {
continue
}
dst, err := os.Create("/Users/sendgrid1/upload-files-go/" + part.FileName())
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, part); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// pass list to consuela to remove bad addresses
inputfile, err := os.Open("/Users/sendgrid1/upload-files-go/" + part.FileName())
if err != nil {
log.Fatal(err)
}
defer inputfile.Close()
update.Consuela(inputfile, session.Values["username"].(string), session.Values["password"].(string))
//unsubscribe.Add(dst, username, password)
//send output link in email using SendGrid
// emailRecipient := r.FormValue("email")
// fmt.Println(emailRecipient)
// email.SendEmail("kyle.w.kern@gmail.com")
}
//display success message.
display(w, "upload", "Upload successful.")
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func main() {
http.HandleFunc("/auth", credentialHandler)
http.HandleFunc("/email", emailHandler)
http.HandleFunc("/upload", uploadHandler)
//static file handler.
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
//Listen on port 8080
http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))
}