-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (116 loc) · 3.53 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
package main
import (
"encoding/json"
"html/template"
"io/ioutil"
"net/http"
"os"
"log"
"github.com/gorilla/pat"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/google"
)
type Source struct {
UpdateTime string `json:"updateTime"`
Type string `json:"type"`
}
type PhoneMetadata struct {
Primary bool `json:"primary"`
}
type Name struct {
DisplayName string `json:"displayName"`
}
type phoneNumber struct {
Metadata PhoneMetadata `json:"metadata"`
Value string `json:"value"`
}
type ContactMetadata struct {
Sources []Source `json:"sources"`
}
type Photo struct {
Default bool `json:"default"`
Url string `json:"url"`
}
type Contact struct {
Metadata ContactMetadata `json:"metadata"`
Names []Name `json:"names"`
PhoneNumbers []phoneNumber `json:"phoneNumbers"`
}
type Result struct {
Connections []Contact `json:"connections"`
TotalPeople int `json:"totalPeople"`
}
func main() {
url := os.Getenv("DOMAIN_URL")
if url == "" {
url = "http://localhost:3000"
}
goth.UseProviders(
google.New(os.Getenv("GOOGLE_CLIENT_ID"), os.Getenv("GOOGLE_CLIENT_SECRET"), os.Getenv("DOMAIN_URL")+"/auth/google/callback", "email", "profile", "https://www.googleapis.com/auth/contacts.readonly"),
)
p := pat.New()
p.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) {
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
http.Redirect(res, req, "/", http.StatusSeeOther)
println(err)
return
}
t, _ := template.ParseFiles("templates/success.html")
url := `https://people.googleapis.com/v1/people/me/connections?sortOrder=LAST_MODIFIED_DESCENDING&personFields=metadata&personFields=names&personFields=phoneNumbers`
reqContacts, _ := http.NewRequest("GET", url, nil)
reqContacts.Header.Set("Authorization", "Bearer "+user.AccessToken)
ans, err := http.DefaultClient.Do(reqContacts)
if err != nil {
http.Redirect(res, req, "/", http.StatusSeeOther)
println(err)
return
}
defer ans.Body.Close()
body, err := ioutil.ReadAll(ans.Body)
if err != nil {
http.Redirect(res, req, "/", http.StatusSeeOther)
println(err)
return
}
result := Result{}
json.Unmarshal(body, &result)
t.Execute(res, map[string]interface{}{
"Email": user.Email,
"Name": user.Name,
"Connections": result.Connections,
})
})
p.Get("/logout/{provider}", func(res http.ResponseWriter, req *http.Request) {
gothic.Logout(res, req)
res.Header().Set("Location", "/")
res.WriteHeader(http.StatusTemporaryRedirect)
})
p.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
gothic.BeginAuthHandler(res, req)
})
p.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
p.Get("/login", func(res http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("templates/login.html")
t.Execute(res, false)
})
p.Get("/privacy", func(res http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("templates/privacy.html")
t.Execute(res, false)
})
p.Get("/terms", func(res http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("templates/terms.html")
t.Execute(res, false)
})
p.Get("/", func(res http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("templates/index.html")
t.Execute(res, false)
})
log.Println("listening on localhost:3000")
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
log.Fatal(http.ListenAndServe(":"+port, p))
}