rest-api: add config dir parametrable.

This commit is contained in:
2018-04-24 22:37:53 +02:00
parent dac1706386
commit a539a82792
4 changed files with 24 additions and 17 deletions

View File

@@ -148,6 +148,9 @@ static int usage(const char *name)
" -l string URL prefix for Lua handler, default is '/lua'\n" " -l string URL prefix for Lua handler, default is '/lua'\n"
" -L file Lua handler script, omit to disable Lua\n" " -L file Lua handler script, omit to disable Lua\n"
#endif #endif
#ifdef HAVE_REST_API_PLUGING
" -U directory Specify the config root path\n"
#endif
#ifdef HAVE_UBUS #ifdef HAVE_UBUS
" -u string URL prefix for UBUS via JSON-RPC handler\n" " -u string URL prefix for UBUS via JSON-RPC handler\n"
" -U file Override ubus socket path\n" " -U file Override ubus socket path\n"
@@ -441,11 +444,16 @@ int main(int argc, char **argv)
#else #else
case 'a': case 'a':
case 'u': case 'u':
case 'U': //case 'U':
case 'X': case 'X':
fprintf(stderr, "uhttpd: UBUS support not compiled, " fprintf(stderr, "uhttpd: UBUS support not compiled, "
"ignoring -%c\n", ch); "ignoring -%c\n", ch);
break; break;
#endif
#ifdef HAVE_REST_API_PLUGING
case 'U':
conf.ubus_prefix = optarg;
break;
#endif #endif
default: default:
return usage(argv[0]); return usage(argv[0]);

View File

@@ -30,7 +30,7 @@
/*--------------------------------- PLUGINS ----------------------------------*/ /*--------------------------------- PLUGINS ----------------------------------*/
extern struct ubus_context *get_uhttp_server_ctx(void); 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 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); 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(); uloop_done();
uhttp_server_setup(the_ctx, an_ops); return uhttp_server_setup(the_ctx, an_ops, a_conf->ubus_prefix);
return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------

View File

@@ -59,8 +59,6 @@ extern "C" {
/*------------------------------- GLOBALS ----------------------------------*/ /*------------------------------- GLOBALS ----------------------------------*/
#define kConfigDirectory "/usr/local/configs/restd/"
#define kControllerKey "controller" #define kControllerKey "controller"
#define kEtagControllerKey "etag_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. * @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. * @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_ctx = a_ctx;
m_ops = const_cast<struct uhttpd_ops *>(an_ops); m_ops = const_cast<struct uhttpd_ops *>(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 * @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; DIR *the_rep = NULL;
struct dirent *the_dir_ent = NULL; struct dirent *the_dir_ent = NULL;
std::string the_path; std::string the_path;
struct json_object *the_root_node; 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) while ((the_dir_ent = readdir(the_rep)) != NULL)
{ {
if (strcmp(the_dir_ent->d_name, ".") != 0 && strcmp(the_dir_ent->d_name, "..") != 0) if (strcmp(the_dir_ent->d_name, ".") != 0 && strcmp(the_dir_ent->d_name, "..") != 0)
{ {
// printf ("load: %s\n", the_dir_ent->d_name); // 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_path += the_dir_ent->d_name;
the_root_node = load(the_path); the_root_node = load(the_path);

View File

@@ -56,7 +56,7 @@ class UhttpServer
static UhttpServer &get_instance(void); 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); 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); int data_send(struct client *a_client, const char *a_data, int a_len);
private: 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); 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, 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, std::string &an_event_name, std::string &an_etag_key,