//--- Methods --- public void Startup() { _log.InfoMethodCall("Startup", _uri); // create listener and make it listen to the uri _listener = new HttpListener(); _listener.IgnoreWriteExceptions = true; _listener.AuthenticationSchemes = _authenticationSheme; _listener.Prefixes.Add(_uri.ToString()); try { _listener.Start(); } catch (Exception x) { _log.WarnExceptionFormat(x, "Unable to start listening on '{0}'", _uri); throw; } _listener.BeginGetContext(RequestHandler, _listener); // register plug factory for this uri Plug.AddEndpoint(this); }
//--- Class Constructors --- static MockEndpoint() { Plug.AddEndpoint(Instance); }
//--- Constructors --- /// <summary> /// Create new handler instance /// </summary> public HttpHandler() { if (_env == null) { lock (SyncRoot) { if (_env == null) { _log.InfoMethodCall("Startup"); try { _log.InfoMethodCall("ctor: initializing HttpHandler"); NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings; // determine storage locations string basePath = HttpContext.Current.ApplicationInstance.Server.MapPath("~"); string storagePath = settings["storage-dir"] ?? settings["service-dir"]; if (string.IsNullOrEmpty(storagePath)) { storagePath = Path.Combine(basePath, "storage"); } else if (!Path.IsPathRooted(storagePath)) { storagePath = Path.Combine(basePath, storagePath); } // read configuration string apikey = settings["apikey"]; _uri = new XUri(settings["public-uri"] ?? settings["root-uri"] ?? "http://localhost/@api"); _minSimilarity = _uri.MaxSimilarity; _dreamInParamAuthtoken = settings["dream.in.authtoken"]; // start dreamhost XDoc config = new XDoc("config") .Elem("guid", settings["guid"]) .Elem("uri.public", _uri.ToString()) .Elem("storage-dir", storagePath) .Elem("host-path", settings["host-path"]) .Elem("connect-limit", settings["connect-limit"]) .Elem("apikey", apikey); IDreamEnvironment env = new DreamHostService(); env.Initialize(config); // load assemblies in 'services' folder string servicesFolder = settings["service-dir"] ?? Path.Combine("bin", "services"); if (!Path.IsPathRooted(servicesFolder)) { servicesFolder = Path.Combine(basePath, servicesFolder); } _log.DebugFormat("examining services directory '{0}'", servicesFolder); if (Directory.Exists(servicesFolder)) { Plug host = env.Self.With("apikey", apikey); foreach (string file in Directory.GetFiles(servicesFolder, "*.dll")) { string assembly = Path.GetFileNameWithoutExtension(file); _log.DebugFormat("attempting to load '{0}'", assembly); // register assembly blueprints DreamMessage response = host.At("load").With("name", assembly).Post(new Result <DreamMessage>(TimeSpan.MaxValue)).Wait(); if (!response.IsSuccessful) { _log.WarnFormat("DreamHost: ERROR: assembly '{0}' failed to load", file); } } } else { _log.WarnFormat("DreamHost: WARN: no services directory '{0}'", servicesFolder); } // execute script string scriptFilename = settings["script"]; if (!string.IsNullOrEmpty(scriptFilename)) { string filename = scriptFilename; if (!Path.IsPathRooted(filename)) { filename = Path.Combine(basePath, filename); } // execute xml script file XDoc script = XDocFactory.LoadFrom(filename, MimeType.XML); Plug host = env.Self.With("apikey", apikey); host.At("execute").Post(script); } // register plug factory for this uri Plug.AddEndpoint(this); // set _env variable so other constructors don't initialize it anymore _env = env; } catch (Exception e) { _log.ErrorExceptionMethodCall(e, "ctor"); throw; } } } } }