-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapper.go
70 lines (60 loc) · 1.77 KB
/
mapper.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
package mapper
import (
"strings"
"net/http"
"github.com/captaincodeman/datastore-locker"
)
const (
// DefaultPath is a decent default path to mount the mapper mux on
DefaultPath = "/_ah/mapper/"
)
type (
// mapper holds the http mux we will attach our handlers to and the
// the path it is mounted at so we can generate tasks to address it
mapper struct {
*http.ServeMux
locker *locker.Locker
config *Config
}
)
var (
// there can only be one instance and we need it created before init
// so that we can attach our handlers to it. Their paths are relative
// so it's OK that we don't yet know the prefix it will be mounted at.
server = newMapper()
)
// NewServer configures the server and returns the handler for mounting
// within the app so it can control the endpoint to use. The server is
// actually already created but we need to know what the path prefix is.
func NewServer(path string, options ...Option) (http.Handler, error) {
server.config.Path = strings.TrimSuffix(path, "/")
for _, option := range options {
if err := option(server.config); err != nil {
return nil, err
}
}
handler := http.StripPrefix(server.config.Path, server)
// pass on locker options
lockerOptions := []locker.Option{
locker.LeaseDuration(server.config.LeaseDuration),
locker.LeaseTimeout(server.config.LeaseTimeout),
locker.DefaultQueue(server.config.DefaultQueue),
locker.MaxRetries(server.config.Retries),
locker.Host(server.config.Host),
}
if server.config.LogVerbose {
lockerOptions = append(lockerOptions, locker.LogVerbose)
}
var err error
server.locker, err = locker.NewLocker(lockerOptions...)
if err != nil {
return nil, err
}
return handler, nil
}
func newMapper() *mapper {
return &mapper{
ServeMux: http.NewServeMux(),
config: newConfig(),
}
}