mirror of
https://github.com/webui-dev/webui.git
synced 2024-09-11 22:17:13 +03:00
New API webui_start_server
This commit is contained in:
parent
cc103aa37f
commit
bb887d76f6
@ -284,6 +284,18 @@ WEBUI_EXPORT bool webui_show_client(webui_event_t* e, const char* content);
|
||||
*/
|
||||
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
|
||||
|
||||
/**
|
||||
* @brief Start only the web server and return the URL. This is useful for web app.
|
||||
*
|
||||
* @param window The window number
|
||||
* @param path The local root folder full path
|
||||
*
|
||||
* @return Returns the url of this window server.
|
||||
*
|
||||
* @example const char* url = webui_start_server(myWindow, "/full/root/path");
|
||||
*/
|
||||
WEBUI_EXPORT const char* webui_start_server(size_t window, const char* path);
|
||||
|
||||
/**
|
||||
* @brief Show a WebView window using embedded HTML, or a file. If the window is already
|
||||
* open, it will be refreshed. Note: Win32 need `WebView2Loader.dll`.
|
||||
@ -420,7 +432,8 @@ WEBUI_EXPORT void webui_set_file_handler(size_t window, const void* (*handler)(c
|
||||
WEBUI_EXPORT bool webui_is_shown(size_t window);
|
||||
|
||||
/**
|
||||
* @brief Set the maximum time in seconds to wait for the window to connect. This effect `show()` and `wait()`.
|
||||
* @brief Set the maximum time in seconds to wait for the window to connect.
|
||||
* This effect `show()` and `wait()`. Value of `0` means wait forever.
|
||||
*
|
||||
* @param second The timeout in seconds
|
||||
*
|
||||
|
89
src/webui.c
89
src/webui.c
@ -83,6 +83,7 @@
|
||||
#define WEBUI_SHOW_HTML (1) // Show window using HTML
|
||||
#define WEBUI_SHOW_FILE (2) // Show window using a local file
|
||||
#define WEBUI_SHOW_URL (3) // Show window using a URL
|
||||
#define WEBUI_SHOW_FOLDER (4) // Show window using a Folder
|
||||
#define WEBUI_MIN_PORT (10000) // Minimum socket port
|
||||
#define WEBUI_MAX_PORT (65500) // Should be less than 65535
|
||||
#define WEBUI_STDOUT_BUF (10240) // Command STDOUT output buffer size
|
||||
@ -1551,6 +1552,38 @@ void webui_delete_profile(size_t window) {
|
||||
}
|
||||
}
|
||||
|
||||
const char* webui_start_server(size_t window, const char* path) {
|
||||
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[User] webui_start_server([%zu])\n", window);
|
||||
#endif
|
||||
|
||||
// Initialization
|
||||
_webui_init();
|
||||
|
||||
// Dereference
|
||||
if (_webui_mutex_is_exit_now(WEBUI_MUTEX_NONE) || _webui.wins[window] == NULL)
|
||||
return "";
|
||||
_webui_window_t* win = _webui.wins[window];
|
||||
|
||||
// Check
|
||||
if (win->server_running || _webui_is_empty(path) ||
|
||||
(_webui_strlen(path) > WEBUI_MAX_PATH) ||
|
||||
!_webui_folder_exist((char*)path)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Make `wait()` waits forever
|
||||
webui_set_timeout(0);
|
||||
|
||||
// Start the window without any GUI
|
||||
if (webui_show_browser(window, path, NoBrowser)) {
|
||||
return webui_get_url(window);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
bool webui_show_client(webui_event_t* e, const char* content) {
|
||||
|
||||
#ifdef WEBUI_LOG
|
||||
@ -3803,7 +3836,7 @@ static bool _webui_is_valid_url(const char* url) {
|
||||
printf("[Core]\t\t_webui_is_valid_url([%.8s...])\n", url);
|
||||
#endif
|
||||
|
||||
if (_webui_is_empty(url))
|
||||
if ((_webui_is_empty(url)) || (url[0] != 'h'))
|
||||
return false;
|
||||
if (strncmp(url, "http://", 7) == 0 || strncmp(url, "https://", 8) == 0)
|
||||
return true;
|
||||
@ -6424,13 +6457,31 @@ static bool _webui_show(_webui_window_t* win, struct mg_connection* client, cons
|
||||
return _webui_show_window(win, client, content_cpy, WEBUI_SHOW_URL, browser);
|
||||
}
|
||||
// Embedded HTML
|
||||
else if (strstr(content_cpy, "<html") || strstr(content_cpy, "<!DOCTYPE") || strstr(content_cpy, "<!doctype") || strstr(content_cpy, "<!Doctype")) {
|
||||
else if (strstr(content_cpy, "<html") ||
|
||||
strstr(content_cpy, "<!DOCTYPE") ||
|
||||
strstr(content_cpy, "<!doctype") ||
|
||||
strstr(content_cpy, "<!Doctype")) {
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[Core]\t\t_webui_show() -> Embedded HTML:\n");
|
||||
printf("- - -[HTML]- - - - - - - - - -\n%s\n- - - - - - - - - - - - - - - -\n", content_cpy);
|
||||
#endif
|
||||
return _webui_show_window(win, client, content_cpy, WEBUI_SHOW_HTML, browser);
|
||||
}
|
||||
// Folder
|
||||
else if (_webui_folder_exist(content_cpy)) {
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[Core]\t\t_webui_show() -> Folder: [%s]\n", content_cpy);
|
||||
#endif
|
||||
// Set root folder
|
||||
if (!webui_set_root_folder(win->window_number, content_cpy)) {
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[Core]\t\t_webui_show() -> Failed to set folder root path\n");
|
||||
#endif
|
||||
_webui_free_mem((void*)content_cpy);
|
||||
return false;
|
||||
}
|
||||
return _webui_show_window(win, client, content_cpy, WEBUI_SHOW_FOLDER, browser);
|
||||
}
|
||||
// File
|
||||
else {
|
||||
#ifdef WEBUI_LOG
|
||||
@ -6652,6 +6703,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
|
||||
printf("[Core]\t\t_webui_show_window(HTML, [%zu])\n", browser);
|
||||
else if (type == WEBUI_SHOW_URL)
|
||||
printf("[Core]\t\t_webui_show_window(URL, [%zu])\n", browser);
|
||||
else if (type == WEBUI_SHOW_FOLDER)
|
||||
printf("[Core]\t\t_webui_show_window(FOLDER, [%zu])\n", browser);
|
||||
else
|
||||
printf("[Core]\t\t_webui_show_window(FILE, [%zu])\n", browser);
|
||||
#endif
|
||||
@ -6733,7 +6786,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
|
||||
size_t len = _webui_strlen(win->url);
|
||||
window_url = (char*)_webui_malloc(len);
|
||||
WEBUI_STR_COPY_DYN(window_url, len, win->url);
|
||||
} else if (type == WEBUI_SHOW_URL) {
|
||||
}
|
||||
else if (type == WEBUI_SHOW_URL) {
|
||||
|
||||
const char* user_url = content;
|
||||
|
||||
@ -6746,8 +6800,19 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
|
||||
|
||||
// Set window URL
|
||||
window_url = (char*)user_url;
|
||||
} else {
|
||||
}
|
||||
else if (type == WEBUI_SHOW_FOLDER) {
|
||||
|
||||
const char* folder_path = content;
|
||||
|
||||
// Show a window using a local folder
|
||||
win->is_embedded_html = false;
|
||||
win->html = NULL;
|
||||
|
||||
// Set window URL
|
||||
window_url = win->url;
|
||||
}
|
||||
else {
|
||||
const char* user_file = content;
|
||||
|
||||
// Show a window using a local file
|
||||
@ -6845,13 +6910,15 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
|
||||
}
|
||||
|
||||
_webui_free_mem((void*)window_url);
|
||||
if (!runWebView && !runBrowser) {
|
||||
// Browser and WebView are not available
|
||||
_webui_free_mem((void*)win->html);
|
||||
_webui_free_mem((void*)win->url);
|
||||
_webui_free_port(win->server_port);
|
||||
win->server_port = 0;
|
||||
return false;
|
||||
if (browser != NoBrowser) {
|
||||
if (!runWebView && !runBrowser) {
|
||||
// Browser and WebView are not available
|
||||
_webui_free_mem((void*)win->html);
|
||||
_webui_free_mem((void*)win->url);
|
||||
_webui_free_port(win->server_port);
|
||||
win->server_port = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_webui.ui) {
|
||||
|
Loading…
Reference in New Issue
Block a user