public void Start() { if (_isRunning) { throw new InvalidOperationException(); } _isRunning = true; Console.WriteLine("Starting server..."); var server = new HttpServer(_settings.HttpOptions); _resolver = new UrlResolver(); _resolver.Add(new UrlHook(path: "/", specificPath: true), handleIndex); _resolver.Add(new UrlHook(path: "/app.html", specificPath: true), handleApp); _resolver.Add(new UrlHook(path: "/api", specificPath: true), handleApi); _resolver.Add(new UrlHook(path: null), handleStatic); server.Handler = serverHandler; server.ErrorHandler = exceptionHandler; _services.Add(new ReloadService(this, _settings.StaticPath)); if (_settings.WeatherSettings != null) { _services.Add(new WeatherService(this, _settings.WeatherSettings)); } if (_settings.TimeSettings != null) { _services.Add(new TimeService(this, _settings.TimeSettings)); } if (_settings.PingSettings != null) { _services.Add(new PingService(this, _settings.PingSettings)); } if (_settings.HttpingSettings != null) { _services.Add(new HttpingService(this, _settings.HttpingSettings, _services.OfType <PingService>().Single())); } if (_settings.RouterSettings != null) { _services.Add(new RouterService(this, _settings.RouterSettings)); } Console.WriteLine("Initialising database..."); var absoluteDbPath = Path.Combine(Path.GetDirectoryName(SettingsUtil.GetAttribute <Settings>().GetFileName()), _settings.DbFilePath); Db.Initialise(absoluteDbPath, _services); foreach (var svc in _services) { Console.WriteLine($"Initialising {svc.ServiceName}..."); svc.Start(); } server.StartListening(blocking: false); }
public void Start(string settingsPath, bool backgroundThread = false) { _settingsPath = settingsPath ?? SettingsUtil.GetAttribute <PropellerSettings>().GetFileName(); // Do one reinitialization outside of the periodic schedule so that if the first initialization fails, the service doesn’t start try { reinitialize(); } catch (Exception e) { PropellerUtil.LogException(_log ?? new ConsoleLogger(), e); throw; } // Now start the periodic checking that might trigger reinitialization base.Start(backgroundThread); }
/// <summary> /// Loads Propeller settings from the appropriate location. Using this method ensures that settings are loaded in /// exactly the same way and from the same place as the Propeller engine would load them.</summary> /// <param name="settingsPath"> /// Path and filename of the settings to load.</param> /// <param name="log"> /// Information about how the settings were loaded is logged here. Must not be <c>null</c>.</param> /// <param name="firstRunEver"> /// Adjusts the log messages for improved human readability.</param> public static PropellerSettings LoadSettings(string settingsPath, LoggerBase log, bool firstRunEver) { settingsPath = settingsPath ?? SettingsUtil.GetAttribute <PropellerSettings>().GetFileName(); log.Info((firstRunEver ? "Loading settings file: " : "Reloading settings file: ") + settingsPath); PropellerSettings settings; var success = false; try { success = SettingsUtil.LoadSettings(out settings, settingsPath); if (success) { settings.SaveQuiet(settingsPath); return(settings); } } catch (Exception e) { settings = new PropellerSettings(); log.Error("Settings file could not be loaded: {0} ({1}). Using default settings.".Fmt(e.Message, e.GetType().FullName)); } if (!success) { if (!File.Exists(settingsPath)) { try { settings.Save(settingsPath); log.Info("Default settings saved to {0}.".Fmt(settingsPath)); } catch (Exception ex) { log.Warn("Attempt to save default settings to {0} failed: {1} ({2})".Fmt(settingsPath, ex.Message, ex.GetType().FullName)); } } } return(settings); }
private static void UpdateSettingsIfNecessary() { var curVersion = Assembly.GetExecutingAssembly().GetName().Version.Major; if (App.Settings.SavedByVersion != curVersion) { try { var origName = SettingsUtil.GetAttribute <Settings>().GetFileName(); File.Copy(origName, Path.Combine( Path.GetDirectoryName(origName), Path.GetFileNameWithoutExtension(origName) + ".v" + App.Settings.SavedByVersion.ToString().PadLeft(3, '0') + Path.GetExtension(origName))); } catch { } var migrator = new Migrator(); migrator.MigrateToVersion(App.Settings, App.Settings.SavedByVersion, curVersion); } }