-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
664 lines (605 loc) · 17.8 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"sort"
"strconv"
"strings"
"text/template"
"time"
// "github.com/gobwas/glob/util/strings"
// _ "github.com/mattn/go-sqlite3"
_ "modernc.org/sqlite"
"gopkg.in/cas.v2"
)
const (
port = ":8080"
layout = "2006-01-02 15:04:05"
)
// structure for receiving the user modifiable information
type config_vars struct {
Term string `json:"term"`
Year int `json:"year"`
RepoPath string `json:"repopath"`
}
var currentTerm string
var currentYear int
var lastTerm string
var lastYear int
var pathToBinary string
var pathToRepo string
var term_start string
var term_end string
// set path to binary
func setPathToBinary() {
e, _ := os.Executable() // retrieves the path to the binary including the filename itself
pathToBinary = path.Dir(e) + "/" // strips the filename only
}
// this sets/resets the term, year, and path variables
func setTermsYearsRepoPath() {
config := config_vars{}
configPath := pathToBinary + "config.json"
jsonFile, _ := os.Open(configPath)
file, _ := ioutil.ReadAll(jsonFile)
_ = json.Unmarshal(file, &config)
currentTerm = config.Term
currentYear = config.Year
pathToRepo = config.RepoPath
setLastTermandYear()
setTermStart()
setTermEnd()
}
func setLastTermandYear() {
if currentTerm == "Fall" {
lastTerm = "Spring"
lastYear = currentYear
} else if currentTerm == "Spring" {
lastTerm = "Fall"
lastYear = currentYear - 1
}
}
// converting term and year into layouts for time.Time
func setTermStart() {
if currentTerm == "Fall" {
term_start = strconv.Itoa(currentYear) + "-08-01 00:00:00"
} else if currentTerm == "Spring" {
term_start = strconv.Itoa(currentYear) + "-01-01 00:00:00"
}
}
func setTermEnd() {
if currentTerm == "Fall" {
term_end = strconv.Itoa(currentYear) + "-12-31 23:59:59"
} else if currentTerm == "Spring" {
term_end = strconv.Itoa(currentYear) + "-05-30 00:00:00"
}
}
// struct for housing the relevant data of a talk
type Talk struct {
Id int
Date time.Time
Upcoming bool
Year string
Month string
Month_name string
Day string
Date_string string
Time_string string
Speaker_first string
Speaker_last string
Speaker_url string
Affiliation string
Title string
Abstract string
Vid_conf_url string
Vid_conf_pw string
Recording_url string
Host string
Location string
}
// opens the datebase
func dbOpen(file string) (db *sql.DB) {
dbDriver := "sqlite"
db, err := sql.Open(dbDriver, pathToBinary+file+".db")
if err != nil {
log.Fatal(err)
}
return db
}
// parse template files
var tmpl = template.Must(template.ParseGlob(pathToBinary + "forms/*"))
// trash talk to provide some protection against undesired deletion
// var trash = Talk{}
func PrependHTTP(url string) string {
if len(url) == 0 {
return url
} else if (url[0:7] != "http://") && (url[0:8] != "https://") {
return "http://" + url
} else {
return url
}
}
// helper function that reads a sql query result into a slice of talks
func rowsToTalkSlice(rows *sql.Rows) (talksslice []Talk) {
talk := Talk{}
talks := []Talk{}
for rows.Next() {
var id int
var event_date, event_time, speaker_first, speaker_last, speaker_url, speaker_affiliation, title, abstract, vid_conf_url, vid_conf_pw, recording_url, host, location string
err := rows.Scan(&id, &event_date, &event_time, &speaker_first, &speaker_last, &speaker_url, &speaker_affiliation, &title, &abstract, &vid_conf_url, &vid_conf_pw, &recording_url, &host, &location)
if err != nil {
log.Fatal(err)
}
talk.Id = id
s := strings.Split(event_time, "-")
if len(s[0]) == 4 {
s[0] = "0" + s[0]
}
event_time = s[0]
string_time := event_date + "T" + event_time + ":00.000Z"
converted_time, err := time.Parse("2006-01-02T15:04:05.000Z", string_time)
if err != nil {
log.Fatal(err)
}
talk.Date = converted_time
talk.Upcoming = converted_time.After(time.Now())
talk.Date_string = talk.Date.Format("January 02 2006")
talk.Year = strconv.Itoa(converted_time.Year())
month_num := strconv.Itoa(int(converted_time.Month()))
if len(month_num) == 1 {
talk.Month = "0" + month_num
} else {
talk.Month = strconv.Itoa(int(converted_time.Month()))
}
talk.Month_name = talk.Date.Format("January")
talk.Day = strconv.Itoa(converted_time.Day())
talk.Time_string = converted_time.Format("15:04")
talk.Speaker_first = speaker_first
talk.Speaker_last = speaker_last
talk.Speaker_url = speaker_url
talk.Affiliation = speaker_affiliation
talk.Title = title
talk.Abstract = abstract
talk.Vid_conf_url = vid_conf_url
talk.Vid_conf_pw = vid_conf_pw
talk.Recording_url = recording_url
talk.Host = host
talk.Location = location
talks = append(talks, talk)
}
return talks
}
// reads the database into a slice of talks, converts to data for seminar page template, writes the resulting html to both the main index page and the relevant folder in the git repo, and finally calls the function to update the git repo
func WriteToHTML() {
setTermsYearsRepoPath() // update term and year
db := dbOpen("talks")
rows, err := db.Query("SELECT * FROM scagnt ORDER BY id DESC")
if err != nil {
log.Fatal(err)
}
talks := rowsToTalkSlice(rows)
sort.Slice(talks, func(i, j int) bool {
return talks[j].Date.After(talks[i].Date)
})
start_date, _ := time.Parse(layout, term_start)
end_date, _ := time.Parse(layout, term_end)
current_talks := []Talk{}
for _, talk := range talks {
if talk.Date.After(start_date) && talk.Date.Before(end_date) {
current_talks = append(current_talks, talk)
}
}
type page_talk_data struct {
Date string
Time string
URL string
Speaker string
Speaker_last string
Uni string
Title string
Host string
Abstract string
Location string
}
type page_data struct {
Term string
Year string
LastTermPath string
Talks []page_talk_data
Prefix string
}
page_talks := []page_talk_data{}
for _, talk := range current_talks {
talk_data := page_talk_data{}
talk_data.Date = talk.Date.Format("Monday, Jan 2")
talk_data.Time = talk.Date.Format("3:04PM")
talk_data.URL = talk.Speaker_url
talk_data.Speaker = talk.Speaker_first + " " + talk.Speaker_last
talk_data.Speaker_last = talk.Speaker_last
talk_data.Uni = talk.Affiliation
talk_data.Title = talk.Title
talk_data.Host = talk.Host
talk_data.Abstract = talk.Abstract
talk_data.Location = talk.Location
page_talks = append(page_talks, talk_data)
}
data := page_data{
Term: currentTerm,
Year: strconv.Itoa(currentYear),
LastTermPath: strconv.Itoa(lastYear) + "/" + strings.ToLower(lastTerm),
Talks: page_talks,
}
pathToTemplate := pathToBinary + "html/Seminar_Page.html"
t, err := template.ParseFiles(pathToTemplate)
if err != nil {
log.Println("Parsing index page template:", err)
return
}
index_path := pathToRepo + "index.html"
yearPath := pathToRepo + strconv.Itoa(currentYear)
termPath := yearPath + "/" + strings.ToLower(currentTerm)
termIndexPath := termPath + "/index.html"
writeIndex := func(path string) {
f, err := os.Create(path)
if err != nil {
log.Println("create file: ", err)
return
}
if path != index_path {
data.Prefix = "../../"
}
err = t.Execute(f, data)
if err != nil {
log.Println("execute: ", err)
return
}
f.Close()
}
writeIndex(index_path)
createDir := func(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.Mkdir(path, 0700)
if err != nil {
log.Println("creating directory: ", err)
}
}
}
createDir(yearPath) // need to create directories one level at a time apparently
createDir(termPath)
writeIndex(termIndexPath)
go updateRepo()
}
func updateRepo() {
gitPull := exec.Command("git", "pull")
gitPull.Dir = pathToRepo
output, err := gitPull.Output()
log.Printf("%s", output)
if err != nil {
log.Println("git pull: ", err)
}
gitAdd := exec.Command("git", "add", "-A")
gitAdd.Dir = pathToRepo
_, err = gitAdd.Output()
if err != nil {
log.Println("git add: ", err)
}
gitCommit := exec.Command("git", "commit", "-m", "'Auto update to repo'")
gitCommit.Dir = pathToRepo
_, err = gitCommit.Output()
if err != nil {
log.Println("git commit: ", err)
}
gitPush := exec.Command("git", "push")
gitPush.Dir = pathToRepo
output, err = gitPush.Output()
log.Printf("%s", output)
if err != nil {
log.Println("git push: ", err)
}
}
func Index(w http.ResponseWriter, r *http.Request) {
if !cas.IsAuthenticated(r) {
cas.RedirectToLogin(w, r)
return
}
db := dbOpen("talks")
rows, err := db.Query("SELECT * FROM scagnt ORDER BY id DESC")
if err != nil {
log.Fatal(err)
}
talks := rowsToTalkSlice(rows)
sort.Slice(talks, func(i, j int) bool {
return talks[j].Date.Before(talks[i].Date)
})
tmpl.ExecuteTemplate(w, "Index", talks)
defer db.Close()
}
func Show(w http.ResponseWriter, r *http.Request) {
db := dbOpen("talks")
nId := r.URL.Query().Get("id")
rows, err := db.Query("SELECT * FROM scagnt WHERE id=?", nId)
if err != nil {
log.Fatal(err)
}
talk := rowsToTalkSlice(rows)[0]
tmpl.ExecuteTemplate(w, "Show", talk)
defer db.Close()
}
func New(w http.ResponseWriter, r *http.Request) {
WriteToHTML()
tmpl.ExecuteTemplate(w, "New", nil)
}
func Edit(w http.ResponseWriter, r *http.Request) {
db := dbOpen("talks")
nId := r.URL.Query().Get("id")
rows, err := db.Query("SELECT * FROM scagnt WHERE id=?", nId)
if err != nil {
log.Fatal(err)
}
talk := rowsToTalkSlice(rows)[0]
tmpl.ExecuteTemplate(w, "Edit", talk)
defer db.Close()
}
func formToTalk(r *http.Request) (talk Talk) {
talk = Talk{}
talk.Abstract = r.FormValue("abstract")
talk.Affiliation = r.FormValue("speaker_affiliation")
month := r.FormValue("month")
day := r.FormValue("day")
if len(day) == 1 {
day = "0" + day
}
year := r.FormValue("year")
talk.Date_string = year + "-" + month + "-" + day
talk.Location = r.FormValue("location")
talk.Recording_url = PrependHTTP(r.FormValue("recording_url"))
talk.Host = r.FormValue("host")
talk.Title = r.FormValue("title")
if talk.Title == "" {
talk.Title = "TBA"
}
talk.Abstract = r.FormValue("abstract")
talk.Speaker_first = r.FormValue("speaker_first")
if talk.Speaker_first == "" {
talk.Speaker_first = "Reserved"
}
talk.Speaker_last = r.FormValue("speaker_last")
talk.Time_string = r.FormValue("time")
talk.Speaker_url = PrependHTTP(r.FormValue("speaker_url"))
talk.Vid_conf_url = PrependHTTP(r.FormValue("vid_conf_url"))
talk.Vid_conf_pw = r.FormValue("vid_conf_pw")
return talk
}
func Insert(w http.ResponseWriter, r *http.Request) {
db := dbOpen("talks")
if r.Method == "POST" {
talk := formToTalk(r)
insForm, err := db.Prepare("INSERT INTO scagnt (event_date, time, speaker_first, speaker_last, speaker_url, speaker_affiliation, title, abstract, vid_conf_url, vid_conf_pw, recording_url, host, location) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
log.Fatal(err)
}
insForm.Exec(talk.Date_string, talk.Time_string, talk.Speaker_first, talk.Speaker_last, talk.Speaker_url, talk.Affiliation, talk.Title, talk.Abstract, talk.Vid_conf_url, talk.Vid_conf_pw, talk.Recording_url, talk.Host, talk.Location)
log.Println("INSERT: Name: " + talk.Speaker_first + " " + talk.Speaker_last + " | Title: " + talk.Title)
}
defer db.Close()
WriteToHTML()
tmpl.ExecuteTemplate(w, "Insert", nil)
}
func Update(w http.ResponseWriter, r *http.Request) {
db := dbOpen("talks")
if r.Method == "POST" {
id := r.FormValue("uid")
talk := formToTalk(r)
insForm, err := db.Prepare("UPDATE scagnt SET event_date=?, time=?, speaker_first=? ,speaker_last=?, speaker_url=?, speaker_affiliation=?, title=?, abstract=?, vid_conf_url=?, vid_conf_pw=?, recording_url=?, host=?, location=? WHERE id=?")
if err != nil {
log.Fatal(err)
}
insForm.Exec(talk.Date_string, talk.Time_string, talk.Speaker_first, talk.Speaker_last, talk.Speaker_url, talk.Affiliation, talk.Title, talk.Abstract, talk.Vid_conf_url, talk.Vid_conf_pw, talk.Recording_url, talk.Host, talk.Location, id)
log.Println("UPDATE: Name: " + talk.Speaker_first + " " + talk.Speaker_last + " | Title: " + talk.Title)
}
defer db.Close()
WriteToHTML()
tmpl.ExecuteTemplate(w, "Update", nil)
}
func Delete(w http.ResponseWriter, r *http.Request) {
db := dbOpen("talks")
nId := r.URL.Query().Get("id")
rows, err := db.Query("SELECT * FROM scagnt WHERE id=?", nId)
if err != nil {
log.Fatal(err)
}
trash := rowsToTalkSlice(rows)[0]
log.Println("Pending deletion: " + trash.Speaker_first + " " + trash.Speaker_last + " | " + trash.Title)
tmpl.ExecuteTemplate(w, "Delete", trash)
defer db.Close()
}
func ConfirmDelete(w http.ResponseWriter, r *http.Request) {
// bin := dbOpen("trash")
// statement, err := bin.Prepare("CREATE TABLE scagnt (id INTEGER PRIMARY KEY, event_date TEXT, time TEXT, speaker_first TEXT, speaker_last TEXT, speaker_url TEXT, speaker_affiliation TEXT, title TEXT, abstract TEXT, vid_conf_url TEXT, vid_conf_pw TEXT, recording_url TEXT)")
// if err != nil {
// log.Fatal(err)
// }
// statement.Exec()
// // insForm, err := bin.Prepare("INSERT INTO talks(month,day,year,hour,minute,speaker,title) VALUES(?,?,?,?,?,?,?)")
// // if err != nil {
// // log.Fatal(err)
// // }
// // insForm.Exec(trash.Month, trash.Day, trash.year, 0, 0, trash.speaker, trash.title)
// // log.Println("INSERT: Name: " + trash.speaker + " | Title: " + trash.title)
db := dbOpen("talks")
nId := r.URL.Query().Get("id")
delForm, err := db.Prepare("DELETE FROM scagnt WHERE id=?")
if err != nil {
log.Fatal(err)
}
delForm.Exec(nId)
log.Println("DELETE")
defer db.Close()
WriteToHTML()
tmpl.ExecuteTemplate(w, "ConfirmDelete", nil)
}
func convert_date(exp_date string) time.Time {
dt_typed, err := time.Parse(layout, exp_date)
if err != nil {
log.Fatal(err)
}
return dt_typed
}
func check_token(token string) bool {
var result bool
if len(token) != 24 {
result = false
} else {
var token2, exp_date string
db := dbOpen("tokens")
defer db.Close()
err := db.QueryRow("SELECT * FROM tokens WHERE token=?", token).Scan(&token2, &exp_date)
if err == sql.ErrNoRows {
result = false
} else if err != nil {
log.Fatal(err)
} else {
exp_time := convert_date(exp_date)
if exp_time.After(time.Now()) {
result = false
}
result = true
}
}
return result
}
func auth(handler http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// user_token := r.Header.Get("scagnt-authorization")
var user_token string
cookie, err := r.Cookie("scagnt")
if err == nil {
user_token = cookie.Value
} else {
user_token = ""
}
if check_token(user_token) {
handler.ServeHTTP(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusUnauthorized)
}
})
}
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func String(length int) string {
return StringWithCharset(length, charset)
}
func Login(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "Login", nil)
}
func Attempt(w http.ResponseWriter, r *http.Request) {
// user_token := r.Header.Get("scagnt-authorization")
var user_token string
cookie, err := r.Cookie("scagnt")
if err == nil {
user_token = cookie.Value
} else {
user_token = ""
}
if check_token(user_token) {
tmpl.ExecuteTemplate(w, "Success", nil)
} else {
var authenticate bool
users := dbOpen("users")
var user, pw string
defer users.Close()
if r.Method == "POST" {
current_user := r.FormValue("user")
current_pw := r.FormValue("password")
log.Println("Authentication attempt:" + current_user + " " + current_pw)
err := users.QueryRow("SELECT * FROM users WHERE user=?", current_user).Scan(&user, &pw)
if err == sql.ErrNoRows {
fmt.Println("No user here")
authenticate = false
} else if err != nil {
log.Fatal(err)
} else {
if current_pw == pw {
authenticate = true
} else {
log.Println(current_pw)
log.Println(pw)
log.Println("Passwords don't match")
authenticate = false
}
}
}
if authenticate {
tokens := dbOpen("tokens")
defer tokens.Close()
token := String(24)
expire := time.Now().AddDate(0, 0, 1)
date := expire.Format(layout)
insForm, err := tokens.Prepare("INSERT INTO tokens(token,exp_date) VALUES(?,?)")
if err != nil {
log.Fatal(err)
}
insForm.Exec(token, date)
cookie := &http.Cookie{
Name: "scagnt",
Value: token,
}
http.SetCookie(w, cookie)
tmpl.ExecuteTemplate(w, "Success", nil)
} else {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
}
func setLogFile() {
file, err := os.OpenFile("logs", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(file)
}
var casURL = "https://casserver.herokuapp.com/cas"
// var casURL = "https://cas-qa.auth.sc.edu/cas"
func main() {
setLogFile()
pathToBinary = "/Users/matt/GitHub/server/"
setTermsYearsRepoPath()
url, _ := url.Parse(casURL)
client := cas.NewClient(&cas.Options{
URL: url,
})
fmt.Println("Server started")
log.Println("Server started on: http://localhost" + port)
http.Handle("/", client.HandleFunc(Index))
http.HandleFunc("/login", Login)
http.HandleFunc("/attempt", Attempt)
http.HandleFunc("/show", auth(Show))
http.HandleFunc("/new", auth(New))
http.HandleFunc("/edit", auth(Edit))
http.HandleFunc("/insert", auth(Insert))
http.HandleFunc("/update", auth(Update))
http.HandleFunc("/delete", auth(Delete))
http.HandleFunc("/confirmdeletionbesure", ConfirmDelete)
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal(err)
}
}