Skip to content

Commit

Permalink
Simplify scores routing
Browse files Browse the repository at this point in the history
  • Loading branch information
JRaspass committed Nov 22, 2017
1 parent 338391a commit 8973297
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 71 deletions.
11 changes: 8 additions & 3 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ func init() {
Router.GET("/callback", callback)
Router.GET("/favicon.ico", asset)
Router.GET("/logout", middleware.Gzip(logout))
Router.GET("/scores/:hole/:lang", middleware.Gzip(scores))
Router.GET("/users/:user", middleware.Gzip(user))

for holeName := range preambles {
Router.GET("/"+holeName, middleware.Gzip(hole))
for _, h := range holes {
if h[0] != "all" {
Router.GET("/"+h[0], middleware.Gzip(hole))
}

for _, l := range langs {
Router.GET("/scores/"+h[0]+"/"+l[0], middleware.Gzip(scores))
}
}

Router.POST("/solution", middleware.Gzip(solution))
Expand Down
108 changes: 40 additions & 68 deletions routes/scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,50 @@ package routes
import (
"database/sql"
"net/http"
"strings"

"github.com/julienschmidt/httprouter"
)

var holes = [][]string{
{"all", "All Holes"},
{"99-bottles-of-beer", "99 Bottles of Beer"},
{"arabic-to-roman", "Arabic to Roman"},
{"emirp-numbers", "Emirp Numbers"},
{"evil-numbers", "Evil Numbers"},
{"fibonacci", "Fibonacci"},
{"fizz-buzz", "Fizz Buzz"},
{"happy-numbers", "Happy Numbers"},
{"odious-numbers", "Odious Numbers"},
{"pascals-triangle", "Pascal's Triangle"},
{"pernicious-numbers", "Pernicious Numbers"},
{"prime-numbers", "Prime Numbers"},
{"quine", "Quine"},
{"roman-to-arabic", "Roman to Arabic"},
{"sierpiński-triangle", "Sierpiński Triangle"},
{"seven-segment", "Seven Segment"},
{"spelling-numbers", "Spelling Numbers"},
{"π", "π"},
{"φ", "φ"},
{"𝑒", "𝑒"},
}

var langs = [][]string{
{"all", "All Langs"},
{"bash", "Bash"},
{"javascript", "JavaScript"},
{"perl", "Perl"},
{"perl6", "Perl 6"},
{"php", "PHP"},
{"python", "Python"},
{"ruby", "Ruby"},
}

func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
hole := ps[0].Value
lang := ps[1].Value

// TODO Find a better way to do this.
switch hole {
case "all",
"99-bottles-of-beer",
"arabic-to-roman",
"emirp-numbers",
"evil-numbers",
"fibonacci",
"fizz-buzz",
"happy-numbers",
"odious-numbers",
"pascals-triangle",
"pernicious-numbers",
"prime-numbers",
"quine",
"roman-to-arabic",
"seven-segment",
"sierpiński-triangle",
"spelling-numbers",
"π",
"φ",
"𝑒":
default:
print404(w, r)
return
}
parts := strings.Split(r.URL.Path, "/")

// TODO Find a better way to do this.
switch lang {
case "all", "bash", "javascript", "perl", "perl6", "php", "python", "ruby":
default:
print404(w, r)
return
}
hole := parts[2]
lang := parts[3]

userID := printHeader(w, r, 200)

Expand All @@ -53,28 +55,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
"></script><article id=scores><select id=hole>",
))

for _, v := range [][]string{
{"all", "All Holes"},
{"99-bottles-of-beer", "99 Bottles of Beer"},
{"arabic-to-roman", "Arabic to Roman"},
{"emirp-numbers", "Emirp Numbers"},
{"evil-numbers", "Evil Numbers"},
{"fibonacci", "Fibonacci"},
{"fizz-buzz", "Fizz Buzz"},
{"happy-numbers", "Happy Numbers"},
{"odious-numbers", "Odious Numbers"},
{"pascals-triangle", "Pascal's Triangle"},
{"pernicious-numbers", "Pernicious Numbers"},
{"prime-numbers", "Prime Numbers"},
{"quine", "Quine"},
{"roman-to-arabic", "Roman to Arabic"},
{"sierpiński-triangle", "Sierpiński Triangle"},
{"seven-segment", "Seven Segment"},
{"spelling-numbers", "Spelling Numbers"},
{"π", "π"},
{"φ", "φ"},
{"𝑒", "𝑒"},
} {
for _, v := range holes {
w.Write([]byte("<option "))
if hole == v[0] {
w.Write([]byte("selected "))
Expand All @@ -84,16 +65,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

w.Write([]byte("</select><select id=lang>"))

for _, v := range [][]string{
{"all", "All Langs"},
{"bash", "Bash"},
{"javascript", "JavaScript"},
{"perl", "Perl"},
{"perl6", "Perl 6"},
{"php", "PHP"},
{"python", "Python"},
{"ruby", "Ruby"},
} {
for _, v := range langs {
w.Write([]byte("<option "))
if lang == v[0] {
w.Write([]byte("selected "))
Expand Down

0 comments on commit 8973297

Please sign in to comment.