/
http.go
58 lines (47 loc) · 1.31 KB
/
http.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
package bookmarks
import (
"net/http"
"net/url"
"path"
"github.com/go-chi/chi/v5"
"github.com/readeck/readeck/internal/server"
"github.com/readeck/readeck/pkg/zipfs"
)
// SetupRoutes mounts the routes for the bookmarks domain.
// "/bm" is a public route outside the api scope in order to avoid
// sending the session cookie.
func SetupRoutes(s *server.Server) {
// Routes
// Saved bookmark resources (images & all)
s.AddRoute("/bm", mediaRoutes(s))
// API routes
api := newBookmarkAPI(s)
s.AddRoute("/api/bookmarks", api)
// Website routes
s.AddRoute("/bookmarks", newBookmarkViews(api))
}
// mediaRoutes serves files from a bookmark's saved archive. It reads
// directly from the zip file and returns the requested file's content.
func mediaRoutes(_ *server.Server) http.Handler {
r := chi.NewRouter()
r.Get("/{prefix:[a-zA-Z0-9]{2}}/{fname:[a-zA-Z0-9]+}/{p:^(img|_resources)$}/{name}", func(w http.ResponseWriter, r *http.Request) {
p := path.Join(
chi.URLParam(r, "p"),
chi.URLParam(r, "name"),
)
p = path.Clean(p)
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
zipfile := path.Join(
StoragePath(),
chi.URLParam(r, "prefix"),
chi.URLParam(r, "fname")+".zip",
)
fs := zipfs.HTTPZipFile(zipfile)
fs.ServeHTTP(w, r2)
})
return r
}