1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 | -- Copyright 2019-2021 Bartek thindil Jasicki
--
-- This file is part of YASS.
--
-- YASS is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- YASS is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with YASS. If not, see <http://www.gnu.org/licenses/>.
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones;
with Ada.Directories; use Ada.Directories;
with Ada.Environment_Variables;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.OS_Lib;
with AWS.Response;
with AWS.Services.Page_Server;
with AWS.Services.Directory;
with AWS.Server;
with AWS.Status;
with AtomFeed;
with Config; use Config;
with Messages; use Messages;
with Modules;
with Pages;
with Sitemaps;
package body Server is
--## rule off GLOBAL_REFERENCES
-- ****iv* Server/Server.Http_Server
-- FUNCTION
-- Instance of Http server which will be serving the project's files
-- SOURCE
Http_Server: AWS.Server.HTTP;
-- ****
--## rule on GLOBAL_REFERENCES
task body Monitor_Site is
use Ada.Calendar.Time_Zones;
use AtomFeed;
use Modules;
use Sitemaps;
Site_Rebuild: Boolean := False; --## rule line off GLOBAL_REFERENCES
Page_Tags: Tags_Container.Map := Tags_Container.Empty_Map;
Page_Table_Tags: TableTags_Container.Map :=
TableTags_Container.Empty_Map;
-- Monitor directory with full path Name for changes and update the site if needed
procedure Monitor_Directory(Name: String) is
use GNAT.OS_Lib;
use Pages;
-- Process file with full path Item: create html pages from markdown
-- files or copy any other file if they was updated since last check.
procedure Process_Files(Item: Directory_Entry_Type) is
use Ada.Environment_Variables;
Site_File_Name: Unbounded_String :=
Yass_Config.Output_Directory & Dir_Separator &
To_Unbounded_String
(Source => Simple_Name(Directory_Entry => Item));
begin
if Yass_Config.Excluded_Files.Find_Index
(Item => Simple_Name(Directory_Entry => Item)) /=
Excluded_Container.No_Index or
not Ada.Directories.Exists
(Name => Full_Name(Directory_Entry => Item)) then
return;
end if;
if Containing_Directory
(Name => Full_Name(Directory_Entry => Item)) /=
To_String(Source => Site_Directory) then
Site_File_Name :=
Yass_Config.Output_Directory &
Slice
(Source =>
To_Unbounded_String
(Source => Full_Name(Directory_Entry => Item)),
Low => Length(Source => Site_Directory) + 1,
High => Full_Name(Directory_Entry => Item)'Length);
end if;
if Extension(Name => Simple_Name(Directory_Entry => Item)) =
"md" then
Site_File_Name :=
To_Unbounded_String
(Source =>
Compose
(Containing_Directory =>
Containing_Directory
(Name => To_String(Source => Site_File_Name)),
Name =>
Ada.Directories.Base_Name
(Name => To_String(Source => Site_File_Name)),
Extension => "html"));
end if;
if not Ada.Directories.Exists
(Name => To_String(Source => Site_File_Name)) then
Set
(Name => "YASSFILE",
Value => Full_Name(Directory_Entry => Item));
if Extension(Name => Simple_Name(Directory_Entry => Item)) =
"md" then
Create_Page
(File_Name => Full_Name(Directory_Entry => Item),
Directory => Name);
else
Pages.Copy_File
(File_Name => Full_Name(Directory_Entry => Item),
Directory => Name);
end if;
Put_Line
(Item =>
"[" &
Ada.Calendar.Formatting.Image
(Date => Clock, Time_Zone => UTC_Time_Offset) &
"] " & "File: " & To_String(Source => Site_File_Name) &
" was added.");
Site_Rebuild := True;
elsif Extension(Name => Simple_Name(Directory_Entry => Item)) =
"md" then
if Modification_Time
(Name => Full_Name(Directory_Entry => Item)) >
Modification_Time
(Name => To_String(Source => Site_File_Name)) or
Modification_Time
(Name =>
Get_Layout_Name
(File_Name => Full_Name(Directory_Entry => Item))) >
Modification_Time
(Name => To_String(Source => Site_File_Name)) then
Set
(Name => "YASSFILE",
Value => Full_Name(Directory_Entry => Item));
Create_Page
(File_Name => Full_Name(Directory_Entry => Item),
Directory => Name);
Put_Line
(Item =>
"[" &
Ada.Calendar.Formatting.Image
(Date => Clock, Time_Zone => UTC_Time_Offset) &
"] " & "File: " & To_String(Source => Site_File_Name) &
" was updated.");
Site_Rebuild := True;
end if;
elsif Modification_Time
(Name => Full_Name(Directory_Entry => Item)) >
Modification_Time
(Name => To_String(Source => Site_File_Name)) then
Set
(Name => "YASSFILE",
Value => Full_Name(Directory_Entry => Item));
Pages.Copy_File
(File_Name => Full_Name(Directory_Entry => Item),
Directory => Name);
Put_Line
(Item =>
"[" &
Ada.Calendar.Formatting.Image
(Date => Clock, Time_Zone => UTC_Time_Offset) &
"] " & "File: " & To_String(Source => Site_File_Name) &
" was updated.");
Site_Rebuild := True;
end if;
end Process_Files;
-- Go recursive with directory with full path Item.
procedure Process_Directories(Item: Directory_Entry_Type) is
begin
if Yass_Config.Excluded_Files.Find_Index
(Item => Simple_Name(Directory_Entry => Item)) =
Excluded_Container.No_Index and
Ada.Directories.Exists
(Name => Full_Name(Directory_Entry => Item)) then
Monitor_Directory(Name => Full_Name(Directory_Entry => Item));
end if;
exception
when Ada.Directories.Name_Error =>
null;
end Process_Directories;
begin
Search
(Directory => Name, Pattern => "",
Filter => (Directory => False, others => True),
Process => Process_Files'Access);
Search
(Directory => Name, Pattern => "",
Filter => (Directory => True, others => False),
Process => Process_Directories'Access);
exception
when Generate_Site_Exception =>
Show_Message
(Text =>
"[" &
Ada.Calendar.Formatting.Image
(Date => Clock, Time_Zone => UTC_Time_Offset) &
"] " & "Site rebuilding has been interrupted.");
if Yass_Config.Stop_Server_On_Error then
if Yass_Config.Server_Enabled then
Shutdown_Server;
Show_Message(Text => "done.", Message_Type => SUCCESS);
end if;
Show_Message
(Text => "Stopping monitoring site changes...done.",
Message_Type => SUCCESS);
OS_Exit(Status => 0);
end if;
end Monitor_Directory;
begin
select
accept Start;
-- Load the program modules with 'start' hook
Load_Modules
(State => "start", Page_Tags => Page_Tags,
Page_Table_Tags => Page_Table_Tags);
-- Load data from exisiting sitemap or create new set of data or nothing if sitemap generation is disabled
Start_Sitemap;
-- Load data from existing atom feed or create new set of data or nothing if atom feed generation is disabled
Start_Atom_Feed;
Monitor_Site_Loop :
loop
Site_Rebuild := False;
-- Monitor the site project directory for changes
Monitor_Directory(Name => To_String(Source => Site_Directory));
if Site_Rebuild then
-- Save atom feed to file or nothing if atom feed generation is disabled
Save_Atom_Feed;
-- Save sitemap to file or nothing if sitemap generation is disabled
Save_Sitemap;
-- Load the program modules with 'end' hook
Load_Modules
(State => "end", Page_Tags => Page_Tags,
Page_Table_Tags => Page_Table_Tags);
Put_Line
(Item =>
"[" &
Ada.Calendar.Formatting.Image
(Date => Clock, Time_Zone => UTC_Time_Offset) &
"] " & "Site was rebuild.");
end if;
-- Wait before next check
delay Yass_Config.Monitor_Interval;
end loop Monitor_Site_Loop;
or
terminate;
end select;
end Monitor_Site;
task body Monitor_Config is
Config_Last_Modified: Time; --## rule line off IMPROPER_INITIALIZATION
begin
select
accept Start;
Monitor_Config_Loop :
loop
Config_Last_Modified :=
Modification_Time
(Name =>
To_String(Source => Site_Directory) & Dir_Separator &
"site.cfg");
-- Wait before next check
delay Yass_Config.Monitor_Config_Interval;
-- Update configuration if needed
if Config_Last_Modified /=
Modification_Time
(Name =>
To_String(Source => Site_Directory) & Dir_Separator &
"site.cfg") then
Put_Line
(Item =>
"Site configuration was changed, reconfiguring the project.");
Parse_Config
(Directory_Name => To_String(Source => Site_Directory));
Shutdown_Server;
Show_Message(Text => "done", Message_Type => Messages.SUCCESS);
if Yass_Config.Server_Enabled then
Start_Server;
end if;
end if;
end loop Monitor_Config_Loop;
or
terminate;
end select;
end Monitor_Config;
function Callback(Request: AWS.Status.Data) return AWS.Response.Data is
use AWS.Services.Directory;
Uri: constant String := AWS.Status.URI(D => Request);
begin
-- Show directory listing if requested
if Kind
(Name => To_String(Source => Yass_Config.Output_Directory) & Uri) =
Directory then
return
AWS.Response.Build
(Content_Type => "text/html",
Message_Body =>
Browse
(Directory_Name =>
To_String(Source => Yass_Config.Output_Directory) & Uri,
Template_Filename =>
To_String(Source => Yass_Config.Layouts_Directory) &
Dir_Separator & "directory.html",
Request => Request));
end if;
-- Show selected page if requested
return AWS.Services.Page_Server.Callback(Request => Request);
end Callback;
procedure Start_Server is
begin
AWS.Server.Start
(Web_Server => Http_Server, Name => "YASS static page server",
Port => Yass_Config.Server_Port, Callback => Callback'Access,
Max_Connection => 5);
Put_Line
(Item =>
"Server was started. Web address: http://localhost:" &
Positive'Image(Yass_Config.Server_Port)
(Positive'Image(Yass_Config.Server_Port)'First + 1 ..
Positive'Image(Yass_Config.Server_Port)'Length) &
"/index.html Press ""Q"" for quit.");
end Start_Server;
procedure Shutdown_Server is
begin
Put(Item => "Shutting down server...");
--## rule off DIRECTLY_ACCESSED_GLOBALS
AWS.Server.Shutdown(Web_Server => Http_Server);
--## rule on DIRECTLY_ACCESSED_GLOBALS
end Shutdown_Server;
end Server;
|