/
server.go
198 lines (184 loc) · 4.68 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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2021 Wrangle Ltd
package apiserver
import (
"context"
"fmt"
"io"
"net/http"
"regexp"
"time"
"github.com/wrgl/core/pkg/auth"
"github.com/wrgl/core/pkg/conf"
"github.com/wrgl/core/pkg/objects"
"github.com/wrgl/core/pkg/ref"
"github.com/wrgl/core/pkg/router"
)
var (
patAuthenticate *regexp.Regexp
patConfig *regexp.Regexp
patRefs *regexp.Regexp
patHead *regexp.Regexp
patRefsHead *regexp.Regexp
patUploadPack *regexp.Regexp
patReceivePack *regexp.Regexp
patCommits *regexp.Regexp
patCommit *regexp.Regexp
patSum *regexp.Regexp
patTables *regexp.Regexp
patTable *regexp.Regexp
patBlocks *regexp.Regexp
patTableBlocks *regexp.Regexp
patRows *regexp.Regexp
patTableRows *regexp.Regexp
patDiff *regexp.Regexp
)
type claimsKey struct{}
func SetClaims(r *http.Request, claims *auth.Claims) *http.Request {
return r.WithContext(context.WithValue(r.Context(), claimsKey{}, claims))
}
func getClaims(r *http.Request) *auth.Claims {
if i := r.Context().Value(claimsKey{}); i != nil {
return i.(*auth.Claims)
}
return nil
}
type ServerOption func(s *Server)
func WithPostCommitCallback(postCommit func(r *http.Request, commit *objects.Commit, sum []byte, branch string)) ServerOption {
return func(s *Server) {
s.postCommit = postCommit
}
}
func WithDebug(w io.Writer) ServerOption {
return func(s *Server) {
s.debugOut = w
}
}
type Server struct {
getAuthnS func(r *http.Request) auth.AuthnStore
getDB func(r *http.Request) objects.Store
getRS func(r *http.Request) ref.Store
getConfS func(r *http.Request) conf.Store
getUpSession func(r *http.Request) UploadPackSessionStore
getRPSession func(r *http.Request) ReceivePackSessionStore
postCommit func(r *http.Request, commit *objects.Commit, sum []byte, branch string)
router *router.Router
maxAge time.Duration
debugOut io.Writer
}
func NewServer(
rootPath *regexp.Regexp, getAuthnS func(r *http.Request) auth.AuthnStore, getDB func(r *http.Request) objects.Store, getRS func(r *http.Request) ref.Store,
getConfS func(r *http.Request) conf.Store, getUpSession func(r *http.Request) UploadPackSessionStore, getRPSession func(r *http.Request) ReceivePackSessionStore,
opts ...ServerOption,
) *Server {
s := &Server{
getAuthnS: getAuthnS,
getDB: getDB,
getRS: getRS,
getConfS: getConfS,
getUpSession: getUpSession,
getRPSession: getRPSession,
maxAge: 90 * 24 * time.Hour,
}
s.router = router.NewRouter(rootPath, &router.Routes{
Subs: []*router.Routes{
{
Method: http.MethodPost,
Pat: patAuthenticate,
HandlerFunc: s.handleAuthenticate,
},
{
Pat: patConfig,
Subs: []*router.Routes{
{
Method: http.MethodGet,
HandlerFunc: s.handleGetConfig,
},
{
Method: http.MethodPut,
HandlerFunc: s.handlePutConfig,
},
},
},
{
Pat: patRefs,
Subs: []*router.Routes{
{
Method: http.MethodGet,
HandlerFunc: s.handleGetRefs,
},
{
Method: http.MethodGet,
Pat: patHead,
HandlerFunc: s.handleGetHead,
},
},
},
{
Method: http.MethodPost,
Pat: patUploadPack,
HandlerFunc: s.handleUploadPack,
},
{
Method: http.MethodPost,
Pat: patReceivePack,
HandlerFunc: s.handleReceivePack,
},
{
Pat: patCommits,
Subs: []*router.Routes{
{
Method: http.MethodPost,
HandlerFunc: s.handleCommit,
},
{
Method: http.MethodGet,
HandlerFunc: s.handleGetCommits,
},
{
Method: http.MethodGet,
Pat: patSum,
HandlerFunc: s.handleGetCommit,
},
}},
{
Pat: patTables,
Subs: []*router.Routes{
{
Method: http.MethodGet,
Pat: patSum,
HandlerFunc: s.handleGetTable,
Subs: []*router.Routes{
{
Method: http.MethodGet,
Pat: patBlocks,
HandlerFunc: s.handleGetBlocks,
},
{
Method: http.MethodGet,
Pat: patRows,
HandlerFunc: s.handleGetRows,
},
}},
}},
{
Method: http.MethodGet,
Pat: patDiff,
HandlerFunc: s.handleDiff,
},
},
})
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(rw, r)
}
func (s *Server) cacheControlImmutable(rw http.ResponseWriter) {
rw.Header().Set(
"Cache-Control",
fmt.Sprintf("public, immutable, max-age=%d", int(s.maxAge.Seconds())),
)
}