forked from osminogin/tornote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
103 lines (87 loc) · 2.91 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
// Copyright 2016 Vladimir Osintsev <[email protected]>
//
// This program 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.
//
// This program 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 <http://www.gnu.org/licenses/>.
// See the COPYING file in the main directory for details.
package tornote
import (
"crypto/rand"
"database/sql"
"encoding/base64"
"fmt"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
)
// frontPageHandler render home page.
func frontPageHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "index.html", nil)
}
// publicFileHandler get file from bindata or return not found error.
func publicFileHandler(w http.ResponseWriter, r *http.Request) {
data, err := Asset(r.URL.Path[1:])
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
// Set headers by file extension
switch filepath.Ext(r.URL.Path[1:]) {
case ".js":
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
case ".css":
w.Header().Set("Content-Type", "text/css")
}
w.Write(data)
}
// readNoteHandler print encrypted data for client-side decrypt and destroy note.
func readNoteHandler(db *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var encrypted string
vars := mux.Vars(r)
// Get encrypted note or return error
err := db.QueryRow("SELECT encrypted FROM notes WHERE id = ?", vars["id"]).Scan(&encrypted)
if err != nil {
http.NotFound(w, r)
return
}
// Deferred destroy note
defer func() {
db.Exec("DELETE FROM notes WHERE id = ?", vars["id"])
}()
// Print encrypted note to user
renderTemplate(w, "note.html", encrypted)
})
}
// saveNoteHandler save secret note to persistent datastore and return note ID.
func saveNoteHandler(db *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
encrypted := r.FormValue("body")
secret := make([]byte, 11)
// Generate random data for note id
_, err := rand.Read(secret)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Encode note id with URL safe format
id := base64.RawURLEncoding.EncodeToString(secret)
// Save data to database
_, err = db.Exec("INSERT INTO notes (id, encrypted) VALUES (?, ?)", id, encrypted)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprint(w, id)
})
}