-
Notifications
You must be signed in to change notification settings - Fork 7
/
prpl.go
148 lines (129 loc) · 3.26 KB
/
prpl.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
package prpl
import (
"net/http"
"github.com/ua-parser/uap-go/uaparser"
)
type (
// prpl is an instance of the prpl-server service
prpl struct {
http.Handler
parser *uaparser.Parser
config *ProjectConfig
builds builds
root http.Dir
routes Routes
staticHandlers map[string]http.Handler
createTemplate createTemplateFn
usePush bool
}
// optionFn provides functional option configuration
optionFn func(*prpl) error
)
// New creates a new prpl instance
func New(options ...optionFn) (*prpl, error) {
p := prpl{
parser: uaparser.NewFromSaved(),
root: http.Dir("."),
staticHandlers: make(map[string]http.Handler),
createTemplate: createDefaultTemplate,
usePush: true,
}
for _, option := range options {
if err := option(&p); err != nil {
return nil, err
}
}
// use polymer.json for build file by default
if p.config == nil {
if err := WithConfigFile("polymer.json")(&p); err != nil {
return nil, err
}
}
// TODO: pass p in rather than all the properties
p.builds = loadBuilds(p.config, p.root, p.routes, p.createTemplate)
p.Handler = p.createHandler()
return &p, nil
}
// WithRoutes sets the route -> fragment mapping
func WithRoutes(routes Routes) optionFn {
return func(p *prpl) error {
p.routes = routes
return nil
}
}
// WithRoot sets the root directory
func WithRoot(root http.Dir) optionFn {
return func(p *prpl) error {
p.root = root
return nil
}
}
// WithConfig sets the project configuration
func WithConfig(config *ProjectConfig) optionFn {
return func(p *prpl) error {
p.config = config
return nil
}
}
// WithConfigFile loads the project configuration
func WithConfigFile(filename string) optionFn {
return func(p *prpl) error {
config, err := ConfigFromFile(filename)
if err != nil {
return err
}
p.config = config
return nil
}
}
// WithUAParserFile allows the uaparser configuration
// to be overriden from the inbuilt settings
func WithUAParserFile(regexFile string) optionFn {
return func(p *prpl) error {
parser, err := uaparser.New(regexFile)
if err != nil {
return err
}
p.parser = parser
return nil
}
}
// WithUAParserBytes allows the uaparser configuration
// to be overriden from the inbuilt settings
func WithUAParserBytes(data []byte) optionFn {
return func(p *prpl) error {
parser, err := uaparser.NewFromBytes(data)
if err != nil {
return err
}
p.parser = parser
return nil
}
}
// WithStaticHandler allows the handler for certain static
// files to be overridden. This could be used to customize
// the manifest.json file per tenant or to serve specific
// images based on host headers etc ...
func WithStaticHandler(path string, handler http.Handler) optionFn {
return func(p *prpl) error {
p.staticHandlers[path] = handler
return nil
}
}
// WithRouteTemplate allows the entrypoint to be converted
// into a template so that the output can be transformed if
// required
func WithRouteTemplate(factory createTemplateFn) optionFn {
return func(p *prpl) error {
p.createTemplate = factory
return nil
}
}
// WithPush allows control over the sending of http server
// push / link headers
func WithPush(usePush bool) optionFn {
return func(p *prpl) error {
p.usePush = usePush
return nil
}
}