1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.core.webinitialization;
7 
8 import diamond.core.apptype;
9 
10 static if (isWebApi)
11 {
12   /**
13   * Generates the global view data.
14   * Returns:
15   *   A string containing all global view data such as imports etc.
16   */
17   string generateGlobalView()
18   {
19     string content;
20 
21     import diamond.core.io : handleCTFEFile;
22 
23     mixin handleCTFEFile!("globalview.d", q{
24       content = __fileResult;
25     });
26     handle();
27 
28     return content ? content : "";
29   }
30 
31   /**
32   * Generates the controller data.
33   * Returns:
34   *   An array with the names of the controllers to handle.
35   */
36   string[] generateControllerData()
37   {
38     import std.array : replace, split, array;
39     import std..string : strip;
40     import std.algorithm : filter;
41 
42     string[] controllers;
43 
44     import diamond.core.io : handleCTFEFile;
45 
46     mixin handleCTFEFile!("controllers.config", q{
47       controllers = __fileResult.replace("\r", "").split("\n").filter!(c => c && c.strip().length).array;
48     });
49     handle();
50 
51     return controllers ? controllers : [];
52   }
53 }
54 
55 static if (isWebServer)
56 {
57   /// Mixin template to load view data (name + content)
58   mixin template LoadViewData(bool namesOnly = false)
59   {
60     /// Generates the functon "getViewData()" which gives you an AA like content[viewName]
61     private string generateViewData()
62     {
63       string viewDataString = "string[string] getViewData()
64       {
65         string[string] viewData;
66       ";
67 
68       static if (__traits(compiles, { auto s = import("views.config"); }))
69       {
70         import std..string : strip;
71         import std.array : split, replace;
72 
73         enum viewConfig = import("views.config");
74 
75         foreach (line; viewConfig.split("\n"))
76         {
77           if (!line)
78           {
79             continue;
80           }
81 
82           line = line.strip().replace("\r", "");
83 
84           if (!line && line.length)
85           {
86             continue;
87           }
88 
89           auto data = line.split(".");
90 
91           if (data.length != 2)
92           {
93             continue;
94           }
95 
96           static if (namesOnly)
97           {
98             auto viewName = data[0].strip();
99 
100             viewDataString ~= "  viewData[\"" ~ viewName ~ "\"] = \"" ~ viewName  ~ "\";";
101           }
102           else
103           {
104             viewDataString ~= "  viewData[\"" ~ data[0].strip() ~ "\"] = import(\"" ~ (data[0].strip() ~ "." ~ data[1].strip()) ~ "\");";
105           }
106         }
107       }
108 
109       viewDataString ~= "  return viewData;
110       }";
111 
112       return viewDataString;
113     }
114 
115     mixin(generateViewData);
116   }
117 }