Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020, 2023, 2024 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of EXCHANGEABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file taler-auditor-httpd_spa.c
18 : * @brief logic to load single page app (/webui)
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <gnunet/gnunet_util_lib.h>
23 : #include "taler/taler_util.h"
24 : #include "taler/taler_mhd_lib.h"
25 : #include <gnunet/gnunet_mhd_compat.h>
26 : #include "taler-auditor-httpd_spa.h"
27 :
28 :
29 : /**
30 : * Resources of the auditor SPA.
31 : */
32 : static struct TALER_MHD_Spa *spa;
33 :
34 :
35 : enum MHD_Result
36 0 : TAH_spa_handler (
37 : /* const */ struct TAH_RequestHandler *rh,
38 : struct MHD_Connection *connection,
39 : void **connection_cls,
40 : const char *upload_data,
41 : size_t *upload_data_size,
42 : const char *const args[])
43 : {
44 0 : const char *path = args[1];
45 :
46 0 : GNUNET_assert (0 == strcmp (args[0],
47 : "webui"));
48 0 : if (NULL == path)
49 0 : path = "index.html";
50 0 : return TALER_MHD_spa_handler (spa,
51 : connection,
52 : path);
53 : }
54 :
55 :
56 : /**
57 : * Load the SPA data from an directory.
58 : *
59 : * If the directory is not found,
60 : * fall back to a directory owned by the taler-exchange package.
61 : *
62 : * @param spa_abs_dir absolute location of the SPA,
63 : * typically packaged separately from the exchange
64 : * @param spa_fallback_dir fallback directory, relative
65 : * to the project data.
66 : * @returns loaded SPA or NULL on error
67 : */
68 : static struct TALER_MHD_Spa *
69 5 : spa_load (const char *spa_abs_dir,
70 : const char *spa_fallback_dir)
71 : {
72 5 : bool have_spa = false;
73 5 : GNUNET_assert (NULL != spa_fallback_dir);
74 5 : if (NULL != spa_abs_dir)
75 : {
76 : struct stat sb;
77 5 : have_spa = stat (spa_abs_dir, &sb) == 0 && S_ISDIR (sb.st_mode);
78 5 : if (have_spa)
79 : {
80 0 : return TALER_MHD_spa_load_dir (spa_abs_dir);
81 : }
82 5 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
83 : "SPA not found at '%s', using fallback\n",
84 : spa_abs_dir);
85 : }
86 5 : return TALER_MHD_spa_load (TALER_AUDITOR_project_data (),
87 : spa_fallback_dir);
88 : }
89 :
90 :
91 : enum GNUNET_GenericReturnValue
92 5 : TAH_spa_init ()
93 : {
94 5 : spa = spa_load (TAH_spa_dir,
95 : "spa/");
96 5 : if (NULL == spa)
97 : {
98 0 : GNUNET_break (0);
99 0 : return GNUNET_SYSERR;
100 : }
101 5 : return GNUNET_OK;
102 : }
103 :
104 :
105 : /**
106 : * Nicely shut down.
107 : */
108 : void __attribute__ ((destructor))
109 : get_spa_fini (void);
110 :
111 : /* declaration to suppress compiler warning */
112 : void __attribute__ ((destructor))
113 5 : get_spa_fini ()
114 : {
115 5 : if (NULL != spa)
116 : {
117 5 : TALER_MHD_spa_free (spa);
118 5 : spa = NULL;
119 : }
120 5 : }
|