diff --git a/packages/cli/app/cmd/root.go b/packages/cli/app/cmd/root.go index cbf7694..5c6a72c 100644 --- a/packages/cli/app/cmd/root.go +++ b/packages/cli/app/cmd/root.go @@ -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, diff --git a/packages/cli/internal/static/static.go b/packages/cli/internal/static/static.go index 73a4deb..a174c84 100644 --- a/packages/cli/internal/static/static.go +++ b/packages/cli/internal/static/static.go @@ -2,9 +2,11 @@ package static import ( "embed" + "encoding/json" "fmt" "io/fs" "net/http" + "strings" "github.com/kataras/iris/v12" ) @@ -12,10 +14,9 @@ import ( //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 { @@ -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("", configJSON) + modifiedContent := strings.ReplaceAll(string(content), "", configScript) + + ctx.ContentType("text/html") + ctx.WriteString(modifiedContent) + return + } + + iris.FileServer(http.FS(assets), iris.DirOptions{ + IndexName: "index.html", + })(ctx) + } } diff --git a/packages/frontend/index.html b/packages/frontend/index.html index 15bebdd..a6959b5 100644 --- a/packages/frontend/index.html +++ b/packages/frontend/index.html @@ -5,6 +5,7 @@