-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
90 lines (73 loc) · 1.79 KB
/
server.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
package streisand
import (
"io"
"net/http"
"net/url"
"sync"
"github.com/Jille/convreq"
"github.com/Jille/errchain"
"github.com/bertha/streisand/diskstore"
)
type Server interface {
http.Handler
io.Closer
}
type PeersFunc func() ([]*url.URL, error)
type ServerConfig struct {
DataDir, CacheDir string
WithFsync bool
Debug bool
GetPeers PeersFunc
}
func NewServer(conf ServerConfig) (Server, error) {
s := server{
conf: conf,
store: &diskstore.Store{
Path: conf.DataDir,
BitsPerFolder: []uint8{8, 8},
Fsync: conf.WithFsync,
},
xors: &XorStore{
LayerCount: 6,
LayerDepth: 4,
Path: conf.CacheDir,
},
hmux: http.NewServeMux(),
}
s.store.Initialize()
if err := s.xors.Initialize(); err != nil {
return nil, err
}
s.hmux.HandleFunc("/blob/", convreq.Wrap(func(r *http.Request) convreq.HttpResponse {
return s.handleGetBlob(r, true)
}))
s.hmux.HandleFunc("/internal/blob/", convreq.Wrap(func(r *http.Request) convreq.HttpResponse {
return s.handleGetBlob(r, false)
}))
s.hmux.HandleFunc("/upload", convreq.Wrap(s.handlePostBlob))
s.hmux.HandleFunc("/internal/upload", convreq.Wrap(s.handleInternalPostBlob))
s.hmux.HandleFunc("/list", convreq.Wrap(s.handleGetList))
s.hmux.HandleFunc("/query", func(w http.ResponseWriter, r *http.Request) {
})
if s.conf.Debug {
s.hmux.HandleFunc("/debug/add-xor",
convreq.Wrap(s.handleDebugAddXor))
}
return &s, nil
}
type server struct {
conf ServerConfig
store *diskstore.Store
xors *XorStore
mutex sync.RWMutex
hmux *http.ServeMux
}
func (s *server) Close() (err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
errchain.Call(&err, s.xors.Close)
return err
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.hmux.ServeHTTP(w, r)
}