-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (107 loc) · 3.47 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"encoding/json"
"regexp"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/google"
"gorm.io/gorm"
"gorm.io/driver/sqlite"
)
var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_SECRET")))
var db, err = gorm.Open(sqlite.Open("bot/sq.db"), &gorm.Config{})
func CallbackHandler(res http.ResponseWriter, req *http.Request) {
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
fmt.Fprintln(res, err)
return
}
session, _ := store.Get(req, "session-name")
session.Values["email"] = user.Email
fmt.Println(session.Values["email"])
err = session.Save(req, res)
res.Header().Set("Location", "/")
res.WriteHeader(http.StatusTemporaryRedirect)
}
func AuthHandler(res http.ResponseWriter, req *http.Request) {
if user, err := gothic.CompleteUserAuth(res, req); err == nil {
fmt.Println(user)
} else {
gothic.BeginAuthHandler(res, req)
}
}
func ProfileHandler(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session-name")
email := session.Values["email"]
if email == nil {
res.Write([]byte("false"))
} else {
match, _ := regexp.MatchString("@umn\\.edu$", email.(string))
if match {
res.Write([]byte("true"))
} else {
res.Write([]byte("false"))
}
}
}
type Guild struct {
Link string
Name string
ServerId string
IconHash string
Range string
}
func ServeGuilds(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session-name")
email := session.Values["email"]
if email == nil {
res.Write([]byte(""))
} else {
match, _ := regexp.MatchString("umn\\.edu$", email.(string))
if match == false {
return
}
var guilds []Guild
db.Find(&guilds)
res.Header().Set("Content-Type", "application/json")
json.NewEncoder(res).Encode(guilds)
}
}
func RedirectHandler(w http.ResponseWriter, req *http.Request) {
target := "https://" + req.Host + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}
http.Redirect(w, req, target, http.StatusPermanentRedirect)
}
func main() {
db.AutoMigrate(&Guild{})
http.Handle("/", http.FileServer(http.Dir("static/")))
port := os.Getenv("PORT")
redirect := "http://localhost:"+port+"/api/callback/google"
if os.Getenv("BUILD") == "PROD" {
redirect = "https://studentrun.chat/api/callback/google"
}
goth.UseProviders(
google.New(os.Getenv("GOOGLE_KEY"), os.Getenv("GOOGLE_SECRET"), redirect),
)
r := mux.NewRouter()
r.HandleFunc("/api/callback/{provider}", CallbackHandler)
r.HandleFunc("/api/auth/{provider}", AuthHandler)
r.HandleFunc("/api/profile", ProfileHandler)
r.HandleFunc("/api/guilds", ServeGuilds)
http.Handle("/api/", r)
fmt.Println("listening on localhost:" + port)
if os.Getenv("BUILD") == "PROD" {
go http.ListenAndServe(":80", http.HandlerFunc(RedirectHandler))
log.Fatal(http.ListenAndServeTLS(":" + port, "/etc/letsencrypt/live/studentrun.chat-0001/fullchain.pem", "/etc/letsencrypt/live/studentrun.chat-0001/privkey.pem", nil))
} else {
log.Fatal(http.ListenAndServe(":" + port, nil))
}
}