-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
77 lines (51 loc) · 1.35 KB
/
handlers.go
File metadata and controls
77 lines (51 loc) · 1.35 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
)
type gist_json struct {
User string `json:"user"`
}
func RootHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("RootHandler started and redirecting to the index page")
t, _ := template.ParseFiles(INDEX)
t.Execute(w, nil)
}
func QueryHandler(w http.ResponseWriter, r *http.Request) {
//Retrieve the HTML form parameter of POST method
user := r.FormValue("github-user")
g, err := GetGist(user)
if err != nil {
log.Printf("GetGistError: %s, %v",
err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t, _ := template.ParseFiles(INDEX)
t.Execute(w, g)
if _, exists := GlobalStore[user]; !exists {
GlobalStore[user] = g.Counter
}
}
func ApiHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var request gist_json
err := decoder.Decode(&request)
g, err := GetGist(request.User)
if err != nil {
log.Printf("GetGistError: %s, %v",
err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, exists := GlobalStore[request.User]; !exists {
GlobalStore[request.User] = g.Counter
}
jsonData, _ := json.Marshal(g.Gist)
fmt.Fprint(w, bytes.NewBuffer(jsonData))
return
}