rest-api: add config dir parametrable.
This commit is contained in:
@@ -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]);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
|
||||
@@ -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<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
|
||||
*/
|
||||
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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user