Skip to content

Commit

Permalink
WIP dynamic start
Browse files Browse the repository at this point in the history
  • Loading branch information
bumi committed Jan 21, 2024
1 parent 904893c commit 02e39d7
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 169 deletions.
15 changes: 14 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
13 changes: 13 additions & 0 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 13 additions & 10 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -21,21 +22,23 @@ function App() {
return <Loading />;
}

let home;
if (info?.setupCompleted && info.running) {
home = "/apps";
} else if (info.setupCompleted && !info.running) {
home = "/start";
} else {
home = "/setup";
}

return (
<div className="bg:white dark:bg-black min-h-full">
<div className="bg:white min-h-full dark:bg-black">
<Toaster />
<HashRouter>
<Routes>
<Route path="/" element={<Navbar />}>
<Route
index
element={
<Navigate
to={info.setupCompleted ? "/apps" : "/setup"}
replace
/>
}
/>
<Route index element={<Navigate to={home} replace />} />
<Route path="start" element={<Start />} />
<Route path="setup" element={<Setup />} />
<Route path="apps" element={<AppsList />} />
<Route path="apps/:pubkey" element={<ShowApp />} />
Expand Down
40 changes: 8 additions & 32 deletions frontend/src/screens/Setup.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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"
>
<option value={"BREEZ"}>Breez</option>
<option value={"LND"}>LND</option>
Expand All @@ -78,30 +78,6 @@ export function Setup() {
);
}

type ConnectButtonProps = {
isConnecting: boolean;
};

function ConnectButton({ isConnecting }: ConnectButtonProps) {
return (
<button
type="submit"
className={`mt-4 gap-2 inline-flex w-full ${
isConnecting ? "bg-gray-300 dark:bg-gray-700" : "bg-purple-700"
} cursor-pointer dark:text-neutral-200 duration-150 focus-visible:ring-2 focus-visible:ring-offset-2 focus:outline-none font-medium hover:bg-purple-900 items-center justify-center px-5 py-3 rounded-md shadow text-white transition`}
disabled={isConnecting}
>
{isConnecting ? (
<>
<Loading /> Connecting...
</>
) : (
<>Connect</>
)}
</button>
);
}

type SetupFormProps = {
isConnecting: boolean;
handleSubmit(data: unknown): void;
Expand Down Expand Up @@ -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"
/>
<label
htmlFor="breez-api-key"
Expand All @@ -155,7 +131,7 @@ function BreezForm({ isConnecting, handleSubmit }: SetupFormProps) {
value={breezApiKey}
type="password"
id="breez-api-key"
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"
/>
<label
htmlFor="mnemonic"
Expand All @@ -169,7 +145,7 @@ function BreezForm({ isConnecting, handleSubmit }: SetupFormProps) {
value={breezMnemonic}
type="password"
id="mnemonic"
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"
/>
</>
<ConnectButton isConnecting={isConnecting} />
Expand Down Expand Up @@ -209,7 +185,7 @@ function LNDForm({ isConnecting, handleSubmit }: SetupFormProps) {
onChange={(e) => setLndAddress(e.target.value)}
value={lndAddress}
id="lnd-address"
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"
/>

<label
Expand All @@ -224,7 +200,7 @@ function LNDForm({ isConnecting, handleSubmit }: SetupFormProps) {
value={lndCertHex}
type="password"
id="lnd-cert-hex"
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"
/>
<label
htmlFor="lnd-macaroon-hex"
Expand All @@ -238,7 +214,7 @@ function LNDForm({ isConnecting, handleSubmit }: SetupFormProps) {
value={lndMacaroonHex}
type="password"
id="lnd-macaroon-hex"
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"
/>
</>
<ConnectButton isConnecting={isConnecting} />
Expand Down
62 changes: 62 additions & 0 deletions frontend/src/screens/Start.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import { useCSRF } from "src/hooks/useCSRF";
import { request, handleRequestError } from "src/utils/request";
import ConnectButton from "src/components/ConnectButton";

export default function Start() {
const [unlockPassword, setUnlockPassword] = React.useState("");
const [loading, setLoading] = React.useState(false);
const navigate = useNavigate();
const { data: csrf } = useCSRF();

async function onSubmit(e: React.FormEvent) {
e.preventDefault();
try {
setLoading(true);
if (!csrf) {
throw new Error("info not loaded");
}
const res = await request("/api/start", {
method: "POST",
headers: {
"X-CSRF-Token": csrf,
"Content-Type": "application/json",
},
body: JSON.stringify({
unlockPassword,
}),
});
console.log({ res });

navigate("/apps");
} catch (error) {
handleRequestError("Failed to connect", error);
} finally {
setLoading(false);
}
}

return (
<>
<form onSubmit={onSubmit}>
<>
<label
htmlFor="greenlight-invite-code"
className="block font-medium text-gray-900 dark:text-white"
>
Unlock password
</label>
<input
name="unlock"
onChange={(e) => setUnlockPassword(e.target.value)}
value={unlockPassword}
type="password"
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"
/>
<ConnectButton isConnecting={loading} />
</>
</form>
</>
);
}
3 changes: 2 additions & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type BudgetRenewalType = "daily" | "weekly" | "monthly" | "yearly" | "";

export type IconMap = {
[key in RequestMethodType]: (
props: React.SVGAttributes<SVGElement>
props: React.SVGAttributes<SVGElement>,
) => JSX.Element;
};

Expand Down Expand Up @@ -89,6 +89,7 @@ export interface NostrEvent {
export interface InfoResponse {
backendType: BackendType;
setupCompleted: boolean;
running: boolean;
}

export interface CreateAppResponse {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/labstack/echo/v4 v4.10.2
github.com/nbd-wtf/go-nostr v0.25.5
github.com/nbd-wtf/ln-decodepay v1.11.1
github.com/orandin/lumberjackrus v1.0.1
github.com/stretchr/testify v1.8.2
github.com/wailsapp/wails/v2 v2.7.1
google.golang.org/grpc v1.53.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/orandin/lumberjackrus v1.0.1 h1:7ysDQ0MHD79zIFN9/EiDHjUcgopNi5ehtxFDy8rUkWo=
github.com/orandin/lumberjackrus v1.0.1/go.mod h1:xYLt6H8W93pKnQgUQaxsApS0Eb4BwHLOkxk5DVzf5H0=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pierrec/lz4/v4 v4.0.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
Expand Down
Loading

0 comments on commit 02e39d7

Please sign in to comment.