1 /** 2 * Copyright © DiamondMVC 2018 3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module diamond.init.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 */ 19 void handleWebServer(HttpClient client) 20 { 21 import diamond.core.webconfig; 22 23 if (webConfig.maintenance) 24 { 25 import std.algorithm: canFind; 26 27 if 28 ( 29 webConfig.maintenanceWhiteList && 30 !webConfig.maintenanceWhiteList.canFind(client.ipAddress) 31 ) 32 { 33 foreach (headerKey,headerValue; webConfig.defaultHeaders.general) 34 { 35 client.rawResponse.headers[headerKey] = headerValue; 36 } 37 38 import std.file : exists, readText; 39 40 if (!exists(webConfig.maintenance)) 41 { 42 client.write("\n"); 43 } 44 else 45 { 46 client.write(readText(webConfig.maintenance)); 47 } 48 return; 49 } 50 } 51 52 import diamond.init.web : getView; 53 54 import diamond.views.view : View; 55 56 View page; 57 58 if (webConfig.viewOnly) 59 { 60 page = getView(client, new Route("__view"), false, true); 61 } 62 else 63 { 64 page = getView(client, client.route, true); 65 } 66 67 if (!page) 68 { 69 client.notFound(); 70 } 71 72 string pageResult; 73 74 if (webConfig.shouldCacheViews && page.cached) 75 { 76 pageResult = client.session.getCachedView(page.name); 77 } 78 79 import diamond.core.webconfig; 80 81 foreach (headerKey,headerValue; webConfig.defaultHeaders.general) 82 { 83 client.rawResponse.headers[headerKey] = headerValue; 84 } 85 86 if (!pageResult) 87 { 88 pageResult = page.generate(); 89 90 if (client.redirected || !client.isLastRoute) 91 { 92 return; 93 } 94 95 if (webConfig.shouldCacheViews && pageResult && pageResult.length && page.cached) 96 { 97 client.session.cacheView(page.name, pageResult); 98 } 99 } 100 101 if (pageResult && pageResult.length) 102 { 103 client.write(pageResult); 104 } 105 else 106 { 107 client.write("\n"); 108 } 109 } 110 }