Skip to content

Commit

Permalink
pass dynamic port to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
marhaupe committed Sep 12, 2024
1 parent 0a452bb commit 66daca5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
7 changes: 5 additions & 2 deletions packages/cli/app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ type Root struct {
func New(command string) *Root {
logsService := logs.New()
return &Root{
logService: logsService,
staticServer: static.New(),
logService: logsService,
staticServer: &static.Static{
Command: command,
Port: port,
},
executor: &executor.Executor{
Command: command,
Out: logsService,
Expand Down
47 changes: 39 additions & 8 deletions packages/cli/internal/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package static

import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"strings"

"github.com/kataras/iris/v12"
)

//go:embed all:dist
var dist embed.FS

type Static struct{}

func New() *Static {
return &Static{}
type Static struct {
Command string
Port int
}

func (s *Static) Handler() iris.Handler {
Expand All @@ -28,8 +29,38 @@ func (s *Static) Handler() iris.Handler {
}
}

return iris.FileServer(http.FS(assets), iris.DirOptions{
IndexName: "index.html",
ShowList: true, // Enable directory listing for debugging
})
return func(ctx iris.Context) {
path := ctx.Request().URL.Path
if path == "/" || path == "/index.html" {
content, err := fs.ReadFile(assets, "index.html")
if err != nil {
ctx.StatusCode(http.StatusInternalServerError)
ctx.WriteString("Error reading index.html")
return
}
config := struct {
Command string `json:"command"`
Port int `json:"port"`
}{
Command: s.Command,
Port: s.Port,
}
configJSON, err := json.Marshal(config)
if err != nil {
ctx.StatusCode(http.StatusInternalServerError)
ctx.WriteString("Error marshalling config")
return
}
configScript := fmt.Sprintf("<script>window.config = %s;</script>", configJSON)
modifiedContent := strings.ReplaceAll(string(content), "<!--#echo var=\"configscript\" -->", configScript)

ctx.ContentType("text/html")
ctx.WriteString(modifiedContent)
return
}

iris.FileServer(http.FS(assets), iris.DirOptions{
IndexName: "index.html",
})(ctx)
}
}
1 change: 1 addition & 0 deletions packages/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/icon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>uitail</title>
<!--#echo var="configscript" -->
</head>
<body>
<div id="root"></div>
Expand Down
16 changes: 13 additions & 3 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ export function App() {

const logListRef = useRef<LogListRef>(null);

useEffect(() => {
const prevTitle = document.title;
if (config.command) {
document.title = "uitail - " + config.command;
}
return () => {
document.title = prevTitle;
};
}, []);

async function handleClear() {
try {
const response = await fetch(`${config.backendUrl}${config.routes.clear}`, {
const response = await fetch(`http://localhost:${config.port}${config.routes.clear}`, {
method: "POST",
});
if (response.ok) {
Expand All @@ -36,7 +46,7 @@ export function App() {

async function handleRestart() {
try {
const response = await fetch(`${config.backendUrl}${config.routes.restart}`, {
const response = await fetch(`http://localhost:${config.port}${config.routes.restart}`, {
method: "POST",
});
if (response.ok) {
Expand All @@ -48,7 +58,7 @@ export function App() {
}

useEffect(() => {
const url = new URL(config.backendUrl + config.routes.events);
const url = new URL(`http://localhost:${config.port}${config.routes.events}`);
url.searchParams.set("stream", nanoid());
if (filterState.message) {
url.searchParams.set("filter", filterState.message);
Expand Down
14 changes: 13 additions & 1 deletion packages/frontend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
declare global {
interface Window {
config: {
port: number;
command: string;
};
}
}

export const config = {
backendUrl: "http://localhost:8787",
routes: {
events: "/events",
restart: "/restart",
clear: "/clear",
},
...(window.config || {
port: 8765,
command: "",
}),
};

0 comments on commit 66daca5

Please sign in to comment.