-
Notifications
You must be signed in to change notification settings - Fork 15
/
handlers.go
122 lines (106 loc) · 3.21 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
// Copyright 2016-2020 Vladimir Osintsev <[email protected]>
//
// This file is part of Tornote.
//
// Tornote is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tornote is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package tornote
import (
"encoding/base64"
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
)
// MainFormHandler renders main form.
func MainFormHandler(s *Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.renderTemplate(w, "index.html", map[string]interface{}{
csrf.TemplateTag: csrf.TemplateField(r),
})
})
}
// PublicFileHandler get file from bindata or return not found error.
func PublicFileHandler(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Path[1:]
http.ServeFile(w, r, uri)
}
// Return status for health checks.
func HealthStatusHandler(w http.ResponseWriter, r *http.Request) {
// TODO: Ping database connection
w.WriteHeader(http.StatusOK)
}
// ReadNoteHandler print encrypted data for client-side decrypt and destroy note.
func ReadNoteHandler(s *Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
raw, _ := base64.RawURLEncoding.DecodeString(vars["id"])
id, err := uuid.FromBytes(raw)
if err != nil {
http.NotFound(w, r)
return
}
n := &Note{UUID: id}
// Get encrypted n or return 404
err = s.db.Select(n)
if err != nil {
http.NotFound(w, r)
return
}
// Render "ready" template to user
s.renderTemplate(w, "note.html", nil)
})
}
// ReadRawNoteHandler print encrypted data for client-side decrypt and destroy note.
func ReadRawNoteHandler(s *Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
raw, _ := base64.RawURLEncoding.DecodeString(vars["id"])
id, err := uuid.FromBytes(raw)
if err != nil {
http.NotFound(w, r)
return
}
n := &Note{UUID: id}
// Get encrypted n or return 404
err = s.db.Select(n)
if err != nil {
http.NotFound(w, r)
return
}
// Deferred n deletion
defer func() {
s.db.Delete(n)
}()
// Print encrypted n to user
//s.renderTemplate(w, "note.html", string(n.Data))
w.Write(n.Data)
})
}
// CreateNoteHandler save secret note to persistent datastore and return note ID.
func CreateNoteHandler(s *Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n := &Note{
UUID: uuid.New(),
Data: []byte(r.FormValue("body")),
}
err := s.db.Insert(n)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, n.String())
})
}