2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 #include <systemd/sd-daemon.h>
28 #include "afb-common.h"
30 #include "afb-hswitch.h"
32 #include "afb-apiset.h"
33 #include "afb-session.h"
35 #include "afs-supervisor.h"
36 #include "afs-config.h"
40 #include "process-name.h"
43 struct afs_config *main_config;
46 struct afb_apiset *main_apiset;
48 /*************************************************************************************/
50 static int init_http_server(struct afb_hsrv *hsrv)
52 if (!afb_hsrv_add_handler
53 (hsrv, main_config->rootapi, afb_hswitch_websocket_switch, main_apiset, 20))
56 if (!afb_hsrv_add_handler
57 (hsrv, main_config->rootapi, afb_hswitch_apis, main_apiset, 10))
60 if (main_config->roothttp != NULL) {
61 if (!afb_hsrv_add_alias
62 (hsrv, "", afb_common_rootdir_get_fd(), main_config->roothttp,
67 if (!afb_hsrv_add_handler
68 (hsrv, main_config->rootbase, afb_hswitch_one_page_api_redirect, NULL,
75 static struct afb_hsrv *start_http_server()
78 struct afb_hsrv *hsrv;
80 if (afb_hreq_init_download_path(main_config->uploaddir)) {
81 ERROR("unable to set the upload directory %s", main_config->uploaddir);
85 hsrv = afb_hsrv_create();
87 ERROR("memory allocation failure");
91 if (!afb_hsrv_set_cache_timeout(hsrv, main_config->cacheTimeout)
92 || !init_http_server(hsrv)) {
93 ERROR("initialisation of httpd failed");
98 NOTICE("Waiting port=%d rootdir=%s", main_config->httpdPort, main_config->rootdir);
99 NOTICE("Browser URL= http://localhost:%d", main_config->httpdPort);
101 rc = afb_hsrv_start(hsrv, (uint16_t) main_config->httpdPort, 15);
103 ERROR("starting of httpd failed");
111 static void start(int signum, void *arg)
113 struct afb_hsrv *hsrv;
118 ERROR("start aborted: received signal %s", strsignal(signum));
122 /* set the directories */
123 mkdir(main_config->workdir, S_IRWXU | S_IRGRP | S_IXGRP);
124 if (chdir(main_config->workdir) < 0) {
125 ERROR("Can't enter working dir %s", main_config->workdir);
128 if (afb_common_rootdir_set(main_config->rootdir) < 0) {
129 ERROR("failed to set common root directory");
133 /* configure the daemon */
134 if (afb_session_init(main_config->nbSessionMax, main_config->cntxTimeout, main_config->token)) {
135 ERROR("initialisation of session manager failed");
139 main_apiset = afb_apiset_create("main", main_config->apiTimeout);
141 ERROR("can't create main apiset");
145 /* init the main apiset */
146 rc = afs_supervisor_add(main_apiset);
148 ERROR("Can't create supervision's apiset: %m");
152 /* start the services */
153 if (afb_apiset_start_all_services(main_apiset, 1) < 0)
156 /* start the HTTP server */
157 if (main_config->httpdPort <= 0) {
158 ERROR("no port is defined");
162 if (!afb_hreq_init_cookie(main_config->httpdPort, main_config->rootapi, main_config->cntxTimeout)) {
163 ERROR("initialisation of HTTP cookies failed");
167 hsrv = start_http_server();
172 sd_notify(1, "READY=1");
173 afs_supervisor_discover();
180 * initalize the supervision
182 int main(int ac, char **av)
185 main_config = afs_config_parse_arguments(ac, av);
186 if (main_config->name) {
187 verbose_set_name(main_config->name, 0);
188 process_name_set_name(main_config->name);
189 process_name_replace_cmdline(av, main_config->name);
191 /* enter job processing */
192 jobs_start(3, 0, 10, start, av[1]);
193 WARNING("hoops returned from jobs_enter! [report bug]");