aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-04-17 18:14:23 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-04-17 18:14:23 +0200
commitbd375330fe7e3d79495762bcc7c86d907a453aae (patch)
tree172e6b0e25905065f9fee2e48e95d821ff93e541 /src
parentf5314448943216b558a3efd53f35ac9f14926b32 (diff)
cleaner http handling in main
Change-Id: I3e13b73910217082f32c5775adefb643b3a91baf Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/main.c122
1 files changed, 62 insertions, 60 deletions
diff --git a/src/main.c b/src/main.c
index 9977c7d4..d8065e42 100644
--- a/src/main.c
+++ b/src/main.c
@@ -74,8 +74,6 @@
#define SET_MODE 18
#define SET_READYFD 19
-static struct afb_hsrv *start(struct afb_config * config);
-
// Command line structure hold cli --command + help text
typedef struct {
int val; // command number within application
@@ -487,6 +485,67 @@ static void daemonize(struct afb_config *config)
}
/*---------------------------------------------------------
+ | http server
+ | Handles the HTTP server
+ +--------------------------------------------------------- */
+static int init_http_server(struct afb_hsrv *hsrv, struct afb_config * config)
+{
+ int idx;
+
+ 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, config->aliasdir[idx].path, 0))
+ return 0;
+
+ if (!afb_hsrv_add_alias(hsrv, "", config->rootdir, -10))
+ 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;
+
+ hsrv = afb_hsrv_create();
+ if (hsrv == NULL) {
+ fprintf(stderr, "memory allocation failure\n");
+ return NULL;
+ }
+
+ if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout)
+ || !init_http_server(hsrv, config)) {
+ fprintf (stderr, "Error: initialisation of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ if (verbosity) {
+ fprintf (stderr, "AFB:notice Waiting port=%d rootdir=%s\n", config->httpdPort, config->rootdir);
+ fprintf (stderr, "AFB:notice Browser URL= http:/*localhost:%d\n", config->httpdPort);
+ }
+
+ rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15);
+ if (!rc) {
+ fprintf (stderr, "Error: starting of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ return hsrv;
+}
+
+
+/*---------------------------------------------------------
| main
| Parse option and launch action
+--------------------------------------------------------- */
@@ -547,7 +606,7 @@ int main(int argc, char *argv[]) {
}
- hsrv = start (config);
+ hsrv = start_http_server(config);
if (hsrv == NULL)
exit(1);
@@ -567,60 +626,3 @@ int main(int argc, char *argv[]) {
return 0;
}
-static int init(struct afb_hsrv *hsrv, struct afb_config * config)
-{
- int idx;
-
- 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, config->aliasdir[idx].path, 0))
- return 0;
-
- if (!afb_hsrv_add_alias(hsrv, "", config->rootdir, -10))
- 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(struct afb_config * config)
-{
- int rc;
- struct afb_hsrv *hsrv;
-
- hsrv = afb_hsrv_create();
- if (hsrv == NULL) {
- fprintf(stderr, "memory allocation failure\n");
- return NULL;
- }
-
- if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout)
- || !init(hsrv, config)) {
- fprintf (stderr, "Error: initialisation of httpd failed");
- afb_hsrv_put(hsrv);
- return NULL;
- }
-
- if (verbosity) {
- fprintf (stderr, "AFB:notice Waiting port=%d rootdir=%s\n", config->httpdPort, config->rootdir);
- fprintf (stderr, "AFB:notice Browser URL= http:/*localhost:%d\n", config->httpdPort);
- }
-
- rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15);
- if (!rc) {
- fprintf (stderr, "Error: starting of httpd failed");
- afb_hsrv_put(hsrv);
- return NULL;
- }
-
- return hsrv;
-}
-
-