-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb.go
175 lines (150 loc) · 4.92 KB
/
web.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"strings"
"text/template"
"github.com/bmizerany/mc"
"github.com/gorilla/mux"
)
var memcached *mc.Conn
var err error
func main() {
if m, err := mc.Dial("tcp", os.Getenv("MEMCACHEDCLOUD_SERVERS")); err == nil {
err := m.Auth(
os.Getenv("MEMCACHEDCLOUD_USERNAME"),
os.Getenv("MEMCACHEDCLOUD_PASSWORD"),
)
if err == nil {
memcached = m
}
}
router := mux.NewRouter()
router.HandleFunc("/", index)
router.HandleFunc("/compare/", redirectToHome)
router.HandleFunc("/compare", redirectToHome)
router.HandleFunc("/compare/{lang1}/{lang2}/", languages)
router.HandleFunc("/compare/{lang1}/{lang2}", redirectToSlash)
router.HandleFunc("/codeblock/{lang1}/{lang2}/{taskName}/", codeblocks)
router.HandleFunc("/codeblock/{lang1}/{lang2}/{taskName}", redirectToSlash)
router.HandleFunc("/codeblock/{lang1}/{lang2}/{taskGroup}/{taskName}/", codeblocks)
router.HandleFunc("/codeblock/{lang1}/{lang2}/{taskGroup}/{taskName}", redirectToSlash)
http.Handle("/", router)
port := os.Getenv("PORT")
if port == "" {
port = "5000"
}
log.Print("listening...")
http.ListenAndServe(":"+port, nil)
}
func index(w http.ResponseWriter, req *http.Request) {
var content []byte
content, _ = Asset("static/languages.json")
languages := make([]string, 0)
if err := json.Unmarshal(content, &languages); err != nil {
log.Print(err)
http.Error(w, "internal json parsing error", 505)
return
}
headerC := Context{Title: "Side-by-side programming languages comparisons"}
headerbytes, _ := Asset("static/header.html")
headerT := template.Must(template.New("header").Parse(string(headerbytes)))
headerT.Execute(w, headerC)
context := Context{Languages: languages}
indexbytes, _ := Asset("static/index.html")
t := template.Must(template.New("index").Parse(string(indexbytes)))
t.Execute(w, context)
}
func languages(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
log.Print(params)
// try to get the list of suitable tasks from the live rosetta code page
langs := map[int]string{1: params["lang1"], 2: params["lang2"]}
tasks, err := TasksForLanguages(langs)
if err != nil {
log.Print(err)
http.Error(w, "these languages are probably fake", 406)
return
} else if len(tasks) == 0 {
// if nothing was found, return all
tasks := make([]map[string]string, 0)
var content []byte
content, _ = Asset("static/tasks.json")
if err := json.Unmarshal(content, &tasks); err != nil {
log.Print(err)
http.Error(w, "internal json parsing error", 505)
return
}
}
// cache this, please
w.Header().Set("Cache-control", "public; max-age=5184000")
headerC := Context{Title: strings.Title(langs[1]) + " x " + strings.Title(langs[2]) + " side-by-side"}
headerbytes, _ := Asset("static/header.html")
headerT := template.Must(template.New("header").Parse(string(headerbytes)))
headerT.Execute(w, headerC)
context := Context{Lang1: params["lang1"], Lang2: params["lang2"], Tasks: tasks}
tasksbytes, _ := Asset("static/tasks.html")
t := template.Must(template.New("tasks").Parse(string(tasksbytes)))
t.Execute(w, context)
}
func codeblocks(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
log.Print(params)
taskName := params["taskName"]
if taskGroup, ok := params["taskGroup"]; ok {
taskName = taskGroup + "/" + taskName
}
lang1 := strings.ToLower(params["lang1"])
lang2 := strings.ToLower(params["lang2"])
langs := map[int]string{1: lang1, 2: lang2}
code := map[int]string{1: "", 2: ""}
// try to found the code in memcache
if memcached != nil {
for i := 1; i <= 2; i++ {
html, _, _, err := memcached.Get(taskName + "::" + langs[i])
if err == nil && html != "" {
code[i] += "<pre><code class=\"language-" + langs[i] + "\">" + html + "</code></pre>"
}
}
}
if len(code[1]) == 0 || len(code[2]) == 0 {
// nothing found on cache, search the html
code, err = CodeblockForTask(taskName, langs)
if err != nil {
http.Error(w, "couldn't parse rosetta code", 505)
return
}
}
if len(code[1]) == 0 || len(code[2]) == 0 {
http.Error(w, "code not found for these two languages", 404)
return
}
// save code for this task in memcached
if memcached != nil {
memcached.Set(taskName+"::"+langs[1], code[1], 0, 0, 1296000)
memcached.Set(taskName+"::"+langs[2], code[2], 0, 0, 1296000)
}
// cache this, please
w.Header().Set("Cache-control", "public; max-age=5184000")
context := Context{Lang1: code[1], Lang2: code[2]}
codeblockbytes, _ := Asset("static/codeblock.html")
t := template.Must(template.New("codeblock").Parse(string(codeblockbytes)))
t.Execute(w, context)
}
func redirectToSlash(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, req.URL.String()+"/", 301)
return
}
func redirectToHome(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/", 302)
return
}
type Context struct {
Title string
Lang1 string
Lang2 string
Tasks []map[string]string
Languages []string
}