+}
+
+/*---------------------------------------------------------
+ | http server
+ | Handles the HTTP server
+ +--------------------------------------------------------- */
+static int init_http_server(struct afb_hsrv *hsrv, struct afb_config * config)
+{
+ int idx, dfd;
+
+ dfd = afb_common_rootdir_get_fd();
+
+ if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_websocket_switch, NULL, 20))
+ return 0;
+
+ if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_apis, NULL, 10))
+ return 0;
+
+ for (idx = 0; idx < config->aliascount; idx++)
+ if (!afb_hsrv_add_alias (hsrv, config->aliasdir[idx].url, dfd, config->aliasdir[idx].path, 0, 0))
+ return 0;
+
+ if (!afb_hsrv_add_alias(hsrv, "", dfd, config->roothttp, -10, 1))
+ return 0;
+
+ if (!afb_hsrv_add_handler(hsrv, config->rootbase, afb_hswitch_one_page_api_redirect, NULL, -20))
+ return 0;
+
+ return 1;
+}
+
+static struct afb_hsrv *start_http_server(struct afb_config * config)
+{
+ int rc;
+ struct afb_hsrv *hsrv;
+
+ if (afb_hreq_init_download_path("/tmp")) { /* TODO: sessiondir? */
+ ERROR("unable to set the tmp directory");
+ return NULL;
+ }
+
+ hsrv = afb_hsrv_create();
+ if (hsrv == NULL) {
+ ERROR("memory allocation failure");
+ return NULL;
+ }
+
+ if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout)
+ || !init_http_server(hsrv, config)) {
+ ERROR("initialisation of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ NOTICE("Waiting port=%d rootdir=%s", config->httpdPort, config->rootdir);
+ NOTICE("Browser URL= http:/*localhost:%d", config->httpdPort);
+
+ rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15);
+ if (!rc) {
+ ERROR("starting of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ return hsrv;
+}