mirror of
https://github.com/webui-dev/webui.git
synced 2024-09-11 22:17:13 +03:00
Adding webui_set_port
- New API `webui_set_port(win, port)`
This commit is contained in:
parent
8363cccc9a
commit
eb50747970
@ -500,6 +500,20 @@ WEBUI_EXPORT size_t webui_get_parent_process_id(size_t window);
|
||||
*/
|
||||
WEBUI_EXPORT size_t webui_get_child_process_id(size_t window);
|
||||
|
||||
/**
|
||||
* @brief Set a custom web-server network port to be used by WebUI.
|
||||
* This can be useful to determine the HTTP link of `webui.js` in case
|
||||
* you are trying to use WebUI with an external web-server like NGNIX
|
||||
*
|
||||
* @param window The window number
|
||||
* @param port The web-server network port WebUI should use
|
||||
*
|
||||
* @return Returns True if the port is free and usable by WebUI
|
||||
*
|
||||
* @example bool ret = webui_set_port(myWindow, 8080);
|
||||
*/
|
||||
WEBUI_EXPORT bool webui_set_port(size_t window, size_t port);
|
||||
|
||||
// -- SSL/TLS -------------------------
|
||||
|
||||
/**
|
||||
|
41
src/webui.c
41
src/webui.c
@ -134,6 +134,7 @@ typedef struct _webui_window_t {
|
||||
bool bridge_handled;
|
||||
bool server_handled;
|
||||
bool is_embedded_html;
|
||||
size_t custom_server_port;
|
||||
size_t server_port;
|
||||
size_t ws_port;
|
||||
char* url;
|
||||
@ -331,6 +332,7 @@ static uint32_t _webui_generate_random_uint32();
|
||||
static const char* _webui_url_encode(const char* str);
|
||||
static bool _webui_regular_open_url(const char* url);
|
||||
static bool _webui_is_valid_url(const char* url);
|
||||
static bool _webui_port_is_used(size_t port_num);
|
||||
#ifdef WEBUI_TLS
|
||||
static int _webui_tls_initialization(void* ssl_ctx, void* ptr);
|
||||
static bool _webui_tls_generate_self_signed_cert(char* root_cert, char* root_key, char* ssl_cert, char* ssl_key);
|
||||
@ -1599,6 +1601,27 @@ bool webui_set_tls_certificate(const char* certificate_pem, const char* private_
|
||||
return false;
|
||||
}
|
||||
|
||||
bool webui_set_port(size_t window, size_t port) {
|
||||
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[User] webui_set_port([%zu], [%zu])...\n", window, port);
|
||||
#endif
|
||||
|
||||
// Initialization
|
||||
_webui_init();
|
||||
|
||||
// Dereference
|
||||
if (_webui_core.exit_now || _webui_core.wins[window] == NULL)
|
||||
return false;
|
||||
_webui_window_t* win = _webui_core.wins[window];
|
||||
|
||||
if (_webui_port_is_used(port))
|
||||
return false;
|
||||
|
||||
win->custom_server_port = port;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t webui_get_child_process_id(size_t window) {
|
||||
|
||||
#ifdef WEBUI_LOG
|
||||
@ -5686,8 +5709,22 @@ static bool _webui_show_window(_webui_window_t* win, const char* content, int ty
|
||||
_webui_free_mem((void*)win->url);
|
||||
|
||||
// Get network ports
|
||||
win->server_port = (win->server_port == 0 ? _webui_get_free_port() : win->server_port);
|
||||
win->ws_port = (win->ws_port == 0 ? _webui_get_free_port() : win->ws_port);
|
||||
win->server_port = (win->custom_server_port > 0 ? win->custom_server_port : _webui_get_free_port());
|
||||
win->ws_port = (win->ws_port > 0 ? win->ws_port : _webui_get_free_port());
|
||||
if (_webui_port_is_used(win->server_port)) {
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[Core]\t\t_webui_show_window() -> Web server port %zu is busy.\n",
|
||||
win->server_port);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
else if (_webui_port_is_used(win->ws_port)) {
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[Core]\t\t_webui_show_window() -> WebSocket server port %zu is busy.\n",
|
||||
win->ws_port);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Generate the server URL
|
||||
win->url = (char*)_webui_malloc(32); // [http][domain][port]
|
||||
|
Loading…
Reference in New Issue
Block a user