/
0_interface.go
78 lines (68 loc) · 2.24 KB
/
0_interface.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
package endpoints
import (
"context"
"github.com/sneat-co/sneat-go-core/apicore"
"github.com/sneat-co/sneat-go-core/apicore/verify"
"log"
"net/http"
)
// ProjectEndpoints defines project endpoints
type ProjectEndpoints interface {
CreateProject(w http.ResponseWriter, r *http.Request)
DeleteProject(w http.ResponseWriter, r *http.Request)
}
// getContextFromRequest creates context for a given request
// We need a request for example to decode Firebase token
// Probably can be done better then being hard-depending on an *http.Request
var getContextFromRequest = func(r *http.Request) (context.Context, error) {
return r.Context(), nil
}
// VerifyRequest implements VerifyRequestOptions
type VerifyRequest struct { // TODO: move to shared Sneat package
MinContentLength int64
MaxContentLength int64
AuthRequired bool
}
// MinimumContentLength defines min content length
func (v VerifyRequest) MinimumContentLength() int64 {
return v.MinContentLength
}
// MaximumContentLength defines max content length, if < 0 no limit
func (v VerifyRequest) MaximumContentLength() int64 {
return v.MaxContentLength
}
// AuthenticationRequired specifies if authentication is mandatory
func (v VerifyRequest) AuthenticationRequired() bool {
return v.AuthRequired
}
// Handler is responsible for creating context and call `handler()` func that should use
// provided context along with `requestDTO` that was populated from request body
// Its is exposed publicly so it can be replaced with custom implementation
type Handler = func(
w http.ResponseWriter,
r *http.Request,
requestDTO apicore.RequestDTO,
verifyOptions verify.RequestOptions,
successStatusCode int,
getContext apicore.ContextProvider,
handler apicore.Worker,
)
//goland:noinspection GoVarAndConstTypeMayBeOmitted
var handle Handler = func(
w http.ResponseWriter,
r *http.Request,
requestDTO apicore.RequestDTO,
verifyOptions verify.RequestOptions,
successStatusCode int,
getContext apicore.ContextProvider,
doWork apicore.Worker,
) {
panic("handle is not initialized properly")
}
func route(r router, wrap wrapper, method, path string, handler http.HandlerFunc) {
log.Printf("Registering %v %v", method, path)
if wrap != nil {
handler = wrap(handler)
}
r.HandlerFunc(method, path, handler)
}