-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
130 lines (107 loc) · 3.13 KB
/
handlers.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
package main
import (
"fmt"
"net/http"
"encoding/json"
"io"
"io/ioutil"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2/bson"
)
func UserHandler(w http.ResponseWriter, r *http.Request) {
session, _ := GlobalSessions.SessionStart(w, r)
defer session.SessionRelease(w)
RenderTemplate(w, "user", session.Get("profile"))
}
func IsAuthenticated(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
session, _ := GlobalSessions.SessionStart(w, r)
defer session.SessionRelease(w)
if session.Get("profile") == nil {
http.Redirect(w, r, "/", http.StatusMovedPermanently)
} else {
next(w, r)
}
}
func Index(w http.ResponseWriter, r *http.Request) {
List(w, r)
}
func Add(w http.ResponseWriter, r *http.Request) {
var note Note
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, ¬e); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
u, err := repo.FindUser("shiva")
if err != nil { panic(err) }
n := repo.InsertNote(u, note)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(n); err != nil {
panic(err)
}
}
type ErrorMessage struct {
status int
source string
title string
detail string
}
func (errMsg *ErrorMessage) Serialize(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(errMsg.status)
err := json.NewEncoder(w).Encode(*errMsg)
if err != nil {
panic(err)
}
}
func RaiseBadRequest(source, detail string) *ErrorMessage {
return &ErrorMessage{
http.StatusBadRequest,
source,
"Invalid request",
detail,
}
}
func Find(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
noteId := vars["noteId"]
if !bson.IsObjectIdHex(noteId) {
errMsg := RaiseBadRequest("Find Note", "Provided note id is not valid")
errMsg.Serialize(w, r)
return
}
// at this point, we can either find the note or not
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
noteObjId := bson.ObjectIdHex(noteId)
n, err := repo.FindNote(noteObjId)
if err != nil {
err = json.NewEncoder(w).Encode(err)
}
err = json.NewEncoder(w).Encode(n)
}
func Remove(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Remove")
}
func List(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
u, err := repo.FindUser("shiva")
if err != nil { panic(err) }
notes, err := repo.ListAllNotes(u)
if err != nil { panic(err) }
err = json.NewEncoder(w).Encode(notes)
if err != nil {
panic(err)
}
}