1 /* 2 * Hunt - A high-level D Programming Language Web framework that encourages rapid development and clean, pragmatic design. 3 * 4 * Copyright (C) 2015-2019, HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.framework.middleware.MiddlewareInterface; 13 14 import hunt.framework.http.Request; 15 import hunt.framework.http.Response; 16 17 import hunt.logging; 18 import hunt.Functions; 19 20 import std.exception; 21 import std.format; 22 import std.traits; 23 24 alias RouteChecker = Func2!(string, string, bool); 25 alias MiddlewareEventHandler = Func2!(MiddlewareInterface, Request, Response); 26 27 /** 28 * 29 */ 30 interface MiddlewareInterface 31 { 32 ///get the middleware name 33 string name(); 34 35 ///return null is continue, response is close the session 36 Response onProcess(Request request, Response response = null); 37 38 private __gshared TypeInfo_Class[string] _all; 39 40 static void register(T)() if(is(T : MiddlewareInterface)) { 41 string simpleName = T.stringof; 42 auto itemPtr = simpleName in _all; 43 if(itemPtr !is null) { 44 warning("The middleware [%s] will be overwritten by [%s]", 45 itemPtr.name, T.classinfo.name); 46 } 47 48 _all[simpleName] = T.classinfo; 49 } 50 51 /** 52 * Simple name 53 */ 54 static TypeInfo_Class get(string name) { 55 auto itemPtr = name in _all; 56 if(itemPtr is null) { 57 throw new Exception(format("The middleware %s has not been registered", name)); 58 } 59 60 return *itemPtr; 61 } 62 63 static TypeInfo_Class[string] all() { 64 return _all; 65 } 66 67 } 68 69 /** 70 * 71 */ 72 abstract class AbstractMiddleware : MiddlewareInterface { 73 74 /// get the middleware name 75 string name() { 76 return typeid(this).name; 77 } 78 }