-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
217 lines (181 loc) · 5.31 KB
/
app.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
package main
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
//"go.mongodb.org/mongo-driver/mongo/options"
)
const (
HomeColumns = 3
ProbColumns = 3
)
var baseTemplates = []string{
"templates/layout/footer.tmpl",
"templates/layout/header.tmpl",
"templates/layout/base.tmpl",
}
// Models
type ProblemSetOverview struct {
Name string `bson:"problem_set_name"`
Description string `bson:"problem_set_description"`
}
type ProblemSetProblems struct {
Name string `bson:"problem_name"`
Id string `bson:"problem_id"`
}
type ProblemSet struct {
Name string `bson:"name"`
Description string `bson:"description"`
Problems []*ProblemSetProblems `bson:"problems"`
}
func (p ProblemSet) TemplateProblems() [][]*ProblemSetProblems {
var results [][]*ProblemSetProblems
for row := 0; row <= len(p.Problems)/ProbColumns; row++ {
var rowResult []*ProblemSetProblems
for col := 0; col < ProbColumns && (row*ProbColumns+col) < len(p.Problems); col++ {
rowResult = append(rowResult, p.Problems[row*HomeColumns+col])
}
results = append(results, rowResult)
}
return results
}
type Problem struct {
Name string `bson:"problem_name"`
Id string `bson:"problem_id"`
Set string `bson:"problem_set"`
NextProblem string `bson:"next_problem"`
PrevProblem string `bson:"prev_problem"`
Description string `bson:"problem_description"`
StartCode string `bson:"problem_start_code"`
ProbTests []*string `bson:"problem_tests"`
}
func (p Problem) DescriptionSplit() []string {
return strings.Split(p.Description, "\n")
}
type DataType interface{}
type ProblemTest struct {
Inputs []*DataType
Output DataType
}
// Handlers
func homeHandler(w http.ResponseWriter, r *http.Request) {
homeTemplates := append(baseTemplates, "templates/home.tmpl")
t, err := template.ParseFiles(homeTemplates...)
if err != nil {
fmt.Println(err)
return
}
// Get Problem Sets
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
if err != nil {
log.Println(err)
}
defer client.Disconnect(context.TODO())
var problemSets []*ProblemSetOverview
problemSetsColl := client.Database("goding-bat").Collection("problem_sets")
cur, err := problemSetsColl.Find(context.TODO(), bson.D{})
defer cur.Close(context.TODO())
for cur.Next(context.TODO()) {
var probSet ProblemSetOverview
err := cur.Decode(&probSet)
if err != nil {
log.Println(err)
}
problemSets = append(problemSets, &probSet)
}
if err := cur.Err(); err != nil {
log.Println(err)
}
var results [][]*ProblemSetOverview
for row := 0; row <= len(problemSets)/HomeColumns; row++ {
var rowResult []*ProblemSetOverview
for col := 0; col < HomeColumns && (row*HomeColumns+col) < len(problemSets); col++ {
rowResult = append(rowResult, problemSets[row*HomeColumns+col])
}
results = append(results, rowResult)
}
err = t.ExecuteTemplate(w, "home.tmpl", results)
if err != nil {
fmt.Println(err.Error())
}
}
func setHandler(w http.ResponseWriter, r *http.Request) {
// Get problem set
vars := mux.Vars(r)
probSetName := vars["setName"]
// Check and Get Problem Set
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
if err != nil {
log.Println(err)
}
defer client.Disconnect(context.TODO())
var problemSet ProblemSet
problemMapColl := client.Database("goding-bat").Collection("problem_map")
filter := bson.D{{"name", probSetName}}
err = problemMapColl.FindOne(context.TODO(), filter).Decode(&problemSet)
if err != nil {
log.Println(err)
http.NotFound(w, r)
return
}
setTemplates := append(baseTemplates, "templates/problem_set.tmpl")
t, err := template.ParseFiles(setTemplates...)
if err != nil {
fmt.Println(err)
return
}
err = t.ExecuteTemplate(w, "problem_set.tmpl", problemSet)
if err != nil {
fmt.Println(err.Error())
}
}
func probHandler(w http.ResponseWriter, r *http.Request) {
// Get problem info
vars := mux.Vars(r)
probId := vars["probId"]
// Check and Get Problem Set
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
if err != nil {
log.Println(err)
}
defer client.Disconnect(context.TODO())
var problem Problem
problemColl := client.Database("goding-bat").Collection("problem_info")
// TODO: change problem_id in mongo to bot include /prob/
filter := bson.D{{"problem_id", "/prob/" + probId}}
err = problemColl.FindOne(context.TODO(), filter).Decode(&problem)
if err != nil {
log.Println(err)
http.NotFound(w, r)
return
}
// Massage data
//fmt.Println(problem)
probTemplates := append(baseTemplates, "templates/problem.tmpl")
t, err := template.ParseFiles(probTemplates...)
if err != nil {
fmt.Println(err)
return
}
if err != nil {
fmt.Println(err.Error())
}
err = t.ExecuteTemplate(w, "problem.tmpl", problem)
}
func main() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
routes := mux.NewRouter()
routes.HandleFunc("/", homeHandler)
//routes.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
routes.HandleFunc("/prob/{probId:p[0-9]+}", probHandler)
routes.HandleFunc("/{setName:[a-zA-z]+\\-[0-9]+}", setHandler)
http.Handle("/", routes)
http.ListenAndServe(":8080", nil)
}