mirror of
https://github.com/henrygd/beszel.git
synced 2026-03-24 14:36:17 +01:00
Compare commits
3 Commits
ssr-system
...
e9fb9b856f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9fb9b856f | ||
|
|
66bca11d36 | ||
|
|
86e87f0d47 |
@@ -112,6 +112,8 @@ func (h *Hub) initialize(e *core.ServeEvent) error {
|
|||||||
// set URL if BASE_URL env is set
|
// set URL if BASE_URL env is set
|
||||||
if h.appURL != "" {
|
if h.appURL != "" {
|
||||||
settings.Meta.AppURL = h.appURL
|
settings.Meta.AppURL = h.appURL
|
||||||
|
} else {
|
||||||
|
h.appURL = settings.Meta.AppURL
|
||||||
}
|
}
|
||||||
if err := e.App.Save(settings); err != nil {
|
if err := e.App.Save(settings); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -3,18 +3,74 @@
|
|||||||
package hub
|
package hub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"beszel"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Wraps http.RoundTripper to modify dev proxy HTML responses
|
||||||
|
type responseModifier struct {
|
||||||
|
transport http.RoundTripper
|
||||||
|
hub *Hub
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *responseModifier) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
resp, err := rm.transport.RoundTrip(req)
|
||||||
|
if err != nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
// Only modify HTML responses
|
||||||
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
if !strings.Contains(contentType, "text/html") {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
// Create a new response with the modified body
|
||||||
|
modifiedBody := rm.modifyHTML(string(body))
|
||||||
|
resp.Body = io.NopCloser(strings.NewReader(modifiedBody))
|
||||||
|
resp.ContentLength = int64(len(modifiedBody))
|
||||||
|
resp.Header.Set("Content-Length", fmt.Sprintf("%d", len(modifiedBody)))
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *responseModifier) modifyHTML(html string) string {
|
||||||
|
parsedURL, err := url.Parse(rm.hub.appURL)
|
||||||
|
if err != nil {
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
// fix base paths in html if using subpath
|
||||||
|
basePath := strings.TrimSuffix(parsedURL.Path, "/") + "/"
|
||||||
|
html = strings.ReplaceAll(html, "./", basePath)
|
||||||
|
html = strings.Replace(html, "{{V}}", beszel.Version, 1)
|
||||||
|
html = strings.Replace(html, "{{HUB_URL}}", rm.hub.appURL, 1)
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
|
||||||
// startServer sets up the development server for Beszel
|
// startServer sets up the development server for Beszel
|
||||||
func (h *Hub) startServer(se *core.ServeEvent) error {
|
func (h *Hub) startServer(se *core.ServeEvent) error {
|
||||||
|
slog.Info("starting server", "appURL", h.appURL)
|
||||||
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
Host: "localhost:5173",
|
Host: "localhost:5173",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
proxy.Transport = &responseModifier{
|
||||||
|
transport: http.DefaultTransport,
|
||||||
|
hub: h,
|
||||||
|
}
|
||||||
|
|
||||||
se.Router.GET("/{path...}", func(e *core.RequestEvent) error {
|
se.Router.GET("/{path...}", func(e *core.RequestEvent) error {
|
||||||
proxy.ServeHTTP(e.Response, e.Request)
|
proxy.ServeHTTP(e.Response, e.Request)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ func (h *Hub) startServer(se *core.ServeEvent) error {
|
|||||||
// fix base paths in html if using subpath
|
// fix base paths in html if using subpath
|
||||||
basePath := strings.TrimSuffix(parsedURL.Path, "/") + "/"
|
basePath := strings.TrimSuffix(parsedURL.Path, "/") + "/"
|
||||||
indexFile, _ := fs.ReadFile(site.DistDirFS, "index.html")
|
indexFile, _ := fs.ReadFile(site.DistDirFS, "index.html")
|
||||||
indexContent := strings.ReplaceAll(string(indexFile), "./", basePath)
|
html := strings.ReplaceAll(string(indexFile), "./", basePath)
|
||||||
indexContent = strings.Replace(indexContent, "{{V}}", beszel.Version, 1)
|
html = strings.Replace(html, "{{V}}", beszel.Version, 1)
|
||||||
indexContent = strings.Replace(indexContent, "{{HUB_URL}}", h.appURL, 1)
|
html = strings.Replace(html, "{{HUB_URL}}", h.appURL, 1)
|
||||||
// set up static asset serving
|
// set up static asset serving
|
||||||
staticPaths := [2]string{"/static/", "/assets/"}
|
staticPaths := [2]string{"/static/", "/assets/"}
|
||||||
serveStatic := apis.Static(site.DistDirFS, false)
|
serveStatic := apis.Static(site.DistDirFS, false)
|
||||||
@@ -45,7 +45,7 @@ func (h *Hub) startServer(se *core.ServeEvent) error {
|
|||||||
e.Response.Header().Del("X-Frame-Options")
|
e.Response.Header().Del("X-Frame-Options")
|
||||||
e.Response.Header().Set("Content-Security-Policy", csp)
|
e.Response.Header().Set("Content-Security-Policy", csp)
|
||||||
}
|
}
|
||||||
return e.HTML(http.StatusOK, indexContent)
|
return e.HTML(http.StatusOK, html)
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import path from "path"
|
|||||||
import tailwindcss from "@tailwindcss/vite"
|
import tailwindcss from "@tailwindcss/vite"
|
||||||
import react from "@vitejs/plugin-react-swc"
|
import react from "@vitejs/plugin-react-swc"
|
||||||
import { lingui } from "@lingui/vite-plugin"
|
import { lingui } from "@lingui/vite-plugin"
|
||||||
import { version } from "./package.json"
|
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
base: "./",
|
base: "./",
|
||||||
@@ -13,13 +12,6 @@ export default defineConfig({
|
|||||||
}),
|
}),
|
||||||
lingui(),
|
lingui(),
|
||||||
tailwindcss(),
|
tailwindcss(),
|
||||||
{
|
|
||||||
name: "replace version in index.html during dev",
|
|
||||||
apply: "serve",
|
|
||||||
transformIndexHtml(html) {
|
|
||||||
return html.replace("{{V}}", version).replace("{{HUB_URL}}", "")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
esbuild: {
|
esbuild: {
|
||||||
legalComments: "external",
|
legalComments: "external",
|
||||||
|
|||||||
@@ -216,11 +216,11 @@ if [ "$UNINSTALL" = true ]; then
|
|||||||
echo "Removing the OpenRC service files..."
|
echo "Removing the OpenRC service files..."
|
||||||
rm -f /etc/init.d/beszel-agent
|
rm -f /etc/init.d/beszel-agent
|
||||||
|
|
||||||
# Remove the update service if it exists
|
# Remove the daily update cron job if it exists
|
||||||
echo "Removing the daily update service..."
|
echo "Removing the daily update cron job..."
|
||||||
rc-service beszel-agent-update stop 2>/dev/null
|
if crontab -u root -l 2>/dev/null | grep -q "beszel-agent.*update"; then
|
||||||
rc-update del beszel-agent-update default 2>/dev/null
|
crontab -u root -l 2>/dev/null | grep -v "beszel-agent.*update" | crontab -u root -
|
||||||
rm -f /etc/init.d/beszel-agent-update
|
fi
|
||||||
|
|
||||||
# Remove log files
|
# Remove log files
|
||||||
echo "Removing log files..."
|
echo "Removing log files..."
|
||||||
@@ -321,6 +321,9 @@ if [ -z "$KEY" ]; then
|
|||||||
read KEY
|
read KEY
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Remove newlines from KEY
|
||||||
|
KEY=$(echo "$KEY" | tr -d '\n')
|
||||||
|
|
||||||
# TOKEN and HUB_URL are optional for backwards compatibility - no interactive prompts
|
# TOKEN and HUB_URL are optional for backwards compatibility - no interactive prompts
|
||||||
# They will be set as empty environment variables if not provided
|
# They will be set as empty environment variables if not provided
|
||||||
|
|
||||||
@@ -523,35 +526,19 @@ EOF
|
|||||||
elif [ "$AUTO_UPDATE_FLAG" = "false" ]; then
|
elif [ "$AUTO_UPDATE_FLAG" = "false" ]; then
|
||||||
AUTO_UPDATE="n"
|
AUTO_UPDATE="n"
|
||||||
else
|
else
|
||||||
printf "\nWould you like to enable automatic daily updates for beszel-agent? (y/n): "
|
printf "\nEnable automatic daily updates for beszel-agent? (y/n): "
|
||||||
read AUTO_UPDATE
|
read AUTO_UPDATE
|
||||||
fi
|
fi
|
||||||
case "$AUTO_UPDATE" in
|
case "$AUTO_UPDATE" in
|
||||||
[Yy]*)
|
[Yy]*)
|
||||||
echo "Setting up daily automatic updates for beszel-agent..."
|
echo "Setting up daily automatic updates for beszel-agent..."
|
||||||
|
|
||||||
cat >/etc/init.d/beszel-agent-update <<EOF
|
# Create cron job to run beszel-agent update command daily at midnight
|
||||||
#!/sbin/openrc-run
|
if ! crontab -u root -l 2>/dev/null | grep -q "beszel-agent.*update"; then
|
||||||
|
(crontab -u root -l 2>/dev/null; echo "12 0 * * * /opt/beszel-agent/beszel-agent update >/dev/null 2>&1") | crontab -u root -
|
||||||
|
fi
|
||||||
|
|
||||||
name="beszel-agent-update"
|
printf "\nDaily updates have been enabled via cron job.\n"
|
||||||
description="Update beszel-agent if needed"
|
|
||||||
|
|
||||||
depend() {
|
|
||||||
need beszel-agent
|
|
||||||
}
|
|
||||||
|
|
||||||
start() {
|
|
||||||
ebegin "Checking for beszel-agent updates"
|
|
||||||
/opt/beszel-agent/beszel-agent update
|
|
||||||
eend $?
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
|
|
||||||
chmod +x /etc/init.d/beszel-agent-update
|
|
||||||
rc-update add beszel-agent-update default
|
|
||||||
rc-service beszel-agent-update start
|
|
||||||
|
|
||||||
printf "\nAutomatic daily updates have been enabled.\n"
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@@ -612,7 +599,7 @@ EOF
|
|||||||
AUTO_UPDATE="n"
|
AUTO_UPDATE="n"
|
||||||
sleep 1 # give time for the service to start
|
sleep 1 # give time for the service to start
|
||||||
else
|
else
|
||||||
printf "\nWould you like to enable automatic daily updates for beszel-agent? (y/n): "
|
printf "\nEnable automatic daily updates for beszel-agent? (y/n): "
|
||||||
read AUTO_UPDATE
|
read AUTO_UPDATE
|
||||||
fi
|
fi
|
||||||
case "$AUTO_UPDATE" in
|
case "$AUTO_UPDATE" in
|
||||||
@@ -620,12 +607,12 @@ EOF
|
|||||||
echo "Setting up daily automatic updates for beszel-agent..."
|
echo "Setting up daily automatic updates for beszel-agent..."
|
||||||
|
|
||||||
cat >/etc/crontabs/beszel <<EOF
|
cat >/etc/crontabs/beszel <<EOF
|
||||||
0 0 * * * /etc/init.d/beszel-agent update
|
12 0 * * * /etc/init.d/beszel-agent update
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
/etc/init.d/cron restart
|
/etc/init.d/cron restart
|
||||||
|
|
||||||
printf "\nAutomatic daily updates have been enabled.\n"
|
printf "\nDaily updates have been enabled.\n"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@@ -695,7 +682,7 @@ EOF
|
|||||||
AUTO_UPDATE="n"
|
AUTO_UPDATE="n"
|
||||||
sleep 1 # give time for the service to start
|
sleep 1 # give time for the service to start
|
||||||
else
|
else
|
||||||
printf "\nWould you like to enable automatic daily updates for beszel-agent? (y/n): "
|
printf "\nEnable automatic daily updates for beszel-agent? (y/n): "
|
||||||
read AUTO_UPDATE
|
read AUTO_UPDATE
|
||||||
fi
|
fi
|
||||||
case "$AUTO_UPDATE" in
|
case "$AUTO_UPDATE" in
|
||||||
@@ -730,7 +717,7 @@ EOF
|
|||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl enable --now beszel-agent-update.timer
|
systemctl enable --now beszel-agent-update.timer
|
||||||
|
|
||||||
printf "\nAutomatic daily updates have been enabled.\n"
|
printf "\nDaily updates have been enabled.\n"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user