1 /** 2 * Skadi.d Web Framework 3 * 4 * Authors: Faianca 5 * Copyright: Copyright (c) 2015 Faianca 6 * License: MIT License, see LICENSE 7 */ 8 module skadi.core.kernel; 9 10 import std.functional; 11 import std.typetuple; 12 import std.stdio; 13 import std.file; 14 import std.format; 15 import std.traits; 16 17 import vibe.d; 18 import skadi.core.container; 19 import skadi.core.router; 20 import skadi.utils.dynamic; 21 import skadi.core.namespaces; 22 23 static if(__traits(compiles, { 24 import config.config; 25 })) { 26 import config.config; 27 } else { 28 enum port = 8080; 29 enum bindAddresses = ["::1", "0.0.0.0"]; 30 } 31 32 static if(__traits(compiles, { 33 import config.namespaces; 34 })) { 35 import config.namespaces; 36 } else { 37 enum Namespace[] namespaces = []; 38 } 39 40 static if(__traits(compiles, { 41 import config.container; 42 })) { 43 import config.container; 44 } else { 45 enum Service[] services = []; 46 } 47 48 49 final class Kernel 50 { 51 URLRouter router; 52 HTTPServerSettings settings; 53 54 this () 55 { 56 this.buildContainer(); 57 this.buildRouter(); 58 this.configureSettings(); 59 } 60 61 URLRouter getRouter() 62 { 63 return this.router; 64 } 65 66 HTTPServerSettings getSettings() 67 { 68 return this.settings; 69 } 70 71 /** 72 * Configure Http Settings settings 73 **/ 74 void configureSettings() 75 { 76 this.settings = new HTTPServerSettings; 77 this.settings.bindAddresses = bindAddresses; 78 this.settings.port = port; 79 } 80 81 /** 82 * Register our services. 83 **/ 84 void buildContainer() 85 { 86 auto containerIoc = Container.getInstance(); 87 foreach(Service s; TypeTupleOf!services) { 88 mixin(format( 89 q{ 90 import %s; 91 containerIoc.register!(%s); 92 }, 93 s.path, 94 s.className 95 )); 96 } 97 } 98 99 void buildRouter() 100 { 101 this.router = new URLRouter; 102 auto webSettings = new WebInterfaceSettings; 103 104 foreach(Namespace i; TypeTupleOf!namespaces) { 105 106 if (i.controllers !is null) { 107 enum Controller[] controllers = i.controllers; 108 109 foreach(Controller controller; TypeTupleOf!controllers) 110 { 111 if (controller.prefix) { 112 webSettings.urlPrefix = controller.prefix; 113 mixin(format( 114 q{ 115 import Application.%s.Controller.%s; 116 this.router.registerWebInterface(new %s(), webSettings); 117 }, 118 i.bundle, 119 controller.name, 120 controller.name, 121 )); 122 } else { 123 mixin(format( 124 q{ 125 import Application.%s.Controller.%s; 126 this.router.registerWebInterface(new %s()); 127 }, 128 i.bundle, 129 controller.name, 130 controller.name, 131 )); 132 } 133 } 134 135 } 136 } 137 138 } 139 140 void boot() 141 { 142 listenHTTP(this.settings, this.router); 143 } 144 145 }