generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
local.go
46 lines (36 loc) · 1.04 KB
/
local.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
//go:build !release
package frontend
import (
"context"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/TBD54566975/ftl/internal"
"github.com/TBD54566975/ftl/internal/cors"
"github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
)
var proxyURL, _ = url.Parse("http://localhost:5173")
var proxy = httputil.NewSingleHostReverseProxy(proxyURL)
func Server(ctx context.Context, timestamp time.Time, publicURL *url.URL, allowOrigin *url.URL) (http.Handler, error) {
logger := log.FromContext(ctx)
logger.Debugf("Building console...")
gitRoot, ok := internal.GitRoot("").Get()
if !ok {
return nil, fmt.Errorf("failed to find Git root")
}
err := exec.Command(ctx, log.Debug, gitRoot, "just", "build-frontend").RunBuffered(ctx)
if err != nil {
return nil, err
}
err = exec.Command(ctx, log.Debug, "frontend", "npm", "run", "dev").Start()
if err != nil {
return nil, err
}
if allowOrigin == nil {
return proxy, nil
}
return cors.Middleware([]string{allowOrigin.String()}, proxy), nil
}