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
|
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"sync"
)
//go:embed dist
var distFS embed.FS
func main() {
port := flag.Int("port", -1, "port to listen on (default 5252, or random when -open is used)")
openFile := flag.String("open", "", "profile file to open directly in the browser")
flag.Parse()
// Determine the default port: random (0) when opening a file one-shot,
// fixed 5252 when running as a persistent server.
if *port == -1 {
if *openFile != "" {
*port = 0
} else {
*port = 5252
}
}
sub, err := fs.Sub(distFS, "dist")
if err != nil {
log.Fatalf("failed to sub dist FS: %v", err)
}
indexHTML, err := fs.ReadFile(sub, "index.html")
if err != nil {
log.Fatalf("failed to read dist/index.html: %v", err)
}
mux := http.NewServeMux()
// API stubs — all return 501 Not Implemented.
for _, pattern := range []string{
"POST /compressed-store",
"DELETE /profile/",
"POST /shorten",
"POST /expand",
} {
mux.HandleFunc(pattern, apiStub)
}
// Health / diagnostic endpoints.
mux.HandleFunc("/__lbheartbeat__", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, "OK")
})
mux.HandleFunc("/__heartbeat__", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, "OK")
})
mux.HandleFunc("/__version__", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"version": "local"})
})
// One-shot profile endpoint: serves the file once, then returns 410 Gone.
if *openFile != "" {
profileData, err := os.ReadFile(*openFile)
if err != nil {
log.Fatalf("failed to read profile file %q: %v", *openFile, err)
}
var serveOnce sync.Once
mux.HandleFunc("GET /cli/open/profile", func(w http.ResponseWriter, r *http.Request) {
served := false
serveOnce.Do(func() {
served = true
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(profileData)
log.Printf("profile served, route deactivated")
})
if !served {
http.Error(w, "profile already fetched", http.StatusGone)
}
})
}
// Static frontend — serve dist/ with SPA fallback to index.html.
fileServer := http.FileServer(http.FS(sub))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
if path == "" {
// Root — serve index.html directly (avoids FileServer's index redirect).
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(indexHTML)
return
}
if _, err := sub.Open(path); err != nil {
// File not found — serve index.html for client-side routing.
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(indexHTML)
return
}
fileServer.ServeHTTP(w, r)
})
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
actualPort := ln.Addr().(*net.TCPAddr).Port
log.Printf("firefox-profiler listening on http://localhost:%d", actualPort)
if *openFile != "" {
profileURL := fmt.Sprintf("http://localhost:%d/cli/open/profile", actualPort)
openURL := fmt.Sprintf("http://localhost:%d/from-url/%s",
actualPort, url.QueryEscape(profileURL))
log.Printf("opening %s", openURL)
if err := openBrowser(openURL); err != nil {
log.Printf("warning: could not open browser: %v", err)
log.Printf("open manually: %s", openURL)
}
}
if err := http.Serve(ln, mux); err != nil {
log.Fatalf("server error: %v", err)
}
}
func openBrowser(u string) error {
if browser := os.Getenv("BROWSER"); browser != "" {
return exec.Command(browser, u).Start()
}
return exec.Command("xdg-open", u).Start()
}
func apiStub(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotImplemented)
json.NewEncoder(w).Encode(map[string]string{
"error": "This endpoint is not implemented in the local standalone server.",
})
}
|