-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (143 loc) · 3.88 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
URY Off Air Studio Booking App
*/
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/UniversityRadioYork/myradio-go"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
_ "github.com/lib/pq"
)
var db *sql.DB
var myrSession *myradio.Session
var cookiestore = sessions.NewCookieStore([]byte("key"))
type CtxKey string
const UserCtxKey CtxKey = "user"
const AuthRealm string = "ury-off-air-bookings"
func initDB() {
// Replace with your PostgreSQL connection string
connectionString := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
os.Getenv("DBUSER"), os.Getenv("DBPASS"), os.Getenv("DBNAME"), os.Getenv("DBHOST"), os.Getenv("DBPORT"))
var err error
db, err = sql.Open("postgres", connectionString)
if err != nil {
panic(err)
}
// Check if the database connection is successful
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Connected to the database")
}
type Event struct {
ID int `json:"id"`
Type BookingType
Title string `json:"title"`
User int
Start string `json:"start"`
End string `json:"end"`
StartTime time.Time
EndTime time.Time
NoNameAttached bool `json:"noNameAttached"`
Repeat int `json:"repeat"`
}
func (e *Event) parseTimes() {
var err error
e.StartTime, err = time.Parse("2006-01-02T15:04", e.Start)
if err != nil {
panic(err)
// TODO
}
e.EndTime, err = time.Parse("2006-01-02T15:04", e.End)
if err != nil {
panic(err)
// TODO
}
}
func indexPage(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
func js(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "main.js")
}
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func info(w http.ResponseWriter, r *http.Request) {
createTypes := bookingsUserCanCreate(r.Context().Value(UserCtxKey).(int))
name := GetNameOfUser(r.Context().Value(UserCtxKey).(int))
commit := getBuildCommit()
json, err := json.Marshal(struct {
CreateTypes []BookingType
Name string
CommitHash string
UserCanCreateUnnamedEvents bool
}{
CreateTypes: createTypes,
Name: name,
CommitHash: commit,
UserCanCreateUnnamedEvents: isManagement(r.Context().Value(UserCtxKey).(int)),
})
if err != nil {
// TODO
panic(err)
}
w.Header().Add("Content-Type", "application/json")
w.Write(json)
}
func main() {
initDB()
var err error
myrSession, err = myradio.NewSessionFromKeyFile()
if err != nil {
// TODO
panic(err)
}
go myRadioTrainingSync()
r := mux.NewRouter()
r.HandleFunc("/", indexPage).Methods("GET")
r.HandleFunc("/main.js", js).Methods("GET")
r.HandleFunc("/create", createEvent).Methods("POST")
r.HandleFunc("/delete/{id}", deleteEvent).Methods("DELETE")
r.HandleFunc("/claim/{id}", claimEventForStation).Methods("PUT")
r.HandleFunc("/canModify/{id}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventID := vars["id"]
id, err := strconv.Atoi(eventID)
if err != nil {
// TODO
panic(err)
}
userID := r.Context().Value(UserCtxKey).(int)
json, err := json.Marshal(struct {
Delete bool
ClaimForStation bool
}{
Delete: hasPermissionToDelete(userID, id),
ClaimForStation: canClaimEventForStation(userID, id),
})
if err != nil {
// TODO
panic(err)
}
w.Header().Add("Content-Type", "application/json")
w.Write(json)
}).Methods("GET")
r.HandleFunc("/get", getEvents).Methods("GET")
r.HandleFunc("/auth", auth)
r.HandleFunc("/info", info).Methods("GET")
r.HandleFunc("/logout", logout)
r.HandleFunc("/favicon.ico", favicon).Methods("GET")
http.Handle("/", AuthHandler(r))
if err = http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}