From 02e39d7ac0ef7e49f9f3ea39347b43c418a06f63 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 21 Jan 2024 13:08:16 +0100 Subject: [PATCH] WIP dynamic start --- api.go | 15 ++- echo_handlers.go | 13 +++ frontend/package.json | 4 +- frontend/src/App.tsx | 23 +++-- frontend/src/screens/Setup.tsx | 40 ++------ frontend/src/screens/Start.tsx | 62 ++++++++++++ frontend/src/types.ts | 3 +- go.mod | 1 + go.sum | 2 + main.go | 177 ++++++++++++--------------------- main_http.go | 8 +- main_wails.go | 2 +- models/api/api.go | 5 + wails_app.go | 1 + wails_handlers.go | 21 ++++ 15 files changed, 208 insertions(+), 169 deletions(-) create mode 100644 frontend/src/screens/Start.tsx diff --git a/api.go b/api.go index 400730fd..a0332a20 100644 --- a/api.go +++ b/api.go @@ -208,10 +208,23 @@ func (svc *Service) ListApps() ([]api.App, error) { func (svc *Service) GetInfo() *api.InfoResponse { info := api.InfoResponse{} info.BackendType = svc.cfg.LNBackendType - info.SetupCompleted = svc.lnClient != nil + info.SetupCompleted = svc.cfg.LNBackendType != "" + info.Running = svc.lnClient != nil return &info } +func (svc *Service) Start(startRequest *api.StartRequest) (*api.InfoResponse, error) { + err := svc.StartApp() + if err != nil { + panic(err) + } + info := api.InfoResponse{} + info.BackendType = svc.cfg.LNBackendType + info.SetupCompleted = svc.lnClient != nil + info.Running = svc.lnClient != nil + return &info, nil +} + func (svc *Service) Setup(setupRequest *api.SetupRequest) error { dbConfig := db.Config{} diff --git a/echo_handlers.go b/echo_handlers.go index fe164745..adb06710 100644 --- a/echo_handlers.go +++ b/echo_handlers.go @@ -48,6 +48,7 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) { e.GET("/api/info", svc.InfoHandler) e.POST("/api/logout", svc.LogoutHandler) e.POST("/api/setup", svc.SetupHandler) + e.POST("/api/start", svc.StartHandler) frontend.RegisterHandlers(e) } @@ -67,6 +68,18 @@ func (svc *Service) InfoHandler(c echo.Context) error { return c.JSON(http.StatusOK, responseBody) } +func (svc *Service) StartHandler(c echo.Context) error { + var startRequest api.StartRequest + if err := c.Bind(&startRequest); err != nil { + return c.JSON(http.StatusBadRequest, ErrorResponse{ + Message: fmt.Sprintf("Bad request: %s", err.Error()), + }) + } + + responseBody, _ := svc.Start(&startRequest) + return c.JSON(http.StatusOK, responseBody) +} + func (svc *Service) LogoutHandler(c echo.Context) error { sess, err := session.Get(CookieName, c) if err != nil { diff --git a/frontend/package.json b/frontend/package.json index 4d53d666..069ac4f9 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -4,9 +4,9 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite", + "dev:http": "yarn prepare:http && vite", "dev:wails": "yarn prepare:wails && VITE_NWC_APP_TYPE=WAILS vite", - "build": "tsc && vite build", + "build:http": "yarn prepare:http && tsc && vite build", "prepare:wails": "cp ./platform_specific/wails/src/utils/request.ts src/utils/request.ts", "prepare:http": "cp ./platform_specific/http/src/utils/request.ts src/utils/request.ts", "build:wails": "yarn prepare:wails && tsc && VITE_NWC_APP_TYPE=WAILS vite build", diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 4cc11f40..3dc3f94c 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -13,6 +13,7 @@ import NotFound from "src/screens/NotFound"; import { useInfo } from "./hooks/useInfo"; import Loading from "./components/Loading"; import { Setup } from "./screens/Setup"; +import Start from "./screens/Start"; function App() { const { data: info } = useInfo(); @@ -21,21 +22,23 @@ function App() { return ; } + let home; + if (info?.setupCompleted && info.running) { + home = "/apps"; + } else if (info.setupCompleted && !info.running) { + home = "/start"; + } else { + home = "/setup"; + } + return ( -
+
}> - - } - /> + } /> + } /> } /> } /> } /> diff --git a/frontend/src/screens/Setup.tsx b/frontend/src/screens/Setup.tsx index 4939f75d..1807a0f0 100644 --- a/frontend/src/screens/Setup.tsx +++ b/frontend/src/screens/Setup.tsx @@ -1,6 +1,6 @@ import React from "react"; import { useNavigate } from "react-router-dom"; -import Loading from "src/components/Loading"; +import ConnectButton from "src/components/ConnectButton"; import { useCSRF } from "src/hooks/useCSRF"; import { useInfo } from "src/hooks/useInfo"; import { BackendType } from "src/types"; @@ -62,7 +62,7 @@ export function Setup() { value={backendType} onChange={(e) => setBackendType(e.target.value as BackendType)} id="backend-type" - className="mb-4 bg-gray-50 border border-gray-300 text-gray-900 focus:ring-purple-700 dark:focus:ring-purple-600 dark:ring-offset-gray-800 focus:ring-2 text-sm rounded-lg block w-full p-2.5 dark:bg-surface-00dp dark:border-gray-700 dark:placeholder-gray-400 dark:text-white" + className="dark:bg-surface-00dp mb-4 block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:ring-2 focus:ring-purple-700 dark:border-gray-700 dark:text-white dark:placeholder-gray-400 dark:ring-offset-gray-800 dark:focus:ring-purple-600" > @@ -78,30 +78,6 @@ export function Setup() { ); } -type ConnectButtonProps = { - isConnecting: boolean; -}; - -function ConnectButton({ isConnecting }: ConnectButtonProps) { - return ( - - ); -} - type SetupFormProps = { isConnecting: boolean; handleSubmit(data: unknown): void; @@ -141,7 +117,7 @@ function BreezForm({ isConnecting, handleSubmit }: SetupFormProps) { value={greenlightInviteCode} type="password" id="greenlight-invite-code" - className="bg-gray-50 border border-gray-300 text-gray-900 focus:ring-purple-700 dark:focus:ring-purple-600 dark:ring-offset-gray-800 focus:ring-2 text-sm rounded-lg block w-full p-2.5 dark:bg-surface-00dp dark:border-gray-700 dark:placeholder-gray-400 dark:text-white" + className="dark:bg-surface-00dp block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:ring-2 focus:ring-purple-700 dark:border-gray-700 dark:text-white dark:placeholder-gray-400 dark:ring-offset-gray-800 dark:focus:ring-purple-600" />