2 * Copyright (C) 2016, 2017, 2018 "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-api-ws.h"
34 #include "afb-session.h"
36 #include "afs-supervisor.h"
37 #include "afs-config.h"
41 #include "process-name.h"
44 struct afs_config *main_config;
47 struct afb_apiset *main_apiset;
49 /*************************************************************************************/
51 static int init_http_server(struct afb_hsrv *hsrv)
53 if (!afb_hsrv_add_handler
54 (hsrv, main_config->rootapi, afb_hswitch_websocket_switch, main_apiset, 20))
57 if (!afb_hsrv_add_handler
58 (hsrv, main_config->rootapi, afb_hswitch_apis, main_apiset, 10))
61 if (main_config->roothttp != NULL) {
62 if (!afb_hsrv_add_alias
63 (hsrv, "", afb_common_rootdir_get_fd(), main_config->roothttp,
68 if (!afb_hsrv_add_handler
69 (hsrv, main_config->rootbase, afb_hswitch_one_page_api_redirect, NULL,
76 static struct afb_hsrv *start_http_server()
79 struct afb_hsrv *hsrv;
81 if (afb_hreq_init_download_path(main_config->uploaddir)) {
82 ERROR("unable to set the upload directory %s", main_config->uploaddir);
86 hsrv = afb_hsrv_create();
88 ERROR("memory allocation failure");
92 if (!afb_hsrv_set_cache_timeout(hsrv, main_config->cacheTimeout)
93 || !init_http_server(hsrv)) {
94 ERROR("initialisation of httpd failed");
99 NOTICE("Waiting port=%d rootdir=%s", main_config->httpdPort, main_config->rootdir);
100 NOTICE("Browser URL= http://localhost:%d", main_config->httpdPort);
102 rc = afb_hsrv_start(hsrv, (uint16_t) main_config->httpdPort, 15);
104 ERROR("starting of httpd failed");
112 static void start(int signum, void *arg)
114 struct afb_hsrv *hsrv;
119 ERROR("start aborted: received signal %s", strsignal(signum));
123 /* set the directories */
124 mkdir(main_config->workdir, S_IRWXU | S_IRGRP | S_IXGRP);
125 if (chdir(main_config->workdir) < 0) {
126 ERROR("Can't enter working dir %s", main_config->workdir);
129 if (afb_common_rootdir_set(main_config->rootdir) < 0) {
130 ERROR("failed to set common root directory");
134 /* configure the daemon */
135 if (afb_session_init(main_config->nbSessionMax, main_config->cntxTimeout, main_config->token)) {
136 ERROR("initialisation of session manager failed");
140 main_apiset = afb_apiset_create("main", main_config->apiTimeout);
142 ERROR("can't create main apiset");
146 /* init the main apiset */
147 rc = afs_supervisor_add(main_apiset, main_apiset);
149 ERROR("Can't create supervision's apiset: %m");
153 /* export the service if required */
154 if (main_config->ws_server) {
155 rc = afb_api_ws_add_server(main_config->ws_server, main_apiset, main_apiset);
157 ERROR("Can't export (ws-server) api %s: %m", main_config->ws_server);
162 /* start the services */
163 if (afb_apiset_start_all_services(main_apiset) < 0)
166 /* start the HTTP server */
167 if (main_config->httpdPort <= 0) {
168 ERROR("no port is defined");
172 if (!afb_hreq_init_cookie(main_config->httpdPort, main_config->rootapi, main_config->cntxTimeout)) {
173 ERROR("initialisation of HTTP cookies failed");
177 hsrv = start_http_server();
182 sd_notify(1, "READY=1");
183 afs_supervisor_discover();
190 * initialize the supervision
192 int main(int ac, char **av)
195 main_config = afs_config_parse_arguments(ac, av);
196 if (main_config->name) {
197 verbose_set_name(main_config->name, 0);
198 process_name_set_name(main_config->name);
199 process_name_replace_cmdline(av, main_config->name);
201 /* enter job processing */
202 jobs_start(3, 0, 10, start, av[1]);
203 WARNING("hoops returned from jobs_enter! [report bug]");