forked from utkarsh-1905/time-table
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (131 loc) · 3.71 KB
/
main.go
File metadata and controls
152 lines (131 loc) · 3.71 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"os"
"sort"
"strings"
"log"
"github.com/MicrosoftStudentChapter/time-table/utils"
)
func init() {
fmt.Println("Initializing server...")
// UNCOMMENT THIS TO RE-GENERATE THE TIMETABLE
// BE CAUTIOUS WHEN USING THIS
// utils.GetSubjectMapping()
// utils.GenerateJson()
fmt.Println("Server initialized")
}
func main() {
dataFile, _ := os.Open("./data.json")
data := make(map[string]map[string][][]utils.Data)
byteRes, _ := io.ReadAll(dataFile)
json.Unmarshal([]byte(byteRes), &data)
defer dataFile.Close()
table, _ := template.ParseFiles("./templates/table.html")
courseNameCode, _ := template.ParseFiles("./templates/course-name-code.html")
errorPage, _ := template.ParseFiles("./templates/error.html")
type HomeData struct {
Sheets []string
Classes map[string][]string
}
var sheets []string
for i := range data {
sheets = append(sheets, strings.Trim(i, " "))
}
sort.StringSlice(sheets).Sort()
classes := make(map[string][]string)
for i, d := range data {
temp := make([]string, 0)
for j := range d {
temp = append(temp, strings.Trim(j, " "))
}
sort.StringSlice(temp).Sort()
classes[i] = temp
}
h := HomeData{
Sheets: sheets,
Classes: classes,
}
// maintenance, _ := template.ParseFiles("./templates/maintenance.html")
home, _ := template.ParseFiles("./templates/home.html")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
errorPage.Execute(w, "This page is under construction !!(404)")
return
}
// COMMENT/UNCOMMENT BELOW TO SWITCH BETWEEN MAINTENANCE AND HOME PAGE
err := home.Execute(w, h)
// err := maintenance.Execute(w, nil)
if err != nil {
log.Printf("Error while executing home template: %v", err)
}
})
type TimeTableData struct {
Data [][]utils.Data
ClassName string
}
http.HandleFunc("/timetable", func(w http.ResponseWriter, r *http.Request) {
class := r.URL.Query().Get("classname")
sheet := r.URL.Query().Get("sheet")
flag := true
for _, d := range classes[sheet] {
if class == d {
flag = false
}
}
if flag {
errorPage.Execute(w, "Invalid category/class combination")
return
}
data := TimeTableData{
Data: data[sheet][class],
ClassName: class,
}
table.Execute(w, data)
})
// handler to serve add course page
http.HandleFunc("/course", func(w http.ResponseWriter, r *http.Request) {
courseNameCode.Execute(w, h)
})
http.HandleFunc("/api/classes", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(h)
})
http.HandleFunc("/api/timetable", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
class := r.URL.Query().Get("classname")
if class == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{
"error": "Missing required query parameter: 'classname'",
})
return
}
var tableData [][]utils.Data
for _, sheetClasses := range data {
if td, ok := sheetClasses[class]; ok {
tableData = td
break
}
}
if tableData == nil {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{
"error": "Class '" + class + "' not found",
})
return
}
mobileData := utils.TransformToMobileFormat(tableData)
json.NewEncoder(w).Encode(mobileData)
})
fs := http.FileServer(http.Dir("assets/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Println("Server Running at http://localhost:5000")
utils.HandleError(http.ListenAndServe(":5000", nil))
}