1 module hunt.framework.routing.RouteGroup; 2 3 import hunt.framework.routing.ActionRouteItem; 4 import hunt.framework.routing.RouteItem; 5 import hunt.framework.auth.AuthOptions; 6 import hunt.framework.middleware.MiddlewareInterface; 7 8 import hunt.logging; 9 10 /** 11 * 12 */ 13 final class RouteGroup { 14 15 private RouteItem[] _allItems; 16 private string _guardName = DEFAULT_GURAD_NAME; 17 18 private TypeInfo_Class[] _allowedMiddlewares; 19 private TypeInfo_Class[] _skippedMiddlewares; 20 21 // type 22 enum string DEFAULT = "default"; 23 enum string HOST = "host"; 24 enum string DOMAIN = "domain"; 25 enum string PATH = "path"; 26 27 string name; 28 string type; 29 string value; 30 31 32 void appendRoutes(RouteItem[] items) { 33 _allItems ~= items; 34 } 35 36 RouteItem get(string actionId) { 37 foreach (RouteItem item; _allItems) { 38 ActionRouteItem actionItem = cast(ActionRouteItem) item; 39 if (actionItem is null) 40 continue; 41 if (actionItem.actionId == actionId) 42 return actionItem; 43 } 44 45 return null; 46 } 47 48 override string toString() { 49 return "{" ~ name ~ ", " ~ type ~ ", " ~ value ~ ", " ~ _guardName ~ "}"; 50 } 51 52 string guardName() { 53 return _guardName; 54 } 55 56 RouteGroup guardName(string value) { 57 _guardName = value; 58 return this; 59 } 60 61 void withMiddleware(T)() if(is(T : MiddlewareInterface)) { 62 _allowedMiddlewares ~= T.classinfo; 63 } 64 65 void withMiddleware(string name) { 66 try { 67 TypeInfo_Class typeInfo = MiddlewareInterface.get(name); 68 _allowedMiddlewares ~= typeInfo; 69 } catch(Exception ex) { 70 warning(ex.msg); 71 } 72 } 73 74 TypeInfo_Class[] allowedMiddlewares() { 75 return _allowedMiddlewares; 76 } 77 78 void withoutMiddleware(T)() if(is(T : MiddlewareInterface)) { 79 _skippedMiddlewares ~= T.classinfo; 80 } 81 82 void withoutMiddleware(string name) { 83 try { 84 TypeInfo_Class typeInfo = MiddlewareInterface.get(name); 85 _skippedMiddlewares ~= typeInfo; 86 } catch(Exception ex) { 87 warning(ex.msg); 88 } 89 } 90 91 TypeInfo_Class[] skippedMiddlewares() { 92 return _skippedMiddlewares; 93 } 94 }