Skip to content

Commit

Permalink
Fix Web UI logs in compiled server
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Dec 9, 2023
1 parent 71a425c commit 1331145
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crates/librqbit/webui/dist/assets/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/librqbit/webui/dist/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"src": "assets/logo.svg"
},
"index.html": {
"file": "assets/index-b02909de.js",
"file": "assets/index-b804d1c8.js",
"isEntry": true,
"src": "index.html"
}
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/webui/src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export interface JSONLogLine {
}

export interface RqbitAPI {
getHttpBaseUrl: () => string | null;
getStreamLogsUrl: () => string | null;
listTorrents: () => Promise<ListTorrentsResponse>;
getTorrentDetails: (index: number) => Promise<TorrentDetails>;
getTorrentStats: (index: number) => Promise<TorrentStats>;
Expand Down
19 changes: 6 additions & 13 deletions crates/librqbit/webui/src/components/LogStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { LogLine } from "./LogLine";
import { JSONLogLine } from "../api-types";

interface LogStreamProps {
httpApiBase: string;
url: string;
maxLines?: number;
}

Expand All @@ -39,7 +39,7 @@ const mergeBuffers = (a1: Uint8Array, a2: Uint8Array): Uint8Array => {
};

const streamLogs = (
httpApiBase: string,
url: string,
addLine: (text: string) => void,
setError: (error: ErrorWithLabel | null) => void
): (() => void) => {
Expand All @@ -55,7 +55,7 @@ const streamLogs = (
};

const runOnce = async () => {
let response = await fetch(httpApiBase + "/stream_logs", { signal });
let response = await fetch(url, { signal });

if (!response.ok) {
let text = await response.text();
Expand Down Expand Up @@ -128,10 +128,7 @@ const streamLogs = (
};
};

export const LogStream: React.FC<LogStreamProps> = ({
httpApiBase,
maxLines,
}) => {
export const LogStream: React.FC<LogStreamProps> = ({ url, maxLines }) => {
const [logLines, setLogLines] = useState<Line[]>([]);
const [error, setError] = useState<ErrorWithLabel | null>(null);
const [filter, setFilter] = useState<string>("");
Expand Down Expand Up @@ -189,12 +186,8 @@ export const LogStream: React.FC<LogStreamProps> = ({
useEffect(() => updateFilter.cancel, []);

useEffect(() => {
return streamLogs(
httpApiBase,
(line) => addLineRef.current(line),
setError
);
}, [httpApiBase]);
return streamLogs(url, (line) => addLineRef.current(line), setError);
}, [url]);

return (
<div>
Expand Down
6 changes: 3 additions & 3 deletions crates/librqbit/webui/src/components/LogStreamModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ interface Props {

export const LogStreamModal: React.FC<Props> = ({ show, onClose }) => {
const api = useContext(APIContext);
const apiBase = api.getHttpBaseUrl();
let logsUrl = api.getStreamLogsUrl();

return (
<Modal size="xl" show={show} onHide={onClose}>
<Modal.Header closeButton>
<Modal.Title>rqbit server logs</Modal.Title>
</Modal.Header>
<Modal.Body>
{apiBase ? (
<LogStream httpApiBase={apiBase} />
{logsUrl ? (
<LogStream url={logsUrl} />
) : (
<ErrorComponent
error={{ text: "HTTP API not available to stream logs" }}
Expand Down
4 changes: 2 additions & 2 deletions crates/librqbit/webui/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export const APIContext = createContext<RqbitAPI>({
delete: () => {
throw new Error("Function not implemented.");
},
getHttpBaseUrl: () => {
throw new Error("Function not implemented.");
getStreamLogsUrl: () => {
return null;
},
});
export const AppContext = createContext<ContextType>({
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/webui/src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const makeRequest = async (
};

export const API: RqbitAPI & { getVersion: () => Promise<string> } = {
getHttpBaseUrl: () => apiUrl,
getStreamLogsUrl: () => apiUrl + "/stream_logs",
listTorrents: (): Promise<ListTorrentsResponse> =>
makeRequest("GET", "/torrents"),
getTorrentDetails: (index: number): Promise<TorrentDetails> => {
Expand Down
13 changes: 9 additions & 4 deletions desktop/src/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ async function readFileAsBase64(file: File): Promise<string> {

export const makeAPI = (configuration: RqbitDesktopConfig): RqbitAPI => {
return {
getHttpBaseUrl: () => {
return configuration.http_api.listen_addr
? `http://${configuration.http_api.listen_addr}`
: null;
getStreamLogsUrl: () => {
if (!configuration.http_api.listen_addr) {
return null;
}
let port = configuration.http_api.listen_addr.split(":")[1];
if (!port) {
return null;
}
return `http://127.0.0.1:${port}/stream_logs`;
},
listTorrents: async function (): Promise<ListTorrentsResponse> {
return await invokeAPI<ListTorrentsResponse>("torrents_list");
Expand Down

0 comments on commit 1331145

Please sign in to comment.