afb-svc: make service existing during its initialisation
[src/app-framework-binder.git] / src / afb-api-so-v2.c
index 2cf181b..038f1ce 100644 (file)
@@ -103,6 +103,7 @@ static void call_sync_cb(void *closure, struct afb_xreq *xreq)
 
 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
 {
+       int rc;
        int (*start)();
        void (*onevent)(const char *event, struct json_object *object);
 
@@ -133,13 +134,24 @@ static int service_start_cb(void *closure, int share_session, int onneed, struct
        }
 
        /* get the event handler if any */
-       desc->service = afb_svc_create_v2(desc->binding->api, apiset, share_session, start, onevent, desc->data);
+       desc->service = afb_svc_create(desc->binding->api, apiset, share_session, onevent, &desc->data->service);
        if (desc->service == NULL) {
                /* starting error */
                ERROR("Starting service %s failed", desc->binding->api);
                return -1;
        }
 
+       /* Starts the service */
+       rc = afb_svc_start_v2(desc->service, start);
+       if (rc < 0) {
+               /* initialisation error */
+               ERROR("Initialisation of service %s failed (%d): %m", desc->binding->api, rc);
+               afb_svc_destroy(desc->service, &desc->data->service);
+               desc->service = NULL;
+               return rc;
+       }
+
+
        return 0;
 }
 
@@ -197,7 +209,7 @@ static struct json_object *addperm_key_valint(struct json_object *o, const char
        return addperm_key_val(o, key, json_object_new_int(val));
 }
 
-static struct json_object *make_description(struct api_so_v2 *desc)
+static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
 {
        char buffer[256];
        const struct afb_verb_v2 *verb;
@@ -210,6 +222,7 @@ static struct json_object *make_description(struct api_so_v2 *desc)
        json_object_object_add(r, "info", i);
        json_object_object_add(i, "title", json_object_new_string(desc->binding->api));
        json_object_object_add(i, "version", json_object_new_string("0.0.0"));
+       json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: desc->binding->api));
 
        p = json_object_new_object();
        json_object_object_add(r, "paths", p);
@@ -243,7 +256,7 @@ static struct json_object *make_description(struct api_so_v2 *desc)
                json_object_object_add(g, "responses", a);
                f = json_object_new_object();
                json_object_object_add(a, "200", f);
-               json_object_object_add(f, "description", json_object_new_string(verb->verb));
+               json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
                verb++;
        }
        return r;
@@ -254,7 +267,7 @@ static struct json_object *describe_cb(void *closure)
        struct api_so_v2 *desc = closure;
        struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
        if (!r)
-               r = make_description(desc);
+               r = make_description_openAPIv3(desc);
        return r;
 }