-
Notifications
You must be signed in to change notification settings - Fork 9
/
web.go
434 lines (357 loc) · 13.4 KB
/
web.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package web
import (
"context"
"fmt"
"io/fs"
"path/filepath"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/encoding/protojson"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/assets"
"namespacelabs.dev/foundation/internal/build/binary"
"namespacelabs.dev/foundation/internal/codegen/protos/fnany"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs/digestfs"
"namespacelabs.dev/foundation/internal/fnfs/memfs"
"namespacelabs.dev/foundation/internal/fnfs/workspace/wsremote"
"namespacelabs.dev/foundation/internal/hotreload"
"namespacelabs.dev/foundation/internal/integrations"
"namespacelabs.dev/foundation/internal/integrations/opaque"
"namespacelabs.dev/foundation/internal/nodejs"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/wscontents"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/development/controller/admin"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/execution/defs"
"namespacelabs.dev/foundation/std/pkggraph"
"namespacelabs.dev/foundation/std/web/http"
)
var (
controllerPkg = schema.MakePackageSingleRef("namespacelabs.dev/foundation/std/development/controller")
)
const (
httpPort = 40000
fileSyncPort = 50000
httpPortName = "http-port"
compiledPath = "static"
webPkg schema.PackageName = "namespacelabs.dev/foundation/std/web/http"
)
func Register() {
integrations.Register(schema.Framework_WEB, impl{})
execution.RegisterHandlerFunc(generateWebBackend)
}
type impl struct {
integrations.MaybePrepare
integrations.MaybeGenerate
integrations.MaybeTidy
}
func (impl) PreParseServer(_ context.Context, _ pkggraph.Location, ext *parsing.ServerFrameworkExt) error {
return nil
}
func (impl) PostParseServer(ctx context.Context, sealed *parsing.Sealed) error {
sealed.Proto.Server.StaticPort = []*schema.Endpoint_Port{{Name: httpPortName, ContainerPort: httpPort}}
return nil
}
func (impl) DevelopmentPackages() []schema.PackageName {
return []schema.PackageName{controllerPkg.AsPackageName()}
}
func (impl) PrepareBuild(ctx context.Context, buildAssets assets.AvailableBuildAssets, srv planning.Server, isFocus bool) (build.Spec, error) {
if opaque.UseDevBuild(srv.SealedContext().Environment()) {
pkg, err := srv.SealedContext().LoadByName(ctx, controllerPkg.AsPackageName())
if err != nil {
return nil, err
}
p, err := binary.Plan(ctx, pkg, controllerPkg.Name, srv.SealedContext(), buildAssets, binary.BuildImageOpts{UsePrebuilts: false})
if err != nil {
return nil, err
}
return buildDevServer{p.Plan, srv, isFocus, buildAssets.IngressFragments}, nil
}
return buildProdWebServer{
srv, isFocus, buildAssets.IngressFragments,
}, nil
}
func buildWebApps(ctx context.Context, conf build.Configuration, ingressFragments compute.Computable[[]*schema.IngressFragment], srv planning.Server, isFocus bool) ([]oci.NamedImage, error) {
var builds []oci.NamedImage
for _, entry := range srv.Proto().UrlMap {
dep := srv.GetDep(schema.PackageName(entry.PackageName))
if dep == nil {
return nil, fnerrors.NewWithLocation(srv.Location, "%s: included in url map but not loaded", entry.PackageName)
}
backends, err := parseBackends(dep.Node())
if err != nil {
return nil, err
}
var extra []*memfs.FS
if len(backends) > 0 {
fragments, err := compute.GetValue(ctx, ingressFragments)
if err != nil {
return nil, fnerrors.InternalError("failed to build web app while waiting on ingress computation: %w", err)
}
resolveFunc := resolveBackend(fragments)
backend := &OpGenHttpBackend{Backend: backends}
fsys, err := generateBackendConf(ctx, dep.Location, backend, resolveFunc, false)
if err != nil {
return nil, err
}
extra = append(extra, fsys)
}
loc, err := srv.SealedContext().Resolve(ctx, dep.PackageName())
if err != nil {
return nil, err
}
targetConf := build.NewBuildTarget(conf.TargetPlatform()).
WithTargetName(conf.PublishName()).
WithSourcePackage(srv.PackageName()).
WithWorkspace(conf.Workspace())
externalModules := GetExternalModuleForDeps(srv)
b, err := prepareBuild(ctx, loc, srv.SealedContext(), targetConf, entry, isFocus, externalModules, extra)
if err != nil {
return nil, err
}
builds = append(builds, b)
}
return builds, nil
}
func prepareBuild(ctx context.Context, loc pkggraph.Location, env cfg.Context, targetConf build.Configuration, entry *schema.Server_URLMapEntry, isFocus bool, externalModules []build.Workspace, extra []*memfs.FS) (oci.NamedImage, error) {
if !opaque.UseDevBuild(env.Environment()) {
extra = append(extra, generateProdViteConfig())
return ViteProductionBuild(ctx, loc, env, targetConf.SourceLabel(), filepath.Join(compiledPath, entry.PathPrefix), entry.PathPrefix, externalModules, extra...)
}
var devwebConfig memfs.FS
devwebConfig.Add("devweb.config.js", []byte(`
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import pluginRewriteAll from "vite-plugin-rewrite-all";
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
return defineConfig({
plugins: [react(), pluginRewriteAll()],
base: process.env.CMD_DEV_BASE || "/",
resolve: {
// Important to correctly handle ns-managed dependencies.
preserveSymlinks: true,
},
server: {
watch: {
usePolling: true,
interval: 500,
binaryInterval: 1000,
},
hmr: {
clientPort: process.env.CMD_DEV_PORT,
},
},
});
};`))
extra = append(extra, &devwebConfig)
return viteDevBuild(ctx, env, filepath.Join("/packages", loc.Module.ModuleName()), loc, isFocus, targetConf, externalModules, extra...)
}
func generateProdViteConfig() *memfs.FS {
var prodwebConfig memfs.FS
prodwebConfig.Add("prodweb.config.js", []byte(`
import react from "@vitejs/plugin-react";
export default {
plugins: [react()],
resolve: {
// Important to correctly handle ns-managed dependencies.
preserveSymlinks: true,
},
}`))
return &prodwebConfig
}
func (impl) PrepareDev(ctx context.Context, cluster runtime.ClusterNamespace, srv planning.Server) (context.Context, integrations.DevObserver, error) {
return hotreload.ConfigureFileSyncDevObserver(ctx, cluster, srv)
}
func (impl) PrepareHotReload(ctx context.Context, remote *wsremote.SinkRegistrar, srv planning.Server) *integrations.HotReloadOpts {
return &integrations.HotReloadOpts{
Sink: remote.For(&wsremote.Signature{ModuleName: srv.Module().ModuleName(), Rel: "."}),
EventProcessor: func(ev *wscontents.FileEvent) *wscontents.FileEvent {
if ev.Path == yarnLockFn {
return nil
} else {
return ev
}
},
}
}
func (impl) PrepareRun(ctx context.Context, srv planning.Server, run *runtime.ContainerRunOpts) error {
if opaque.UseDevBuild(srv.SealedContext().Environment()) {
configuration := &admin.Configuration{
PackageBase: "/packages",
RevproxyPort: httpPort,
FilesyncPort: fileSyncPort,
}
for k, m := range srv.Proto().UrlMap {
port := httpPort + k + 1
configuration.Backend = append(configuration.Backend, &admin.Backend{
PackageName: m.PackageName,
Execution: &admin.Execution{
Args: []string{
"node_modules/vite/bin/vite.js",
"--config=devweb.config.js",
"--clearScreen=false",
"--host=127.0.0.1",
fmt.Sprintf("--port=%d", port),
},
AdditionalEnv: []string{
fmt.Sprintf("CMD_DEV_BASE=%s", m.PathPrefix),
fmt.Sprintf("CMD_DEV_PORT=%d", runtime.LocalIngressPort),
},
},
HttpPass: &admin.HttpPass{
UrlPrefix: m.PathPrefix,
Backend: fmt.Sprintf("127.0.0.1:%d", port),
},
})
}
serialized, err := protojson.Marshal(configuration)
if err != nil {
return fnerrors.InternalError("failed to serialize configuration: %v", err)
}
// XXX support a persistent cache: https://vitejs.dev/guide/dep-pre-bundling.html#file-system-cache
run.Command = []string{"/devcontroller"}
run.Args = append(run.Args, fmt.Sprintf("--configuration=%s", serialized))
return nil
}
run.Command = []string{"nginx", "-g", "daemon off;"}
// This is OK because the nginx image output logs to stdout/stderr by default.
run.ReadOnlyFilesystem = false // See #276.
// See #276.
// run.RunAs = &runtime.RunAs{
// UserID: "101", // This is the image's default. We lift it here explicitly for visibility at the runtime level.
// }
return nil
}
func (i impl) TidyNode(ctx context.Context, env cfg.Context, pkgs pkggraph.PackageLoader, p *pkggraph.Package) error {
devPackages := []string{
"typescript@4.5.4",
}
if p.Node().Kind == schema.Node_SERVICE {
devPackages = append(devPackages, "vite@2.7.13")
}
if p.Location.Module.IsExternal() {
return fnerrors.BadInputError("%s: can't run tidy on external module", p.Location.Module)
}
if err := nodejs.RunYarn(ctx, env, p.Location, append([]string{"add", "-D", "--mode=skip-build"}, devPackages...)); err != nil {
return err
}
return nil
}
func parseBackends(n *schema.Node) ([]*OpGenHttpBackend_Backend, error) {
var backends []*OpGenHttpBackend_Backend
for _, p := range n.Instantiate {
backend := &http.Backend{}
if ok, err := fnany.CheckUnmarshal(p.Constructor, webPkg, backend); ok {
if err != nil {
return nil, err
}
backends = append(backends, &OpGenHttpBackend_Backend{
InstanceName: p.Name,
EndpointOwner: backend.EndpointOwner,
ServiceName: backend.ServiceName,
IngressName: backend.IngressName,
Manager: backend.Manager,
})
}
}
return backends, nil
}
func (i impl) GenerateNode(pkg *pkggraph.Package, available []*schema.Node) ([]*schema.SerializedInvocation, error) {
var dl defs.DefList
backends, err := parseBackends(pkg.Node())
if err != nil {
return nil, err
}
if len(backends) > 0 {
dl.Add("Generate Web node dependencies", &OpGenHttpBackend{
Node: pkg.Node(),
Backend: backends,
}, pkg.PackageName())
}
return dl.Serialize()
}
type buildDevServer struct {
baseImage build.Plan
srv planning.Server
isFocus bool
ingressFragments compute.Computable[[]*schema.IngressFragment]
}
func (bws buildDevServer) BuildImage(ctx context.Context, env pkggraph.SealedContext, conf build.Configuration) (compute.Computable[oci.Image], error) {
builds, err := buildWebApps(ctx, conf, bws.ingressFragments, bws.srv, bws.isFocus)
if err != nil {
return nil, err
}
baseImage, err := bws.baseImage.Spec.BuildImage(ctx, env,
build.NewBuildTarget(conf.TargetPlatform()).
WithTargetName(conf.PublishName()).
WithSourceLabel(bws.baseImage.SourceLabel).
WithWorkspace(bws.baseImage.Workspace))
if err != nil {
return nil, err
}
images := []oci.NamedImage{oci.MakeNamedImage(bws.baseImage.SourceLabel, baseImage)}
images = append(images, builds...)
return oci.MergeImageLayers(images...), nil
}
func (bws buildDevServer) PlatformIndependent() bool { return false }
type buildProdWebServer struct {
srv planning.Server
isFocus bool
ingressFragments compute.Computable[[]*schema.IngressFragment]
}
func (bws buildProdWebServer) BuildImage(ctx context.Context, env pkggraph.SealedContext, conf build.Configuration) (compute.Computable[oci.Image], error) {
builds, err := buildWebApps(ctx, conf, bws.ingressFragments, bws.srv, bws.isFocus)
if err != nil {
return nil, err
}
// XXX this is not quite right. We always setup a index.html fallback
// regardless of content. And that's probably over-reaching. The user should
// let us know which paths require this fallback.
var defaultConf memfs.FS
defaultConf.Add("etc/nginx/conf.d/default.conf", []byte(fmt.Sprintf(`server {
listen %d;
server_name localhost;
location / {
root /%s;
index index.html;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}`, httpPort, compiledPath)))
config := oci.MakeLayer("conf", compute.Precomputed[fs.FS](&defaultConf, digestfs.Digest))
images := []oci.NamedImage{
oci.ResolveImage("nginx:1.21.5-alpine", *conf.TargetPlatform()),
oci.MakeImageFromScratch("nginx-configuration", config),
}
images = append(images, builds...)
return oci.MergeImageLayers(images...), nil
}
// This is unfortunate, but because of our layering we do indeed end up building images twice.
func (bws buildProdWebServer) PlatformIndependent() bool { return false }
func GetExternalModuleForDeps(server planning.Server) []build.Workspace {
moduleMap := map[string]*pkggraph.Module{}
for _, dep := range server.Deps() {
if dep.Location.Module.ModuleName() != server.Module().ModuleName() &&
(dep.Node() != nil && (slices.Contains(dep.Node().CodegeneratedFrameworks(), schema.Framework_WEB))) {
moduleMap[dep.Location.Module.ModuleName()] = dep.Location.Module
}
}
modules := []build.Workspace{}
for _, module := range moduleMap {
modules = append(modules, module)
}
return modules
}