-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.go
More file actions
75 lines (69 loc) · 2.28 KB
/
Copy pathassets.go
File metadata and controls
75 lines (69 loc) · 2.28 KB
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
// Copyright 2026 The Faros Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
package main
import (
"embed"
"errors"
"io"
"io/fs"
"log"
"mime"
"net/http"
"path"
"strings"
)
// portalFS embeds the Vite build output. The portal/ subdirectory holds a
// standalone npm project (Vite + TypeScript); see portal/README.md.
//
// `all:` so dotfiles (.gitkeep) are bundled too — without that the embed
// would fail at compile time when the dist/ directory exists but is empty.
// Run `npm --prefix portal install && npm --prefix portal run build` (or
// `make build-kuery-provider` from the repo root) to populate dist/
// before `go build`; the Makefile target chains the two.
//
//go:embed all:portal/dist
var portalFS embed.FS
// portalHandler serves portal/dist as static files. Returns the served
// http.Handler and a sub-FS rooted at the dist directory so the index
// fallback at "/" can read index.html without the "portal/dist/" prefix.
func portalHandler() (http.Handler, fs.FS, error) {
distFS, err := fs.Sub(portalFS, "portal/dist")
if err != nil {
return nil, nil, err
}
return http.FileServer(http.FS(distFS)), distFS, nil
}
// servePortalAsset writes the file at name from distFS to w. Returns false
// (and writes nothing) if the file isn't present, letting the caller fall
// through to its own handling — typically the index fallback. Content-Type
// is set from the path extension because http.FileServer's auto-sniff
// doesn't apply when we're reading bytes ourselves.
func servePortalAsset(w http.ResponseWriter, _ *http.Request, distFS fs.FS, name string) bool {
name = strings.TrimPrefix(name, "/")
if name == "" {
return false
}
f, err := distFS.Open(name)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
log.Printf("portal asset %s: %v", name, err)
}
return false
}
defer f.Close()
ct := mime.TypeByExtension(path.Ext(name))
if ct == "" {
ct = "application/octet-stream"
}
w.Header().Set("Content-Type", ct)
w.Header().Set("Cache-Control", "no-cache")
if _, err := io.Copy(w, f); err != nil {
log.Printf("portal asset %s write: %v", name, err)
}
return true
}