Fix Web UI logs in compiled server

This commit is contained in:
Igor Katson 2023-12-09 13:50:56 +00:00
parent 71a425ce66
commit 1331145333
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
8 changed files with 25 additions and 27 deletions

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,7 @@
"src": "assets/logo.svg" "src": "assets/logo.svg"
}, },
"index.html": { "index.html": {
"file": "assets/index-b02909de.js", "file": "assets/index-b804d1c8.js",
"isEntry": true, "isEntry": true,
"src": "index.html" "src": "index.html"
} }

View file

@ -159,7 +159,7 @@ export interface JSONLogLine {
} }
export interface RqbitAPI { export interface RqbitAPI {
getHttpBaseUrl: () => string | null; getStreamLogsUrl: () => string | null;
listTorrents: () => Promise<ListTorrentsResponse>; listTorrents: () => Promise<ListTorrentsResponse>;
getTorrentDetails: (index: number) => Promise<TorrentDetails>; getTorrentDetails: (index: number) => Promise<TorrentDetails>;
getTorrentStats: (index: number) => Promise<TorrentStats>; getTorrentStats: (index: number) => Promise<TorrentStats>;

View file

@ -14,7 +14,7 @@ import { LogLine } from "./LogLine";
import { JSONLogLine } from "../api-types"; import { JSONLogLine } from "../api-types";
interface LogStreamProps { interface LogStreamProps {
httpApiBase: string; url: string;
maxLines?: number; maxLines?: number;
} }
@ -39,7 +39,7 @@ const mergeBuffers = (a1: Uint8Array, a2: Uint8Array): Uint8Array => {
}; };
const streamLogs = ( const streamLogs = (
httpApiBase: string, url: string,
addLine: (text: string) => void, addLine: (text: string) => void,
setError: (error: ErrorWithLabel | null) => void setError: (error: ErrorWithLabel | null) => void
): (() => void) => { ): (() => void) => {
@ -55,7 +55,7 @@ const streamLogs = (
}; };
const runOnce = async () => { const runOnce = async () => {
let response = await fetch(httpApiBase + "/stream_logs", { signal }); let response = await fetch(url, { signal });
if (!response.ok) { if (!response.ok) {
let text = await response.text(); let text = await response.text();
@ -128,10 +128,7 @@ const streamLogs = (
}; };
}; };
export const LogStream: React.FC<LogStreamProps> = ({ export const LogStream: React.FC<LogStreamProps> = ({ url, maxLines }) => {
httpApiBase,
maxLines,
}) => {
const [logLines, setLogLines] = useState<Line[]>([]); const [logLines, setLogLines] = useState<Line[]>([]);
const [error, setError] = useState<ErrorWithLabel | null>(null); const [error, setError] = useState<ErrorWithLabel | null>(null);
const [filter, setFilter] = useState<string>(""); const [filter, setFilter] = useState<string>("");
@ -189,12 +186,8 @@ export const LogStream: React.FC<LogStreamProps> = ({
useEffect(() => updateFilter.cancel, []); useEffect(() => updateFilter.cancel, []);
useEffect(() => { useEffect(() => {
return streamLogs( return streamLogs(url, (line) => addLineRef.current(line), setError);
httpApiBase, }, [url]);
(line) => addLineRef.current(line),
setError
);
}, [httpApiBase]);
return ( return (
<div> <div>

View file

@ -11,7 +11,7 @@ interface Props {
export const LogStreamModal: React.FC<Props> = ({ show, onClose }) => { export const LogStreamModal: React.FC<Props> = ({ show, onClose }) => {
const api = useContext(APIContext); const api = useContext(APIContext);
const apiBase = api.getHttpBaseUrl(); let logsUrl = api.getStreamLogsUrl();
return ( return (
<Modal size="xl" show={show} onHide={onClose}> <Modal size="xl" show={show} onHide={onClose}>
@ -19,8 +19,8 @@ export const LogStreamModal: React.FC<Props> = ({ show, onClose }) => {
<Modal.Title>rqbit server logs</Modal.Title> <Modal.Title>rqbit server logs</Modal.Title>
</Modal.Header> </Modal.Header>
<Modal.Body> <Modal.Body>
{apiBase ? ( {logsUrl ? (
<LogStream httpApiBase={apiBase} /> <LogStream url={logsUrl} />
) : ( ) : (
<ErrorComponent <ErrorComponent
error={{ text: "HTTP API not available to stream logs" }} error={{ text: "HTTP API not available to stream logs" }}

View file

@ -27,8 +27,8 @@ export const APIContext = createContext<RqbitAPI>({
delete: () => { delete: () => {
throw new Error("Function not implemented."); throw new Error("Function not implemented.");
}, },
getHttpBaseUrl: () => { getStreamLogsUrl: () => {
throw new Error("Function not implemented."); return null;
}, },
}); });
export const AppContext = createContext<ContextType>({ export const AppContext = createContext<ContextType>({

View file

@ -64,7 +64,7 @@ const makeRequest = async (
}; };
export const API: RqbitAPI & { getVersion: () => Promise<string> } = { export const API: RqbitAPI & { getVersion: () => Promise<string> } = {
getHttpBaseUrl: () => apiUrl, getStreamLogsUrl: () => apiUrl + "/stream_logs",
listTorrents: (): Promise<ListTorrentsResponse> => listTorrents: (): Promise<ListTorrentsResponse> =>
makeRequest("GET", "/torrents"), makeRequest("GET", "/torrents"),
getTorrentDetails: (index: number): Promise<TorrentDetails> => { getTorrentDetails: (index: number): Promise<TorrentDetails> => {

View file

@ -69,10 +69,15 @@ async function readFileAsBase64(file: File): Promise<string> {
export const makeAPI = (configuration: RqbitDesktopConfig): RqbitAPI => { export const makeAPI = (configuration: RqbitDesktopConfig): RqbitAPI => {
return { return {
getHttpBaseUrl: () => { getStreamLogsUrl: () => {
return configuration.http_api.listen_addr if (!configuration.http_api.listen_addr) {
? `http://${configuration.http_api.listen_addr}` return null;
: 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> { listTorrents: async function (): Promise<ListTorrentsResponse> {
return await invokeAPI<ListTorrentsResponse>("torrents_list"); return await invokeAPI<ListTorrentsResponse>("torrents_list");