-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (66 loc) · 1.86 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
import "github.com/boushley/go-family-search/client"
import "github.com/gorilla/sessions"
var cli *client.Client
var config Configuration
var store *sessions.CookieStore
func main() {
loadConfig()
initClient()
store = sessions.NewCookieStore([]byte("aoiajdsio@309FA)023-aglkj"))
http.HandleFunc("/login", Login)
http.HandleFunc("/familySearch/auth", OAuthReturn)
http.HandleFunc("/recent-people", ShowRecentPeople)
http.Handle("/", http.FileServer(http.Dir("public")))
fmt.Println("Listening on port 8080")
http.ListenAndServe(":8080", nil)
}
func Login(rw http.ResponseWriter, r *http.Request) {
fmt.Println("Handling Login")
authEndpoint := cli.GetAuthorizationEndpoint("http://localhost:8080/familySearch/auth")
http.Redirect(rw, r, authEndpoint, http.StatusTemporaryRedirect)
}
func OAuthReturn(rw http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
token := cli.GetTokenFromCode(code)
session, err := store.Get(r, "auth")
handleError(err)
session.Values["accessToken"] = token
err = session.Save(r, rw)
handleError(err)
http.Redirect(rw, r, "/recent-people", http.StatusTemporaryRedirect)
}
func ShowRecentPeople(w http.ResponseWriter, r *http.Request) {
history := cli.GetCurrentUserHistory(getToken(r))
fmt.Println(history)
}
func handleError(err error) {
if err != nil {
log.Fatal(err)
}
}
func loadConfig() {
file, err := os.Open("private-conf.json")
handleError(err)
decoder := json.NewDecoder(file)
config = Configuration{}
err = decoder.Decode(&config)
handleError(err)
}
func initClient() {
conf := client.Config{config.DeveloperKey}
cli = client.NewClient(client.Sandbox, conf)
cli.Initialize()
}
func getToken(r *http.Request) string {
session, err := store.Get(r, "auth")
handleError(err)
return session.Values["accessToken"].(string)
}