-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (102 loc) · 4.04 KB
/
main.go
File metadata and controls
128 lines (102 loc) · 4.04 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Author: Kévin Le
// Version 1.0
package main
import (
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"server/routes"
"firebase.google.com/go/messaging"
"server/global"
"strconv"
"server/auth"
)
func main() {
// Creating router
router := mux.NewRouter()
// Making unprotected subrouter
loginRouter := router.PathPrefix("/login").Subrouter()
loginRouter.HandleFunc("/checkmail/", routes.ExistingMail).Methods("POST")
loginRouter.HandleFunc("/signup/", routes.SignUp).Methods("POST")
loginRouter.HandleFunc("/signin/", routes.SignIn).Methods("POST")
loginRouter.HandleFunc("/forgot/", routes.Forgot).Methods("POST")
loginRouter.HandleFunc("/refreshToken/", auth.RefreshToken).Methods("GET")
// Making protected router with token middleware
protectedRouter := router.PathPrefix("/").Subrouter()
protectedRouter.Use(auth.VerifyToken)
protectedRouter.HandleFunc("/token/", AddOrUpdateToken).Methods("PUT")
protectedRouter.HandleFunc("/users/", routes.UpdateProfile).Methods("PUT")
protectedRouter.HandleFunc("/messages/", routes.GetMessages).Methods("GET")
protectedRouter.HandleFunc("/ws", handleConnections)
protectedRouter.HandleFunc("/events/", routes.GetEvents).Methods("GET")
protectedRouter.HandleFunc("/events/", routes.AddEvent).Methods("POST")
protectedRouter.HandleFunc("/events/", routes.UpdateEvent).Methods("PUT")
protectedRouter.HandleFunc("/events/", routes.CancelEvent).Methods("DELETE")
protectedRouter.HandleFunc("/forums/", routes.GetForums).Methods("GET")
protectedRouter.HandleFunc("/threads/", routes.GetThreads).Methods("GET")
protectedRouter.HandleFunc("/thread/", routes.GetThread).Methods("GET")
protectedRouter.HandleFunc("/thread/", routes.CreateThread).Methods("POST")
protectedRouter.HandleFunc("/post/", routes.AddPost).Methods("POST")
protectedRouter.HandleFunc("/appraisals/", routes.GetLastAppraisal).Methods("GET")
protectedRouter.HandleFunc("/appraisals/", routes.CreateAppraisal).Methods("POST")
protectedRouter.HandleFunc("/measurements/", routes.GetMeasurements).Methods("GET")
protectedRouter.HandleFunc("/measurements/", routes.CreateMeasurements).Methods("POST")
protectedRouter.HandleFunc("/tests/", routes.GetTests).Methods("GET")
protectedRouter.HandleFunc("/tests/", routes.CreateTest).Methods("POST")
protectedRouter.HandleFunc("/prescriptions/", routes.GetPrescriptions).Methods("GET")
protectedRouter.HandleFunc("/prescriptions/", routes.CreatePrescription).Methods("POST")
// Debug route
router.HandleFunc("/notification", DebugNotifications).Methods("GET")
go handleMessages()
// Listen on port 80
err := http.ListenAndServe(":80", router)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func AddOrUpdateToken(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
db := global.OpenDB()
defer db.Close()
// Get fields from request
userId, _ := strconv.Atoi(r.PostFormValue("userId"))
token := r.PostFormValue("token")
// Possible nil value
oldToken := r.PostFormValue("oldToken")
var query string
// Array of args for insert or update
args := make([]interface{}, 0)
if oldToken == "" {
// Add
query = "INSERT INTO tokens (userId, token) VALUES(?, ?)"
args = append(args, userId, token)
} else {
// Update
query = "UPDATE tokens SET token = ? WHERE token = ?"
args = append(args, token, oldToken)
}
// Inserting into DB
_, err := db.Exec(query, args...)
if err != nil {
db.Close()
print(err.Error())
global.SendError(w, "internal_error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func DebugNotifications(w http.ResponseWriter, r *http.Request) {
db := global.OpenDB()
defer db.Close()
tokensDictionary := global.GetTokens(db, 15)
notifications := make([]*messaging.Message, 0)
for _, tokens := range tokensDictionary {
for _, token := range tokens {
notification := global.DebugNotification(token)
notifications = append(notifications, notification)
}
}
global.SendNotifications(notifications...)
}