-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergedata.go
312 lines (267 loc) · 10 KB
/
mergedata.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package controller
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
// Using text/template here instead of html/template
// because we trust both the template and the merge data
"text/template"
"time"
"github.com/thecloudmasters/uesio/pkg/bundlestore"
"github.com/thecloudmasters/uesio/pkg/controller/ctlutil"
"github.com/thecloudmasters/uesio/pkg/preload"
"github.com/thecloudmasters/uesio/pkg/types/wire"
"github.com/thecloudmasters/uesio/pkg/controller/file"
"github.com/thecloudmasters/uesio/pkg/merge"
"github.com/thecloudmasters/uesio/pkg/meta"
"github.com/thecloudmasters/uesio/pkg/routing"
"github.com/thecloudmasters/uesio/pkg/sess"
"github.com/thecloudmasters/uesio/pkg/templating"
)
var indexTemplate *template.Template
func init() {
wd, _ := os.Getwd()
baseDir := wd
// Handle path resolution issues when running tests
if strings.Contains(wd, filepath.Join("pkg", "")) {
baseDir = filepath.Join(wd, "..", "..")
}
indexPath := filepath.Join(baseDir, "platform", "index.gohtml")
indexTemplate = template.Must(template.New("index.gohtml").Funcs(template.FuncMap{
"getComponentPackURLs": getComponentPackURLs,
"getComponentPackStyleURLs": getComponentPackStyleURLs,
"getFontCSSURLs": getFontCSSURLs,
}).ParseFiles(indexPath))
}
func getComponentPackURLs(componentPackDeps *preload.MetadataMergeData, workspace *preload.WorkspaceMergeData, site *preload.SiteMergeData) []string {
allDeps := componentPackDeps.GetItems()
packUrls := make([]string, len(allDeps))
for i, packDep := range allDeps {
key := packDep.GetKey()
var packModstamp int64
workspaceMergeData := workspace
if pack, ok := packDep.(*meta.ComponentPack); ok {
packModstamp = pack.UpdatedAt
if pack.SiteOnly {
workspaceMergeData = nil
}
} else {
packModstamp = time.Now().Unix()
}
packUrls[i] = getPackUrl(key, packModstamp, workspaceMergeData, site, "runtime.js")
}
return packUrls
}
func getComponentPackStyleURLs(componentPackDeps *preload.MetadataMergeData, workspace *preload.WorkspaceMergeData, site *preload.SiteMergeData) []string {
allDeps := componentPackDeps.GetItems()
packUrls := []string{}
for _, packDep := range allDeps {
key := packDep.GetKey()
var packModstamp int64
if pack, ok := packDep.(*meta.ComponentPack); ok {
packModstamp = pack.UpdatedAt
if pack.HasStyles {
packUrls = append(packUrls, getPackUrl(key, packModstamp, workspace, site, "runtime.css"))
}
}
}
return packUrls
}
func getFontCSSURLs(fontDeps *preload.MetadataMergeData, workspace *preload.WorkspaceMergeData, site *preload.SiteMergeData) []string {
allDeps := fontDeps.GetItems()
fontUrls := []string{}
for _, fontDep := range allDeps {
key := fontDep.GetKey()
var fontModstamp int64
if font, ok := fontDep.(*meta.Font); ok {
fontModstamp = font.UpdatedAt
if font.CSSPath != "" {
fontUrls = append(fontUrls, getFontUrl(key, fontModstamp, workspace, site, font.CSSPath))
}
}
}
return fontUrls
}
func getSiteBundleVersion(namespace string, modstamp int64, site *preload.SiteMergeData) string {
siteBundleVersion := ""
// Handle requests for system bundles specially,
// since we don't update their bundle dependencies at all and just use dummy "v0.0.1" everywhere
if bundlestore.IsSystemBundle(namespace) {
if file.GetAssetsPath() != "" {
// We DO update the static assets version for the whole Docker image, so use that if we have it
siteBundleVersion = file.GetAssetsPath() // assets path SHOULD have a leading / already
} else if modstamp != 0 {
// If we don't have a Git sha, then we are in local development,
// in which case we want to use the system pack modstamp to avoid stale file loads
siteBundleVersion = fmt.Sprintf("/%d", modstamp)
}
} else {
// NON-system bundles
if namespace == site.App {
// If requested namespace is the app's name, use the site version
siteBundleVersion = fmt.Sprintf("/%s", site.Version)
} else if site.Dependencies != nil {
// For all other deps, use the site's declared bundle dependency version,
// which SHOULD be present (otherwise how are they using it...)
if dep, found := site.Dependencies[namespace]; found && dep.Version != "" {
siteBundleVersion = "/" + dep.Version
}
}
}
// If we still don't have a bundle version, for some bizarre reason...
if siteBundleVersion == "" {
if modstamp != 0 {
// Prefer modstamp
siteBundleVersion = fmt.Sprintf("/%d", modstamp)
} else if site.Version != "" {
// Final fallback --- use site version
siteBundleVersion = fmt.Sprintf("/%s", site.Version)
}
}
return siteBundleVersion
}
func getPackUrl(key string, modstamp int64, workspace *preload.WorkspaceMergeData, site *preload.SiteMergeData, filePath string) string {
return getAssetUrl(key, "componentpacks", modstamp, workspace, site, filePath)
}
func getFontUrl(key string, modstamp int64, workspace *preload.WorkspaceMergeData, site *preload.SiteMergeData, filePath string) string {
return getAssetUrl(key, "fonts", modstamp, workspace, site, filePath)
}
func getAssetUrl(key, prefix string, modstamp int64, workspace *preload.WorkspaceMergeData, site *preload.SiteMergeData, filePath string) string {
namespace, name, err := meta.ParseKey(key)
if err != nil {
return ""
}
user, namepart, err := meta.ParseNamespace(namespace)
if err != nil {
return ""
}
if workspace != nil {
// If we are in a workspace context, use component pack modstamps to load in their resources,
// since we don't have a stable "site" version that we can safely use, as the bundle dependency list is not immutable.
return fmt.Sprintf("/workspace/%s/%s/%s/%s/%s/%d/%s/%s", workspace.App, workspace.Name, prefix, user, namepart, modstamp, name, filePath)
}
siteBundleVersion := getSiteBundleVersion(namespace, modstamp, site)
return fmt.Sprintf("/site/%s/%s%s/%s/%s", prefix, namespace, siteBundleVersion, name, filePath)
}
func GetWorkspaceMergeData(workspace *meta.Workspace) *preload.WorkspaceMergeData {
if workspace == nil {
return nil
}
return &preload.WorkspaceMergeData{
Name: workspace.Name,
App: workspace.GetAppFullName(),
Wrapper: routing.DEFAULT_BUILDER_COMPONENT,
}
}
func MergeRouteData(mergeableText string, mergeData *merge.ServerMergeData) (string, error) {
// No need to merge if no merge syntax
if !strings.Contains(mergeableText, "{") {
return mergeableText, nil
}
template, err := templating.NewWithFuncs(mergeableText, templating.ForceErrorFunc, merge.ServerMergeFuncs)
if err != nil {
return "", err
}
return templating.Execute(template, mergeData)
}
func GetRoutingMergeData(route *meta.Route, metadata *preload.PreloadMetadata, session *sess.Session) (*preload.RouteMergeData, error) {
// Prepare wire data for server merge data
wireData := map[string]meta.Group{}
if metadata != nil && metadata.Wire != nil {
for _, entity := range metadata.Wire.GetItems() {
wire := entity.(*wire.LoadOp)
wireData[wire.WireName] = wire.Collection
}
}
serverMergeData := &merge.ServerMergeData{
Session: session,
ParamValues: route.Params,
WireData: wireData,
}
// Default if no text
routeTitle := route.Title
if routeTitle == "" {
routeTitle = "Uesio"
}
mergedRouteTitle, err := MergeRouteData(routeTitle, serverMergeData)
if err != nil {
return nil, err
}
var mergedRouteTags []meta.Tag
if len(route.Tags) > 0 {
for _, tag := range route.Tags {
mergedContent, err := MergeRouteData(tag.Content, serverMergeData)
if err == nil {
mergedRouteTags = append(mergedRouteTags, meta.Tag{
Type: tag.Type,
Location: tag.Location,
Name: tag.Name,
Content: mergedContent,
})
} else {
return nil, err
}
}
}
return &preload.RouteMergeData{
Dependencies: metadata,
Namespace: route.Namespace,
Params: route.Params,
Path: route.Path,
Tags: mergedRouteTags,
Theme: route.ThemeRef,
Title: mergedRouteTitle,
Type: route.Type,
View: route.ViewRef,
Workspace: GetWorkspaceMergeData(session.GetWorkspace()),
}, err
}
func GetSiteMergeData(site *meta.Site) *preload.SiteMergeData {
return &preload.SiteMergeData{
Name: site.Name,
App: site.GetAppFullName(),
Subdomain: site.Subdomain,
Domain: site.Domain,
Version: site.Bundle.GetVersionString(),
Title: site.Title,
EnableSEO: site.EnableSEO,
Dependencies: site.GetAppBundle().Dependencies,
}
}
func ExecuteIndexTemplate(w http.ResponseWriter, route *meta.Route, preloadmeta *preload.PreloadMetadata, buildMode bool, session *sess.Session) {
// #2783 Prevent 3rd party sites from iframing Uesio
// Add a content security policy header to prevent any other sites from iframing this site
// TODO: make this configurable by site (see issue #2782)
w.Header().Set("content-security-policy", "frame-ancestors 'none';")
w.Header().Set("content-type", "text/html")
site := session.GetSite()
routingMergeData, err := GetRoutingMergeData(route, preloadmeta, session)
if err != nil {
ctlutil.HandleError(w, errors.New("Error getting route merge data: "+err.Error()))
return
}
vendorScriptUrls := file.GetVendorScriptUrls()
mergeData := preload.MergeData{
Route: routingMergeData,
User: preload.GetUserMergeData(session),
Site: GetSiteMergeData(site),
PreloadMetadata: preloadmeta,
MonacoEditorVersion: file.GetMonacoEditorVersion(),
StaticAssetsPath: file.GetAssetsPath(),
StaticAssetsHost: file.GetAssetsHost(),
VendorScriptUrls: vendorScriptUrls,
}
// Initiate early preloads of all vendor scripts via Link headers
// TODO: Header order seems to be non-deterministic, so unless we can guarantee the order, or load these as modules
// which get invoked in a fixed order later on, we can't use this approach for things like React/ReactDOM
// for _, script := range vendorScriptUrls {
// w.Header().Add("Link", fmt.Sprintf("<%s>; rel=preload; as=script", script))
// }
if err = indexTemplate.Execute(w, mergeData); err != nil {
ctlutil.HandleError(w, errors.New("Error merging template: "+err.Error()))
return
}
}