From f4e95badda3151bfe6dc9c69282701d18f8e45ab Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Fri, 10 Mar 2023 15:39:59 -0800 Subject: [PATCH 1/8] meh --- cmd/server/servers.go | 19 +++++++++++++++++++ internal/manager/start.go | 29 +++++++++++++++++++++++++++++ src/ServerList.tsx | 3 ++- src/app.tsx | 2 ++ src/bridge.ts | 14 +++++++++----- 5 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 internal/manager/start.go diff --git a/cmd/server/servers.go b/cmd/server/servers.go index c6a0a27..db84959 100644 --- a/cmd/server/servers.go +++ b/cmd/server/servers.go @@ -3,10 +3,14 @@ package main import ( "fmt" "net/http" + + "robinplatform.dev/smgr/internal/manager" ) func init() { http.HandleFunc("/api/GetServers", GetServers) + http.HandleFunc("/api/StartServer", StartServer) + } func GetServers(res http.ResponseWriter, req *http.Request) { @@ -16,3 +20,18 @@ func GetServers(res http.ResponseWriter, req *http.Request) { sendJson(res, serverManager.Servers) } } + +func StartServer(res http.ResponseWriter, req *http.Request) { + fmt.Printf("StartServer Running\n") + config := manager.DevServerConfig{ + Command: "/bin/ls", + } + + if err := manager.StartServer(config); err != nil { + sendError(res, 500, fmt.Errorf("failed to discover servers: %w", err)) + } else { + sendJson(res, map[string]any{ + "success": true, + }) + } +} diff --git a/internal/manager/start.go b/internal/manager/start.go new file mode 100644 index 0000000..5e5ece7 --- /dev/null +++ b/internal/manager/start.go @@ -0,0 +1,29 @@ +package manager + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" +) + +func StartServer(config DevServerConfig) error { + jsonValue := []byte(`{ + "appId":"server-manager", + "processKey":"blarg", + "command":"/bin/ls" + }`) + + resp, err := http.Post("http://localhost:9010/api/apps/rpc/StartProcessForApp", "application/json", bytes.NewBuffer(jsonValue)) + if err != nil { + return err + } + + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) + + fmt.Printf("resp body: %s\n", string(body)) + + return nil +} diff --git a/src/ServerList.tsx b/src/ServerList.tsx index 21b7785..e5fab65 100644 --- a/src/ServerList.tsx +++ b/src/ServerList.tsx @@ -40,7 +40,8 @@ export const ServerList: React.FC = () => { {error &&

{String(error)}

}
- {servers?.map((server) => ( + {/* rome-ignore lint/suspicious/noExplicitAny: fuck off for now please, ill fix useRpcQuery later */} + {servers?.map((server: any) => ( { const { selectedServer } = useSelectedServer(); + const { data: res, error } = useRpcQuery("StartServer", {}); return (
diff --git a/src/bridge.ts b/src/bridge.ts index 416bc87..1247a47 100644 --- a/src/bridge.ts +++ b/src/bridge.ts @@ -2,15 +2,19 @@ import { createReactRpcBridge } from "@robinplatform/toolkit/react/rpc"; import { z } from "zod"; export const ServerType = z.object({ - name: z.string(), + name: z.string(), }); export type ServerType = z.infer; const { useRpcQuery } = createReactRpcBridge({ - GetServers: { - input: z.object({}), - output: z.array(ServerType), - }, + GetServers: { + input: z.object({}), + output: z.array(ServerType), + }, + StartServer: { + input: z.object({}), + output: z.any(), + }, }); export { useRpcQuery }; From 4632b5f3064fe27d12044e9132fb029da1c506e1 Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Fri, 10 Mar 2023 15:48:14 -0800 Subject: [PATCH 2/8] meh --- internal/manager/start.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/manager/start.go b/internal/manager/start.go index 5e5ece7..45fed2f 100644 --- a/internal/manager/start.go +++ b/internal/manager/start.go @@ -3,7 +3,7 @@ package manager import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" ) @@ -21,7 +21,7 @@ func StartServer(config DevServerConfig) error { defer resp.Body.Close() - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) fmt.Printf("resp body: %s\n", string(body)) From 67d7315a8cdc06c297da472734dfcd92f4d8e51d Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Mon, 13 Mar 2023 13:31:05 -0700 Subject: [PATCH 3/8] meh --- internal/manager/start.go | 22 +++++++++++++++++++++- src/app.tsx | 21 +++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/internal/manager/start.go b/internal/manager/start.go index 45fed2f..b7af99b 100644 --- a/internal/manager/start.go +++ b/internal/manager/start.go @@ -14,7 +14,27 @@ func StartServer(config DevServerConfig) error { "command":"/bin/ls" }`) - resp, err := http.Post("http://localhost:9010/api/apps/rpc/StartProcessForApp", "application/json", bytes.NewBuffer(jsonValue)) + resp, err := http.Post("http://localhost:9010/api/apps/rpc/StartProcess", "application/json", bytes.NewBuffer(jsonValue)) + if err != nil { + return err + } + + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + + fmt.Printf("resp body: %s\n", string(body)) + + return nil +} + +func StopServer(config DevServerConfig) error { + jsonValue := []byte(`{ + "appId":"server-manager", + "processKey":"blarg" + }`) + + resp, err := http.Post("http://localhost:9010/api/apps/rpc/StopProcess", "application/json", bytes.NewBuffer(jsonValue)) if err != nil { return err } diff --git a/src/app.tsx b/src/app.tsx index e3d3a74..e4220be 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -4,17 +4,34 @@ import "./app.scss"; import { useRpcQuery } from "./bridge"; import "./global.scss"; import { useSelectedServer } from "./hooks/useSelectedServer"; +import { runAppMethod } from "@robinplatform/toolkit"; import { renderApp } from "@robinplatform/toolkit/react"; import "@robinplatform/toolkit/styles.css"; +import { useMutation } from "@tanstack/react-query"; import React from "react"; +import { z } from "zod"; const App = () => { const { selectedServer } = useSelectedServer(); - const { data: res, error } = useRpcQuery("StartServer", {}); + const { mutate } = useMutation({ + mutationKey: ["StartServer"], + mutationFn: () => { + runAppMethod({ + methodName: "StartServer", + resultType: z.any(), + data: { + server: selectedServer, + }, + }); + return null; + }, + }); return (
-
+
+ +
From 2c907f45be9cfd7794b391bdd60cdf02f9e19a9b Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Mon, 13 Mar 2023 15:18:01 -0700 Subject: [PATCH 4/8] move button to control panel --- src/ControlPanel/index.tsx | 18 ++++++++++++++++++ src/app.tsx | 19 ------------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/ControlPanel/index.tsx b/src/ControlPanel/index.tsx index 28616e5..4847f48 100644 --- a/src/ControlPanel/index.tsx +++ b/src/ControlPanel/index.tsx @@ -1,16 +1,34 @@ import { useHistory } from "../hooks/useHistory"; import { useSelectedServer } from "../hooks/useSelectedServer"; +import { runAppMethod } from "@robinplatform/toolkit"; +import { useMutation } from "@tanstack/react-query"; import React from "react"; +import { z } from "zod"; export const ControlPanel: React.FC = () => { const { selectedServer } = useSelectedServer(); const history = useHistory(); + const { mutate } = useMutation({ + mutationKey: ["StartServer"], + mutationFn: () => { + runAppMethod({ + methodName: "StartServer", + resultType: z.any(), + data: { + server: selectedServer, + }, + }); + return null; + }, + }); return (

{selectedServer}

{history.pathname}

+ +
); }; diff --git a/src/app.tsx b/src/app.tsx index e4220be..3880131 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,37 +1,18 @@ import { ControlPanel } from "./ControlPanel"; import { ServerList } from "./ServerList"; import "./app.scss"; -import { useRpcQuery } from "./bridge"; import "./global.scss"; import { useSelectedServer } from "./hooks/useSelectedServer"; -import { runAppMethod } from "@robinplatform/toolkit"; import { renderApp } from "@robinplatform/toolkit/react"; import "@robinplatform/toolkit/styles.css"; -import { useMutation } from "@tanstack/react-query"; import React from "react"; -import { z } from "zod"; const App = () => { const { selectedServer } = useSelectedServer(); - const { mutate } = useMutation({ - mutationKey: ["StartServer"], - mutationFn: () => { - runAppMethod({ - methodName: "StartServer", - resultType: z.any(), - data: { - server: selectedServer, - }, - }); - return null; - }, - }); return (
- -
From 97453a84ef7156e32adb01400a6bdcd963e2421a Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Mon, 13 Mar 2023 15:31:42 -0700 Subject: [PATCH 5/8] fucking hell --- src/ControlPanel/index.tsx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ControlPanel/index.tsx b/src/ControlPanel/index.tsx index 4847f48..5ea26e1 100644 --- a/src/ControlPanel/index.tsx +++ b/src/ControlPanel/index.tsx @@ -8,16 +8,17 @@ import { z } from "zod"; export const ControlPanel: React.FC = () => { const { selectedServer } = useSelectedServer(); const history = useHistory(); - const { mutate } = useMutation({ - mutationKey: ["StartServer"], - mutationFn: () => { - runAppMethod({ + const { mutate } = useMutation({ + mutationKey: ["StartServer", selectedServer], + mutationFn: async (): Promise => { + await runAppMethod({ methodName: "StartServer", resultType: z.any(), data: { server: selectedServer, }, }); + return null; }, }); @@ -28,7 +29,7 @@ export const ControlPanel: React.FC = () => {

{history.pathname}

- +
); }; From 742dd4b3b20a70fa0357efc606c4140c4a1b9540 Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Fri, 17 Mar 2023 13:29:28 -0700 Subject: [PATCH 6/8] meh --- src/ServerList.tsx | 8 +++++--- src/bridge.ts | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ServerList.tsx b/src/ServerList.tsx index e5fab65..0ae1986 100644 --- a/src/ServerList.tsx +++ b/src/ServerList.tsx @@ -10,6 +10,9 @@ const ServerListItem: React.FC<{ onClick: () => void; }> = ({ server, onClick }) => { const { selectedServer } = useSelectedServer(); + const { data: health } = useRpcQuery("CheckServerHealth", { + name: server.name, + }); return ( ); }; @@ -40,8 +43,7 @@ export const ServerList: React.FC = () => { {error &&

{String(error)}

}
- {/* rome-ignore lint/suspicious/noExplicitAny: fuck off for now please, ill fix useRpcQuery later */} - {servers?.map((server: any) => ( + {servers?.map((server) => ( Date: Fri, 17 Mar 2023 15:06:48 -0700 Subject: [PATCH 7/8] some cleanup --- cmd/server/servers.go | 21 ++++++++++- internal/manager/manager.go | 35 +++++++++++++++--- internal/manager/start.go | 74 ++++++++++++++++++++++++++++++++----- 3 files changed, 113 insertions(+), 17 deletions(-) diff --git a/cmd/server/servers.go b/cmd/server/servers.go index db84959..cd26ef2 100644 --- a/cmd/server/servers.go +++ b/cmd/server/servers.go @@ -10,7 +10,7 @@ import ( func init() { http.HandleFunc("/api/GetServers", GetServers) http.HandleFunc("/api/StartServer", StartServer) - + http.HandleFunc("/api/CheckServerHealth", CheckServerHealth) } func GetServers(res http.ResponseWriter, req *http.Request) { @@ -24,7 +24,8 @@ func GetServers(res http.ResponseWriter, req *http.Request) { func StartServer(res http.ResponseWriter, req *http.Request) { fmt.Printf("StartServer Running\n") config := manager.DevServerConfig{ - Command: "/bin/ls", + Name: "hello", + Command: "ls", } if err := manager.StartServer(config); err != nil { @@ -35,3 +36,19 @@ func StartServer(res http.ResponseWriter, req *http.Request) { }) } } + +func CheckServerHealth(res http.ResponseWriter, req *http.Request) { + fmt.Printf("CheckHealth Running\n") + config := manager.DevServerConfig{ + Name: "hello", + Command: "/bin/ls", + } + + if err := manager.CheckServerHealth(config); err != nil { + sendError(res, 500, fmt.Errorf("failed to discover servers: %w", err)) + } else { + sendJson(res, map[string]any{ + "success": true, + }) + } +} diff --git a/internal/manager/manager.go b/internal/manager/manager.go index f90280a..9e82fcf 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -13,8 +13,9 @@ import ( ) type DevServerConfig struct { + Name string `json:"-"` HealthChecks []health.HealthCheck `json:"healthChecks,omitempty"` - Command string `json:"command"` + Command string `json:"command"` } func (config *DevServerConfig) UnmarshalJSON(data []byte) error { @@ -39,8 +40,8 @@ func (config *DevServerConfig) UnmarshalJSON(data []byte) error { type ServerManagerConfig struct { FilePath string `json:"-"` - Name string `json:"name"` - DevServers map[string]DevServerConfig `json:"devServers"` + Name string `json:"name"` + DevServers []DevServerConfig `json:"devServers"` } func (config *ServerManagerConfig) load() error { @@ -54,10 +55,34 @@ func (config *ServerManagerConfig) load() error { return fmt.Errorf("failed to read server config from %s: %w", config.FilePath, err) } - if err := json.Unmarshal(buf, config); err != nil { + type ServerConfigJSON struct { + HealthChecks []health.HealthCheck `json:"healthChecks,omitempty"` + Command string `json:"command"` + } + + type ConfigJSON struct { + Name string `json:"name"` + DevServers map[string]ServerConfigJSON `json:"devServers"` + } + + var configTmp ConfigJSON + if err := json.Unmarshal(buf, &configTmp); err != nil { return fmt.Errorf("failed to unmarshal server config from %s: %w", config.FilePath, err) } + config.Name = configTmp.Name + config.DevServers = make([]DevServerConfig, 0, len(configTmp.DevServers)) + + for name, value := range configTmp.DevServers { + server := DevServerConfig{ + Name: name, + HealthChecks: value.HealthChecks, + Command: value.Command, + } + + config.DevServers = append(config.DevServers, server) + } + if config.Name == "" { config.Name = filepath.Base(filepath.Dir(config.FilePath)) } @@ -93,7 +118,7 @@ func (manager *ServerManager) DiscoverServers(projectPath string) error { if dirEntry.IsDir() { return nil } - + if filepath.Base(filename) == "robin.servers.json" { fmt.Printf("Discovered server config: %s\n", filename) diff --git a/internal/manager/start.go b/internal/manager/start.go index b7af99b..44b6076 100644 --- a/internal/manager/start.go +++ b/internal/manager/start.go @@ -2,17 +2,31 @@ package manager import ( "bytes" + "encoding/json" "fmt" "io" "net/http" ) func StartServer(config DevServerConfig) error { - jsonValue := []byte(`{ - "appId":"server-manager", - "processKey":"blarg", - "command":"/bin/ls" - }`) + type Input struct { + AppId string `json:"appId"` + ProcessKey string `json:"processKey"` + Command string `json:"command"` + Args []string `json:"args"` + } + + input := Input{ + AppId: "server-manager", + ProcessKey: config.Name, + Command: "/bin/bash", + Args: []string{"-c", config.Command}, + } + + jsonValue, err := json.Marshal(input) + if err != nil { + return err + } resp, err := http.Post("http://localhost:9010/api/apps/rpc/StartProcess", "application/json", bytes.NewBuffer(jsonValue)) if err != nil { @@ -23,16 +37,26 @@ func StartServer(config DevServerConfig) error { body, _ := io.ReadAll(resp.Body) - fmt.Printf("resp body: %s\n", string(body)) + fmt.Printf("StartServer resp body: %s\n", string(body)) return nil } func StopServer(config DevServerConfig) error { - jsonValue := []byte(`{ - "appId":"server-manager", - "processKey":"blarg" - }`) + type Input struct { + AppId string `json:"appId"` + ProcessKey string `json:"processKey"` + } + + input := Input{ + AppId: "server-manager", + ProcessKey: config.Name, + } + + jsonValue, err := json.Marshal(input) + if err != nil { + return err + } resp, err := http.Post("http://localhost:9010/api/apps/rpc/StopProcess", "application/json", bytes.NewBuffer(jsonValue)) if err != nil { @@ -47,3 +71,33 @@ func StopServer(config DevServerConfig) error { return nil } + +func CheckServerHealth(config DevServerConfig) error { + type Input struct { + AppId string `json:"appId"` + ProcessKey string `json:"processKey"` + } + + input := Input{ + AppId: "server-manager", + ProcessKey: config.Name, + } + + jsonValue, err := json.Marshal(input) + if err != nil { + return err + } + + resp, err := http.Post("http://localhost:9010/api/apps/rpc/CheckProcessHealth", "application/json", bytes.NewBuffer(jsonValue)) + if err != nil { + return err + } + + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + + fmt.Printf("CheckServer resp body: %s\n", string(body)) + + return nil +} From 7b7359ba28b2faa236d2cfb2866035375becf5a7 Mon Sep 17 00:00:00 2001 From: Albert Liu Date: Fri, 24 Mar 2023 15:09:17 -0700 Subject: [PATCH 8/8] idk --- src/ServerList.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ServerList.tsx b/src/ServerList.tsx index 0ae1986..6f4b01f 100644 --- a/src/ServerList.tsx +++ b/src/ServerList.tsx @@ -10,9 +10,15 @@ const ServerListItem: React.FC<{ onClick: () => void; }> = ({ server, onClick }) => { const { selectedServer } = useSelectedServer(); - const { data: health } = useRpcQuery("CheckServerHealth", { - name: server.name, - }); + const { data: health } = useRpcQuery( + "CheckServerHealth", + { + name: server.name, + }, + { + refetchInterval: 3000, + }, + ); return ( ); };