forked from go-shiori/shiori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
312 lines (257 loc) · 7.49 KB
/
serve.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cmd
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"github.com/RadhiFadlillah/shiori/assets"
"github.com/RadhiFadlillah/shiori/model"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/bcrypt"
"html/template"
"io"
"mime"
"net/http"
fp "path/filepath"
"strings"
"time"
)
var (
jwtKey []byte
tplCache *template.Template
serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve web app for managing bookmarks.",
Long: "Run a simple annd performant web server which serves the site for managing bookmarks." +
"If --port flag is not used, it will use port 8080 by default.",
Run: func(cmd *cobra.Command, args []string) {
// Create JWT key
jwtKey = make([]byte, 32)
_, err := rand.Read(jwtKey)
if err != nil {
cError.Println("Failed to generate key for token")
return
}
// Prepare template
tplFile, _ := assets.ReadFile("cache.html")
tplCache, err = template.New("cache.html").Parse(string(tplFile))
if err != nil {
cError.Println("Failed to generate HTML template")
return
}
// Create router
router := httprouter.New()
router.GET("/js/*filepath", serveFiles)
router.GET("/res/*filepath", serveFiles)
router.GET("/css/*filepath", serveFiles)
router.GET("/webfonts/*filepath", serveFiles)
router.GET("/", serveIndexPage)
router.GET("/login", serveLoginPage)
router.GET("/bookmark/:id", serveBookmarkCache)
router.POST("/api/login", apiLogin)
router.GET("/api/bookmarks", apiGetBookmarks)
router.POST("/api/bookmarks", apiInsertBookmarks)
router.PUT("/api/bookmarks", apiUpdateBookmarks)
router.DELETE("/api/bookmarks", apiDeleteBookmarks)
// Route for panic
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, arg interface{}) {
http.Error(w, fmt.Sprint(arg), 500)
}
port, _ := cmd.Flags().GetInt("port")
url := fmt.Sprintf(":%d", port)
logrus.Infoln("Serve shiori in", url)
logrus.Fatalln(http.ListenAndServe(url, router))
},
}
)
func init() {
serveCmd.Flags().IntP("port", "p", 8080, "Port that used by server")
rootCmd.AddCommand(serveCmd)
}
func serveFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Read asset path
path := r.URL.Path
if path[0:1] == "/" {
path = path[1:]
}
// Load asset
asset, err := assets.ReadFile(path)
checkError(err)
// Set response header content type
ext := fp.Ext(path)
mimeType := mime.TypeByExtension(ext)
if mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
// Serve asset
buffer := bytes.NewBuffer(asset)
io.Copy(w, buffer)
}
func serveIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := checkToken(r)
if err != nil {
redirectPage(w, r, "/login")
return
}
asset, _ := assets.ReadFile("index.html")
w.Header().Set("Content-Type", "text/html")
buffer := bytes.NewBuffer(asset)
io.Copy(w, buffer)
}
func serveLoginPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := checkToken(r)
if err == nil {
redirectPage(w, r, "/")
return
}
asset, _ := assets.ReadFile("login.html")
w.Header().Set("Content-Type", "text/html")
buffer := bytes.NewBuffer(asset)
io.Copy(w, buffer)
}
func serveBookmarkCache(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Read param in URL
id := ps.ByName("id")
// Read bookmarks
bookmarks, err := DB.GetBookmarks(true, id)
checkError(err)
if len(bookmarks) == 0 {
panic(fmt.Errorf("No bookmark with matching index"))
}
// Read template
err = tplCache.Execute(w, &bookmarks[0])
checkError(err)
}
func apiLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Decode request
var request model.LoginRequest
err := json.NewDecoder(r.Body).Decode(&request)
checkError(err)
// Get account data from database
accounts, err := DB.GetAccounts(request.Username, true)
if err != nil || len(accounts) == 0 {
panic(fmt.Errorf("Account does not exist"))
}
// Compare password with database
account := accounts[0]
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(request.Password))
if err != nil {
panic(fmt.Errorf("Username and password don't match"))
}
// Calculate expiration time
nbf := time.Now()
exp := time.Now().Add(12 * time.Hour)
if request.Remember {
exp = time.Now().Add(7 * 24 * time.Hour)
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"nbf": nbf.Unix(),
"exp": exp.Unix(),
"sub": account.ID,
})
tokenString, err := token.SignedString(jwtKey)
checkError(err)
// Return token
fmt.Fprint(w, tokenString)
}
func apiGetBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Get query parameter
keyword := r.URL.Query().Get("keyword")
strTags := r.URL.Query().Get("tags")
tags := strings.Fields(strTags)
// Check token
err := checkAPIToken(r)
checkError(err)
// Fetch all bookmarks
bookmarks, err := DB.SearchBookmarks(true, keyword, tags...)
checkError(err)
err = json.NewEncoder(w).Encode(&bookmarks)
checkError(err)
}
func apiInsertBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := checkAPIToken(r)
checkError(err)
// Decode request
request := model.Bookmark{}
err = json.NewDecoder(r.Body).Decode(&request)
checkError(err)
// Save bookmark
book, err := addBookmark(request, false)
checkError(err)
// Return new saved result
err = json.NewEncoder(w).Encode(&book)
checkError(err)
}
func apiUpdateBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := checkAPIToken(r)
checkError(err)
// Decode request
request := model.Bookmark{}
err = json.NewDecoder(r.Body).Decode(&request)
checkError(err)
// Convert tags and ID
id := []string{fmt.Sprintf("%d", request.ID)}
tags := make([]string, len(request.Tags))
for i, tag := range request.Tags {
tags[i] = tag.Name
}
// Update bookmark
bookmarks, err := updateBookmarks(id, request.URL, request.Title, request.Excerpt, tags, false)
checkError(err)
// Return new saved result
err = json.NewEncoder(w).Encode(&bookmarks[0])
checkError(err)
}
func apiDeleteBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := checkAPIToken(r)
checkError(err)
// Decode request
request := []string{}
err = json.NewDecoder(r.Body).Decode(&request)
checkError(err)
// Delete bookmarks
_, _, err = DB.DeleteBookmarks(request...)
checkError(err)
fmt.Fprint(w, request)
}
func checkToken(r *http.Request) error {
tokenCookie, err := r.Cookie("token")
if err != nil {
return fmt.Errorf("Token does not exist")
}
token, err := jwt.Parse(tokenCookie.Value, jwtKeyFunc)
if err != nil {
return err
}
claims := token.Claims.(jwt.MapClaims)
return claims.Valid()
}
func checkAPIToken(r *http.Request) error {
token, err := request.ParseFromRequest(r, request.AuthorizationHeaderExtractor, jwtKeyFunc)
if err != nil {
return err
}
claims := token.Claims.(jwt.MapClaims)
return claims.Valid()
}
func jwtKeyFunc(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method")
}
return jwtKey, nil
}
func redirectPage(w http.ResponseWriter, r *http.Request, url string) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
http.Redirect(w, r, url, 301)
}