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.app.server; 7 8 import diamond.core.apptype; 9 10 static if (isWebServer) 11 { 12 import diamond.http; 13 14 /** 15 * The handler for a generic webserver request. 16 * Params: 17 * client = The client. 18 * Returns: 19 * True if the page was found, false otherwise. 20 */ 21 bool handleWebServer(HttpClient client) 22 { 23 import diamond.core.webconfig; 24 25 if (webConfig.maintenance && webConfig.maintenance.length) 26 { 27 import std.algorithm: canFind; 28 29 if 30 ( 31 webConfig.maintenanceWhiteList && 32 !webConfig.maintenanceWhiteList.canFind(client.ipAddress) 33 ) 34 { 35 foreach (headerKey,headerValue; webConfig.defaultHeaders.general) 36 { 37 client.rawResponse.headers[headerKey] = headerValue; 38 } 39 40 import std.file : exists, readText; 41 42 if (!exists(webConfig.maintenance)) 43 { 44 client.write("\n"); 45 } 46 else 47 { 48 client.write(readText(webConfig.maintenance)); 49 } 50 51 return true; 52 } 53 } 54 55 import diamond.app.web : getView; 56 57 import diamond.views.view : View; 58 59 View page; 60 61 if (webConfig.viewOnly) 62 { 63 page = getView(client, new Route("__view"), false, true); 64 } 65 else 66 { 67 page = getView(client, client.route, true); 68 } 69 70 if (!page) 71 { 72 return false; 73 } 74 75 import diamond.views.viewcache; 76 77 string pageResult; 78 79 if (page.staticCache) 80 { 81 pageResult = getCachedView(client); 82 } 83 else if (webConfig.shouldCacheViews && page.cached) 84 { 85 pageResult = client.session.getCachedView(page.name); 86 } 87 88 import diamond.core.webconfig; 89 90 foreach (headerKey,headerValue; webConfig.defaultHeaders.general) 91 { 92 client.rawResponse.headers[headerKey] = headerValue; 93 } 94 95 if (!pageResult) 96 { 97 pageResult = page.generate(); 98 99 if (client.redirected || !client.isLastRoute) 100 { 101 return true; 102 } 103 104 if (page.staticCache) 105 { 106 cacheView(client, pageResult, page.cacheTime); 107 } 108 else if (webConfig.shouldCacheViews && pageResult && pageResult.length && page.cached) 109 { 110 client.session.cacheView(page.name, pageResult); 111 } 112 } 113 114 if (pageResult && pageResult.length) 115 { 116 client.write(pageResult); 117 } 118 else 119 { 120 client.write("\n"); 121 } 122 123 return true; 124 } 125 }