From a539a82792b7e34275399476ca627bac54d92e57 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 24 Apr 2018 22:37:53 +0200 Subject: [PATCH] rest-api: add config dir parametrable. --- src/3P/uhttpd/main.c | 10 ++++++++- src/plugins/uhttpd/uhttpd-rest-api/plugin.c | 6 ++---- .../uhttpd/uhttpd-rest-api/uhttp-server.cpp | 21 ++++++++++--------- .../uhttpd/uhttpd-rest-api/uhttp-server.h | 4 ++-- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/3P/uhttpd/main.c b/src/3P/uhttpd/main.c index cf0b19e3..9a4a0b01 100644 --- a/src/3P/uhttpd/main.c +++ b/src/3P/uhttpd/main.c @@ -148,6 +148,9 @@ static int usage(const char *name) " -l string URL prefix for Lua handler, default is '/lua'\n" " -L file Lua handler script, omit to disable Lua\n" #endif +#ifdef HAVE_REST_API_PLUGING + " -U directory Specify the config root path\n" +#endif #ifdef HAVE_UBUS " -u string URL prefix for UBUS via JSON-RPC handler\n" " -U file Override ubus socket path\n" @@ -441,11 +444,16 @@ int main(int argc, char **argv) #else case 'a': case 'u': - case 'U': + //case 'U': case 'X': fprintf(stderr, "uhttpd: UBUS support not compiled, " "ignoring -%c\n", ch); break; +#endif +#ifdef HAVE_REST_API_PLUGING + case 'U': + conf.ubus_prefix = optarg; + break; #endif default: return usage(argv[0]); diff --git a/src/plugins/uhttpd/uhttpd-rest-api/plugin.c b/src/plugins/uhttpd/uhttpd-rest-api/plugin.c index 44955e64..29ae4ca0 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/plugin.c +++ b/src/plugins/uhttpd/uhttpd-rest-api/plugin.c @@ -30,7 +30,7 @@ /*--------------------------------- PLUGINS ----------------------------------*/ extern struct ubus_context *get_uhttp_server_ctx(void); -extern int uhttp_server_setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops); +extern int uhttp_server_setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops, const char *a_config_dir_path); extern bool uhttp_server_check_url(const char *an_url); extern void uhttp_server_handle_request(struct client *a_cl, char *an_url, struct path_info *a_pi); @@ -68,9 +68,7 @@ static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct confi uloop_done(); - uhttp_server_setup(the_ctx, an_ops); - - return 0; + return uhttp_server_setup(the_ctx, an_ops, a_conf->ubus_prefix); } /*! ---------------------------------------------------------------------------- diff --git a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp index 69725b9a..e38be2fb 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp @@ -59,8 +59,6 @@ extern "C" { /*------------------------------- GLOBALS ----------------------------------*/ -#define kConfigDirectory "/usr/local/configs/restd/" - #define kControllerKey "controller" #define kEtagControllerKey "etag_controller" @@ -133,9 +131,9 @@ extern "C" struct ubus_context *get_uhttp_server_ctx(void) * * @brief init method from c plugin to uhttp server. */ -extern "C" int uhttp_server_setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops) +extern "C" int uhttp_server_setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops, const char *a_config_dir_path) { - return UhttpServer::get_instance().setup(a_ctx, an_ops); + return UhttpServer::get_instance().setup(a_ctx, an_ops, a_config_dir_path); } /*! ---------------------------------------------------------------------------- @@ -262,12 +260,12 @@ UhttpServer::~UhttpServer(void) * * @brief setup the http server. */ -int UhttpServer::setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops) +int UhttpServer::setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops, const char *a_config_dir_path) { m_ctx = a_ctx; m_ops = const_cast(an_ops); - return load_config_dir(); + return load_config_dir(a_config_dir_path); } /*! ---------------------------------------------------------------------------- @@ -419,20 +417,23 @@ int UhttpServer::data_send(struct client *a_client, const char *a_data, int a_le * * @brief Load config from the configuration directory */ -int UhttpServer::load_config_dir(void) +int UhttpServer::load_config_dir(const char *a_config_dir_path) { DIR *the_rep = NULL; struct dirent *the_dir_ent = NULL; std::string the_path; struct json_object *the_root_node; - the_rep = opendir(kConfigDirectory); - + the_rep = opendir(a_config_dir_path); + if (the_rep == NULL) { + fprintf (stderr, "Impossible to open the config directory. check your parameters.\n"); + return -1; + } while ((the_dir_ent = readdir(the_rep)) != NULL) { if (strcmp(the_dir_ent->d_name, ".") != 0 && strcmp(the_dir_ent->d_name, "..") != 0) { // printf ("load: %s\n", the_dir_ent->d_name); - the_path = kConfigDirectory; + the_path = a_config_dir_path; the_path += the_dir_ent->d_name; the_root_node = load(the_path); diff --git a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h index e8934b6d..de378307 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h @@ -56,7 +56,7 @@ class UhttpServer static UhttpServer &get_instance(void); - int setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops); + int setup(struct ubus_context *a_ctx, const struct uhttpd_ops *an_ops, const char *a_config_dir_path); struct ubus_context *get_context(void); @@ -68,7 +68,7 @@ class UhttpServer int data_send(struct client *a_client, const char *a_data, int a_len); private: - int load_config_dir(void); + int load_config_dir(const char *a_config_dir_path); int add_controller_from_json(struct json_object *a_root_node); bool get_controller_fields(struct json_object *a_node, std::string &a_path, std::string &a_model_name, std::string &an_event_name, std::string &an_etag_key,