1 module scorpion.context; 2 3 import lighttp : ServerRequest, ServerResponse; 4 5 import scorpion.config : Config; 6 import scorpion.session : SessionManager, Session; 7 8 /** 9 * Represents the context of the application in the current thread. 10 */ 11 final class Context { 12 13 private Config _config; 14 15 private SessionManager _sessionManager; 16 private Session _session; 17 18 private ServerRequest _request; 19 private ServerResponse _response; 20 21 this(Config config) { 22 _config = config; 23 _sessionManager = new SessionManager(config.get("scorpion.session.cookie", "DSESSIONID")); 24 } 25 26 /** 27 * Updates the context using the data from the next request. 28 */ 29 void refresh(ServerRequest request, ServerResponse response) { 30 _session = null; 31 _request = request; 32 _response = response; 33 } 34 35 /** 36 * Gets the server's configuration. 37 */ 38 @property Config config() { 39 return _config; 40 } 41 42 /** 43 * Gets the current request. 44 */ 45 @property ServerRequest request() { 46 return _request; 47 } 48 49 /** 50 * Gets the current response. 51 */ 52 @property ServerResponse response() { 53 return _response; 54 } 55 56 /** 57 * Gets the current session, lazily initialized. 58 */ 59 @property Session session() { 60 if(_session is null) _session = _sessionManager.get(request); 61 return _session; 62 } 63 64 }