Repository navigation
Expand file tree
/
Copy pathroutes.go
More file actions
113 lines (95 loc) · 2.74 KB
/
Copy pathroutes.go
File metadata and controls
113 lines (95 loc) · 2.74 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
package main
import (
"html/template"
"log"
"net/http"
"github.com/gorilla/websocket"
"github.com/markbates/goth/gothic"
)
// serveWs upgrades the HTTP connection to a WebSocket and registers
// a Client (and basically just starts the whole thing!)
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error: %v", err)
return
}
user, err := getUser(r, "facebook")
if err != nil {
log.Printf("Error: %v", err)
return
}
client := &Client{
hub: hub,
conn: conn,
send: make(chan Message),
user: user,
}
client.hub.register <- client
go client.write()
client.read()
}
// home handles the / route
// It looks first if there's a session by getting the User then either
// displays the login or the chat screen.
func home(w http.ResponseWriter, r *http.Request) {
_, err := getUser(r, "facebook")
if err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
} else {
http.Redirect(w, r, "/chat", http.StatusTemporaryRedirect)
}
}
func login(w http.ResponseWriter, r *http.Request) {
T := initT(r.Header.Get("Accept-Language"), "en")
_, err := getUser(r, "facebook")
if err != nil {
t, _ := template.New("login.html").Funcs(template.FuncMap{"T": T}).ParseFiles("./templates/login.html")
t.Execute(w, nil)
} else {
http.Redirect(w, r, "/chat", http.StatusTemporaryRedirect)
}
}
func chat(w http.ResponseWriter, r *http.Request) {
T := initT(r.Header.Get("Accept-Language"), "en")
user, err := getUser(r, "facebook")
if err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
} else {
t, _ := template.New("chat.html").Funcs(template.FuncMap{"T": T}).ParseFiles("./templates/chat.html")
t.Execute(w, user)
}
}
// authCallback handles the callback (duh!) and always redirects to the
// home handler
func authCallback(w http.ResponseWriter, r *http.Request) {
_, err := gothic.CompleteUserAuth(w, r)
if err != nil {
log.Printf("Error: %v", err)
return
}
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
// logout handles the ... logout!
// It doesn't seem to really work at the moment, but well, maybe one day right?
func logout(w http.ResponseWriter, r *http.Request) {
err := gothic.Logout(w, r)
if err != nil {
log.Printf("Error: %v", err)
return
}
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
// auth handles the authentication through Facebook and then redirects to the
// home handler
func auth(w http.ResponseWriter, r *http.Request) {
if _, err := gothic.CompleteUserAuth(w, r); err == nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
} else {
gothic.BeginAuthHandler(w, r)
}
}