-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
212 lines (190 loc) · 4.62 KB
/
Copy pathmain.go
File metadata and controls
212 lines (190 loc) · 4.62 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"log"
"maps"
"net/http"
"net/netip"
"os"
"slices"
"strings"
"time"
bcclient "blobcache.io/blobcache/client/go"
"blobcache.io/blobcache/src/bchttp"
"blobcache.io/blobcache/src/bcsdk"
"blobcache.io/blobcache/src/blobcache"
"blobcache.io/blobcache/src/schema"
"blobcache.io/blobcache/src/schema/bcns"
"blobcache.io/glfs"
"blobcache.io/glfs/glfsiofs"
"go.brendoncarroll.net/star"
)
func main() {
star.Main(rootCmd)
}
var rootCmd = star.Command{
Flags: map[string]star.Flag{
"fs": fsParam,
"glfs": glfsParam,
"api": apiParam,
},
Pos: []star.Positional{addrParam},
F: func(c star.Context) error {
bc := bcclient.NewClientFromEnv()
localFS := map[string]fs.FS{}
for _, spec := range fsParam.Load(c) {
r, err := os.OpenRoot(spec.InFS)
if err != nil {
return err
}
defer r.Close()
localFS[spec.Path] = r.FS()
}
glfsVols := map[string]blobcache.Handle{}
for _, spec := range glfsParam.Load(c) {
volh, err := spec.Volume.Open(c, bc)
if err != nil {
return err
}
glfsVols[spec.Path] = *volh
}
go func() {
for range time.Tick(time.Second) {
hs := slices.Collect(maps.Values(glfsVols))
if err := bc.KeepAlive(c, hs); err != nil {
log.Printf("error refreshing volumes: %v", err)
}
}
}()
ep, err := bc.Endpoint(c)
if err != nil {
return err
}
ap := addrParam.Load(c)
c.Printf("LOCAL BLOBCACHE NODE:\n")
c.Printf("%v\n\n", ep)
c.Printf("serving at http://%v \n", ap)
var routes []Route
for pref, lfs := range localFS {
routes = append(routes, Route{
Prefix: pref,
Handler: http.FileServerFS(lfs),
})
}
for pref, volh := range glfsVols {
routes = append(routes, newGLFSRoute(pref, bc, volh))
}
for _, pref := range apiParam.Load(c) {
routes = append(routes, newAPIRoute(pref, bc))
}
slices.SortStableFunc(routes, func(a, b Route) int {
return len(b.Prefix) - len(a.Prefix)
})
srv := &Server{routes: routes}
return http.ListenAndServe(ap.String(), srv)
},
}
func newAPIRoute(prefix string, bc blobcache.Service) Route {
return Route{
Prefix: prefix,
Handler: &bchttp.Server{Service: bc},
}
}
func newLocalFSRoute(prefix string, fsys fs.FS) Route {
return Route{
Prefix: prefix,
Handler: http.FileServerFS(fsys),
}
}
func newGLFSRoute(prefix string, bc blobcache.Service, volh blobcache.Handle) Route {
return Route{
Prefix: prefix,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := viewGLFS(ctx, bc, volh, func(s schema.RO, root glfs.Ref) error {
fsys := glfsiofs.New(s, root)
http.FileServerFS(fsys).ServeHTTP(w, r)
return nil
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}),
}
}
type localMountSpec struct {
Path string
InFS string
}
type mountSpec struct {
Path string
Volume bcns.ObjectExpr
}
var glfsParam = star.Repeated[mountSpec]{
Parse: func(s string) (mountSpec, error) {
parts := strings.Split(s, ":")
if len(parts) != 2 {
return mountSpec{}, fmt.Errorf("invalid mount spec: %s", s)
}
expr, err := bcns.ParseObjectish(parts[1])
if err != nil {
return mountSpec{}, fmt.Errorf("invalid mount spec: %w", err)
}
return mountSpec{
Path: parts[0],
Volume: expr,
}, nil
},
}
var addrParam = star.Required[netip.AddrPort]{
ID: "addr",
Parse: netip.ParseAddrPort,
ShortDoc: "the address to listen on",
}
var fsParam = star.Repeated[localMountSpec]{
ID: "fs",
ShortDoc: "serve files from the local filesystem",
Parse: func(s string) (localMountSpec, error) {
parts := strings.Split(s, ":")
if len(parts) != 2 {
return localMountSpec{}, fmt.Errorf("invalid mount spec: %s", s)
}
return localMountSpec{
Path: parts[0],
InFS: parts[1],
}, nil
},
}
var apiParam = star.Repeated[string]{
ID: "api",
Parse: star.ParseString,
ShortDoc: "the route to put the Blobcache API on",
}
type Route struct {
Prefix string
Handler http.Handler
}
type Server struct {
routes []Route
}
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
for _, route := range s.routes {
if p, ok := strings.CutPrefix(req.URL.Path, route.Prefix); ok {
req.URL.Path = "/" + p
route.Handler.ServeHTTP(w, req)
return
}
}
http.NotFound(w, req)
}
func viewGLFS(ctx context.Context, svc blobcache.Service, volh blobcache.Handle, fn func(s schema.RO, root glfs.Ref) error) error {
return bcsdk.View(ctx, svc, volh, func(s bcsdk.RO, root []byte) error {
var ref glfs.Ref
if err := json.Unmarshal(root, &ref); err != nil {
return err
}
return fn(s, ref)
})
}