-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroutes.go
More file actions
114 lines (94 loc) · 2.3 KB
/
routes.go
File metadata and controls
114 lines (94 loc) · 2.3 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"text/template"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
"github.com/markbates/goth/gothic"
)
func SetupRoutes(r *gin.Engine, s *socketio.Server) {
//Oauth Authenticaton and Callbacks
r.GET("/auth/github/callback", providerCallback)
r.GET("/auth/github", providerAuth)
//Index Route
r.GET("/", indexHandler)
//Socket.io Route
r.GET("/socket.io/", func(c *gin.Context) {
s.ServeHTTP(c.Writer, c.Request)
})
//Api endpoints
a := r.Group("api")
a.GET("/activity", getActivity)
a.GET("/users", getUsers)
}
func indexHandler(c *gin.Context) {
//Configure default index file location
indexTemplate := *indexFile
if len(os.Getenv("INDEX_FILE")) > 0 {
indexTemplate = os.Getenv("INDEX_FILE")
}
t, err := template.ParseFiles(indexTemplate)
if err != nil {
panic(err)
}
t.Execute(c.Writer, templates)
}
func getActivity(c *gin.Context) {
a := ActivityList{}
err := a.List()
if err != nil {
c.JSON(400, gin.H{"error": err})
}
c.JSON(200, a)
}
func getUsers(c *gin.Context) {
u := UserList{}
err := u.List()
if err != nil {
c.JSON(400, gin.H{"error": err})
}
c.JSON(200, u)
}
func providerCallback(c *gin.Context) {
// Run user auth using the gothic library
user, err := gothic.CompleteUserAuth(c.Writer, c.Request)
checkErr(err, "Failed to authenicate user")
u := User{}
err = u.GetByUsername(user.RawData["login"].(string))
if err != nil {
if err != sql.ErrNoRows {
log.Fatalln("Failed to read from user table", err)
return
}
}
//Add user to the user table
u.Name = user.Name
u.Username = user.RawData["login"].(string)
u.AvatarUrl = user.AvatarURL
u.AccessToken = user.AccessToken
u.ProfileUrl = user.RawData["url"].(string)
u.Email = user.Email
u.Joined = user.RawData["created_at"].(string)
u.Raw = user.RawData
if u.Id != 0 {
u.UpdateTime()
_, err = dbmap.Update(&u)
checkErr(err, "Failed to update user row")
} else {
err = u.Create()
checkErr(err, "Failed to create new user row")
//Add the user's go routine
StartUserRoutine(u, activityChan)
}
c.JSON(200, u)
}
func providerAuth(c *gin.Context) {
gothic.GetProviderName = getProviderName
gothic.BeginAuthHandler(c.Writer, c.Request)
}
func getProviderName(req *http.Request) (string, error) {
return "github", nil
}