/// <summary> /// Instantiate the object. /// </summary> /// <param name="dbFile">Database file.</param> public RbacServer(string dbFile) { if (String.IsNullOrEmpty(dbFile)) { throw new ArgumentNullException(nameof(dbFile)); } _ORM = new WatsonORM(new DatabaseSettings(dbFile)); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(UserRole)); _ORM.InitializeTable(typeof(RolePermission)); }
/// <summary> /// Instantiate the ledger. /// </summary> /// <param name="filename">Sqlite database filename.</param> public Ledger(string filename) { if (String.IsNullOrEmpty(filename)) { throw new ArgumentNullException(nameof(filename)); } _DatabaseFile = filename; _DatabaseSettings = new DatabaseSettings(_DatabaseFile); _ORM = new WatsonORM(_DatabaseSettings); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(Account)); _ORM.InitializeTable(typeof(Entry)); }
/// <summary> /// Instantiates the object /// </summary> /// <param name="indexFile">The index database file.</param> public Database(WatsonORM orm) { if (orm == null) { throw new ArgumentNullException(nameof(orm)); } _ORM = orm; _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(DedupeConfig)); _ORM.InitializeTable(typeof(DedupeObject)); _ORM.InitializeTable(typeof(DedupeChunk)); _ORM.InitializeTable(typeof(DedupeObjectMap)); }
/// <summary> /// Instantiates the object /// </summary> /// <param name="indexFile">The index database file.</param> public SqliteProvider(string indexFile) { if (String.IsNullOrEmpty(indexFile)) { throw new ArgumentNullException(nameof(indexFile)); } _IndexFile = indexFile; _Settings = new DatabaseSettings(_IndexFile); _ORM = new WatsonORM(_Settings); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(DedupeConfig)); _ORM.InitializeTable(typeof(DedupeObject)); _ORM.InitializeTable(typeof(DedupeChunk)); _ORM.InitializeTable(typeof(DedupeObjectMap)); }
/// <summary> /// Instantiate the IndexEngine. /// </summary> /// <param name="databaseFile">Database filename to use. If this file does not exist, it will be created for you.</param> public IndexEngine(string databaseFile) { if (String.IsNullOrEmpty(databaseFile)) { throw new ArgumentNullException(databaseFile); } _Token = _TokenSource.Token; _DbFilename = databaseFile; _DbSettings = new DatabaseSettings(_DbFilename); _ORM = new WatsonORM(_DbSettings); _CurrentThreads = 0; _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(Document)); _ORM.InitializeTable(typeof(IndexEntry)); _ORM.Query("PRAGMA journal_mode = TRUNCATE"); }
private void RunSetup() { #region Variables DateTime timestamp = DateTime.Now; Settings settings = new Settings(); #endregion #region Welcome Console.WriteLine(""); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(@" _ _ "); Console.WriteLine(@" | |____ ___ __| |__ __ _ ___ ___ "); Console.WriteLine(@" | / /\ V / '_ \ '_ \/ _` (_-</ -_) "); Console.WriteLine(@" |_\_\ \_/| .__/_.__/\__,_/__/\___| "); Console.WriteLine(@" |_| "); Console.WriteLine(@" "); Console.ResetColor(); Console.WriteLine(""); Console.WriteLine("Kvpbase Storage Server"); Console.WriteLine(""); // 1 2 3 4 5 6 7 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("Thank you for using Kvpbase! We'll put together a basic system configuration"); Console.WriteLine("so you can be up and running quickly. You'll want to modify the System.json"); Console.WriteLine("file after to ensure a more secure operating environment."); #endregion #region Initial-Settings settings.EnableConsole = true; settings.Server = new Settings.SettingsServer(); settings.Server.Port = 8000; settings.Server.DnsHostname = "localhost"; settings.Server.Ssl = false; settings.Server.HeaderApiKey = "x-api-key"; settings.Server.MaxObjectSize = 2199023255552; // 2TB object size settings.Server.MaxTransferSize = 536870912; // 512MB transfer size settings.Storage = new Settings.SettingsStorage(); settings.Storage.Directory = "./Storage/"; Directory.CreateDirectory(settings.Storage.Directory); settings.Syslog = new Settings.SettingsSyslog(); settings.Syslog.ConsoleLogging = true; settings.Syslog.Header = "kvpbase"; settings.Syslog.ServerIp = "127.0.0.1"; settings.Syslog.ServerPort = 514; settings.Syslog.MinimumLevel = Severity.Info; settings.Syslog.FileLogging = true; settings.Syslog.LogDirectory = "./Logs/"; if (!Directory.Exists(settings.Syslog.LogDirectory)) { Directory.CreateDirectory(settings.Syslog.LogDirectory); } settings.Debug = new Settings.SettingsDebug(); settings.Debug.Database = false; settings.Debug.HttpRequest = false; #endregion #region Databases // 1 2 3 4 5 6 7 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.WriteLine(""); Console.WriteLine("Kvpbase requires access to a database, either Sqlite, Microsoft SQL Server,"); Console.WriteLine("MySQL, or PostgreSQL. Please provide access details for your database. The"); Console.WriteLine("user account supplied must have the ability to CREATE and DROP tables along"); Console.WriteLine("with issue queries containing SELECT, INSERT, UPDATE, and DELETE. Setup will"); Console.WriteLine("attempt to create tables on your behalf if they dont exist."); Console.WriteLine(""); bool dbSet = false; while (!dbSet) { string userInput = Common.InputString("Database type [sqlite|sqlserver|mysql|postgresql]:", "sqlite", false); switch (userInput) { case "sqlite": settings.Database = new DatabaseSettings( Common.InputString("Filename:", "./Kvpbase.db", false) ); // 1 2 3 4 5 6 7 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.WriteLine(""); Console.WriteLine("IMPORTANT: Using Sqlite in production is not recommended if deploying within a"); Console.WriteLine("containerized environment and the database file is stored within the container."); Console.WriteLine("Store the database file in external storage to ensure persistence."); Console.WriteLine(""); dbSet = true; break; case "sqlserver": settings.Database = new DatabaseSettings( Common.InputString("Hostname:", "localhost", false), Common.InputInteger("Port:", 1433, true, false), Common.InputString("Username:"******"sa", false), Common.InputString("Password:"******"Instance (for SQLEXPRESS):", null, true), Common.InputString("Database name:", "kvpbase", false) ); dbSet = true; break; case "mysql": settings.Database = new DatabaseSettings( DbTypes.Mysql, Common.InputString("Hostname:", "localhost", false), Common.InputInteger("Port:", 3306, true, false), Common.InputString("Username:"******"root", false), Common.InputString("Password:"******"Schema name:", "kvpbase", false) ); dbSet = true; break; case "postgresql": settings.Database = new DatabaseSettings( DbTypes.Postgresql, Common.InputString("Hostname:", "localhost", false), Common.InputInteger("Port:", 5432, true, false), Common.InputString("Username:"******"postgres", false), Common.InputString("Password:"******"Schema name:", "kvpbase", false) ); dbSet = true; break; } } #endregion #region Write-Files-and-Records Console.WriteLine(""); Console.WriteLine("| Writing system.json"); Common.WriteFile("System.json", Encoding.UTF8.GetBytes(Common.SerializeJson(settings, true))); Console.WriteLine("| Initializing logging"); LoggingModule logging = new LoggingModule("127.0.0.1", 514); logging.MinimumSeverity = Severity.Info; Console.WriteLine("| Initializing database"); WatsonORM orm = new WatsonORM(settings.Database); orm.InitializeDatabase(); orm.InitializeTable(typeof(ApiKey)); orm.InitializeTable(typeof(AuditLogEntry)); orm.InitializeTable(typeof(Container)); orm.InitializeTable(typeof(ContainerKeyValuePair)); orm.InitializeTable(typeof(ObjectKeyValuePair)); orm.InitializeTable(typeof(ObjectMetadata)); orm.InitializeTable(typeof(Permission)); orm.InitializeTable(typeof(UrlLock)); orm.InitializeTable(typeof(UserMaster)); Console.WriteLine("| Adding user [default]"); UserMaster user = new UserMaster(); user.GUID = "default"; user.Email = "*****@*****.**"; user.Password = "******"; user.FirstName = "Default"; user.LastName = "User"; user.CreatedUtc = timestamp; user.Active = true; user = orm.Insert <UserMaster>(user); Console.WriteLine("| Adding API key [default]"); ApiKey apiKey = new ApiKey(); apiKey = new ApiKey(); apiKey.GUID = "default"; apiKey.UserGUID = user.GUID; apiKey.Active = true; apiKey = orm.Insert <ApiKey>(apiKey); Console.WriteLine("| Adding permission [default]"); Permission perm = new Permission(); perm.GUID = Guid.NewGuid().ToString(); perm.UserGUID = user.GUID; perm.ContainerGUID = "default"; perm.DeleteContainer = true; perm.DeleteObject = true; perm.ReadContainer = true; perm.ReadObject = true; perm.WriteContainer = true; perm.WriteObject = true; perm.IsAdmin = true; perm.ApiKeyGUID = apiKey.GUID; perm.Notes = "Created by setup script"; perm.Active = true; perm = orm.Insert <Permission>(perm); Console.WriteLine("| Creating container [default]"); string htmlFile = SampleHtmlFile("http://github.com/kvpbase"); string textFile = SampleTextFile("http://github.com/kvpbase"); string jsonFile = SampleJsonFile("http://github.com/kvpbase"); ContainerManager containerMgr = new ContainerManager(settings, logging, orm); Container container = new Container(); container.UserGUID = "default"; container.Name = "default"; container.GUID = "default"; container.ObjectsDirectory = settings.Storage.Directory + container.UserGUID + "/" + container.Name + "/"; container.EnableAuditLogging = true; container.IsPublicRead = true; container.IsPublicWrite = false; containerMgr.Add(container); ContainerClient client = containerMgr.GetContainerClient("default", "default"); Console.WriteLine("| Writing sample files to container [default]"); ErrorCode error; client.WriteObject("hello.html", "text/html", Encoding.UTF8.GetBytes(htmlFile), null, out error); client.WriteObject("hello.txt", "text/plain", Encoding.UTF8.GetBytes(textFile), null, out error); client.WriteObject("hello.json", "application/json", Encoding.UTF8.GetBytes(jsonFile), null, out error); #endregion #region Wrap-Up // 1 2 3 4 5 6 7 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.WriteLine(""); Console.WriteLine(Common.Line(79, "-")); Console.WriteLine(""); Console.WriteLine("We have created your first user account and permissions."); Console.WriteLine(""); ConsoleColor prior = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("IMPORTANT: The default API key setup creates has administrative privileges and"); Console.WriteLine("can use admin APIs. We recommend you modify your configuration and reduce its"); Console.WriteLine("permissions before exposing Kvpbase outside of localhost."); Console.ForegroundColor = prior; Console.WriteLine(""); Console.WriteLine(" API Key : " + apiKey.GUID); Console.WriteLine(""); Console.WriteLine("We've also created sample files for you so that you can see your node in"); Console.WriteLine("action. Go to the following URLs in your browser and see what happens!"); Console.WriteLine(""); Console.WriteLine(" http://localhost:8000/"); Console.WriteLine(" http://localhost:8000/default/default?_container&html"); Console.WriteLine(" http://localhost:8000/default/default/hello.html"); Console.WriteLine(" http://localhost:8000/default/default/hello.html?metadata"); Console.WriteLine(" http://localhost:8000/default/default/hello.txt"); Console.WriteLine(" http://localhost:8000/default/default/hello.json"); Console.WriteLine(""); #endregion }
static void Main(string[] args) { try { #region Setup Console.Write("DB type [sqlserver|mysql|postgresql|sqlite]: "); _DbType = Console.ReadLine(); if (String.IsNullOrEmpty(_DbType)) { return; } _DbType = _DbType.ToLower(); if (_DbType.Equals("sqlserver") || _DbType.Equals("mysql") || _DbType.Equals("postgresql")) { Console.Write("User: "******"Password: "******"sqlserver": _Settings = new DatabaseSettings(DbTypes.SqlServer, "localhost", 1433, _Username, _Password, "test"); break; case "mysql": _Settings = new DatabaseSettings(DbTypes.Mysql, "localhost", 3306, _Username, _Password, "test"); break; case "postgresql": _Settings = new DatabaseSettings(DbTypes.Postgresql, "localhost", 5432, _Username, _Password, "test"); break; default: return; } } else if (_DbType.Equals("sqlite")) { Console.Write("Filename: "); _Filename = Console.ReadLine(); if (String.IsNullOrEmpty(_Filename)) { return; } _Settings = new DatabaseSettings(_Filename); } else { Console.WriteLine("Invalid database type."); return; } _Orm = new WatsonORM(_Settings); _Orm.InitializeDatabase(); DebugSettings debug = new DebugSettings(); debug.DatabaseQueries = true; debug.DatabaseResults = true; _Orm.Logger = Logger; _Orm.Debug = debug; _Orm.InitializeTable(typeof(Person)); _Orm.TruncateTable(typeof(Person)); Console.WriteLine("Using table: " + _Orm.GetTableName(typeof(Person))); #endregion #region Insert-Records Person p1 = new Person("Abraham", "Lincoln", Convert.ToDateTime("1/1/1980"), null, 42, null, "initial notes p1", PersonType.Human, null, false); Person p2 = new Person("Ronald", "Reagan", Convert.ToDateTime("2/2/1981"), Convert.ToDateTime("3/3/1982"), 43, 43, "initial notes p2", PersonType.Cat, PersonType.Cat, true); Person p3 = new Person("George", "Bush", Convert.ToDateTime("3/3/1982"), null, 44, null, "initial notes p3", PersonType.Dog, PersonType.Dog, false); Person p4 = new Person("Barack", "Obama", Convert.ToDateTime("4/4/1983"), Convert.ToDateTime("5/5/1983"), 45, null, "initial notes p4", PersonType.Human, null, true); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p1"); p1 = _Orm.Insert <Person>(p1); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p2"); p2 = _Orm.Insert <Person>(p2); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p3"); p3 = _Orm.Insert <Person>(p3); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p4"); p4 = _Orm.Insert <Person>(p4); #endregion #region Insert-Multiple-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p5 through p8"); Person p5 = new Person("Jason", "Christner", Convert.ToDateTime("4/21/2020"), null, 1, null, "initial notes p5", PersonType.Human, null, false); Person p6 = new Person("Maria", "Sanchez", Convert.ToDateTime("10/10/1982"), Convert.ToDateTime("10/10/1982"), 38, null, "initial notes p6", PersonType.Cat, PersonType.Cat, true); Person p7 = new Person("Eddie", "Van Halen", Convert.ToDateTime("3/3/1982"), null, 44, null, "initial notes p7", PersonType.Dog, PersonType.Dog, false); Person p8 = new Person("Steve", "Vai", Convert.ToDateTime("4/4/1983"), Convert.ToDateTime("5/5/1983"), 45, null, "initial notes p8", PersonType.Human, null, true); List <Person> people = new List <Person> { p5, p6, p7, p8 }; _Orm.InsertMultiple <Person>(people); #endregion #region Exists-Count-Sum for (int i = 0; i < 8; i++) { Console.WriteLine(""); } DbExpression existsExpression = new DbExpression(_Orm.GetColumnName <Person>(nameof(Person.Id)), DbOperators.GreaterThan, 0); Console.WriteLine("| Checking existence of records: " + _Orm.Exists <Person>(existsExpression)); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } DbExpression countExpression = new DbExpression(_Orm.GetColumnName <Person>(nameof(Person.Id)), DbOperators.GreaterThan, 2); Console.WriteLine("| Checking count of records: " + _Orm.Count <Person>(countExpression)); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Checking sum of ages: " + _Orm.Sum <Person>(_Orm.GetColumnName <Person>(nameof(Person.Age)), existsExpression)); #endregion #region Select for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting many by column name"); DbExpression eSelect1 = new DbExpression("id", DbOperators.GreaterThan, 0); List <Person> selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect1); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting many by property name"); DbExpression eSelect2 = new DbExpression( _Orm.GetColumnName <Person>(nameof(Person.FirstName)), DbOperators.Equals, "Abraham"); List <Person> selectedList2 = _Orm.SelectMany <Person>(null, null, eSelect2); Console.WriteLine("| Retrieved: " + selectedList2.Count + " records"); foreach (Person curr in selectedList2) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by ID"); Person pSelected = _Orm.SelectByPrimaryKey <Person>(3); Console.WriteLine("| Selected: " + pSelected.ToString()); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting first by column name"); DbExpression eSelect3 = new DbExpression("id", DbOperators.Equals, 4); pSelected = _Orm.SelectFirst <Person>(eSelect3); Console.WriteLine("| Selected: " + pSelected.ToString()); #endregion #region Update-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p1"); p1.Notes = "updated notes p1"; p1 = _Orm.Update <Person>(p1); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p2"); p2.Notes = "updated notes p2"; p2 = _Orm.Update <Person>(p2); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p3"); p3.Notes = "updated notes p3"; p3 = _Orm.Update <Person>(p3); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p4"); p4.Notes = "updated notes p4"; p4 = _Orm.Update <Person>(p4); #endregion #region Update-Many-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating many records"); Dictionary <string, object> updateVals = new Dictionary <string, object>(); updateVals.Add(_Orm.GetColumnName <Person>("Notes"), "Updated during update many!"); _Orm.UpdateMany <Person>(eSelect1, updateVals); #endregion #region Select for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting many, test 1"); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect1); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by ID"); pSelected = _Orm.SelectByPrimaryKey <Person>(3); Console.WriteLine("| Selected: " + pSelected.ToString()); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting first"); pSelected = _Orm.SelectFirst <Person>(eSelect2); Console.WriteLine("| Selected: " + pSelected.ToString()); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting between, test 1"); DbExpression eSelect4 = DbExpression.Between("id", new List <object> { 2, 4 }); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect4); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by persontype"); DbExpression eSelect5 = new DbExpression("persontype", DbOperators.Equals, PersonType.Dog); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect5); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting handsome people"); DbExpression eSelect6 = new DbExpression("ishandsome", DbOperators.Equals, true); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect6); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by reverse ID order"); DbExpression eSelect7 = new DbExpression("id", DbOperators.GreaterThan, 0); DbResultOrder[] resultOrder = new DbResultOrder[1]; resultOrder[0] = new DbResultOrder("id", DbOrderDirection.Descending); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect7, resultOrder); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } #endregion #region Exception for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Catching exception and displaying query"); try { _Orm.Query("SELECT * FROM person ((("); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); Console.WriteLine("Query : " + e.Data["Query"]); } #endregion #region Delete-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Deleting p1"); _Orm.Delete <Person>(p1); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Deleting p2"); _Orm.DeleteByPrimaryKey <Person>(2); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Deleting p3 and p4"); DbExpression eDelete = new DbExpression("id", DbOperators.GreaterThan, 2); _Orm.DeleteMany <Person>(eDelete); #endregion } catch (Exception e) { // Get stack trace for the exception with source file information var st = new StackTrace(e, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Console.WriteLine("Stack trace:" + Environment.NewLine + SerializeJson(st, true)); Console.WriteLine("Stack frame: " + Environment.NewLine + SerializeJson(st, true)); Console.WriteLine("Line number: " + line); Console.WriteLine("Exception: " + Environment.NewLine + SerializeJson(e, true)); } Console.WriteLine(""); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }
static void Main(string[] args) { try { #region Setup Console.Write("Filename: "); _Filename = Console.ReadLine(); if (String.IsNullOrEmpty(_Filename)) { return; } _Settings = new DatabaseSettings(_Filename); _Orm = new WatsonORM(_Settings); _Orm.InitializeDatabase(); DebugSettings debug = new DebugSettings(); debug.DatabaseQueries = true; debug.DatabaseResults = true; _Orm.Logger = Logger; _Orm.Debug = debug; _Orm.InitializeTable(typeof(Person)); _Orm.TruncateTable(typeof(Person)); Console.WriteLine("Using table: " + _Orm.GetTableName(typeof(Person))); #endregion #region Create-and-Store-Records DateTimeOffset localTime = new DateTimeOffset(Convert.ToDateTime("1/1/2021")); Person p1 = new Person("Abraham", "Lincoln", Convert.ToDateTime("1/1/1980"), null, localTime, null, 42, null, "initial notes p1", PersonType.Human, null, false); Person p2 = new Person("Ronald", "Reagan", Convert.ToDateTime("2/2/1981"), Convert.ToDateTime("3/3/1982"), localTime, localTime, 43, 43, "initial notes p2", PersonType.Cat, PersonType.Cat, true); Person p3 = new Person("George", "Bush", Convert.ToDateTime("3/3/1982"), null, localTime, null, 44, null, "initial notes p3", PersonType.Dog, PersonType.Dog, false); Person p4 = new Person("Barack", "Obama", Convert.ToDateTime("4/4/1983"), Convert.ToDateTime("5/5/1983"), localTime, localTime, 45, null, "initial notes p4", PersonType.Human, null, true); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p1"); p1 = _Orm.Insert <Person>(p1); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p2"); p2 = _Orm.Insert <Person>(p2); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p3"); p3 = _Orm.Insert <Person>(p3); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Creating p4"); p4 = _Orm.Insert <Person>(p4); #endregion #region Select for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting many by column name"); DbExpression eSelect1 = new DbExpression("id", DbOperators.GreaterThan, 0); List <Person> selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect1); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting many by property name"); DbExpression eSelect2 = new DbExpression( _Orm.GetColumnName <Person>(nameof(Person.FirstName)), DbOperators.Equals, "Abraham"); List <Person> selectedList2 = _Orm.SelectMany <Person>(null, null, eSelect2); Console.WriteLine("| Retrieved: " + selectedList2.Count + " records"); foreach (Person curr in selectedList2) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by ID"); Person pSelected = _Orm.SelectByPrimaryKey <Person>(3); Console.WriteLine("| Selected: " + pSelected.ToString()); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting first by column name"); DbExpression eSelect3 = new DbExpression("id", DbOperators.Equals, 4); pSelected = _Orm.SelectFirst <Person>(eSelect3); Console.WriteLine("| Selected: " + pSelected.ToString()); #endregion #region Update-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p1"); p1.Notes = "updated notes p1"; p1.NullableType = null; p1 = _Orm.Update <Person>(p1); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p2"); p2.Notes = "updated notes p2"; p2.NullableType = null; p2 = _Orm.Update <Person>(p2); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p3"); p3.Notes = "updated notes p3"; p3.NullableType = null; p3 = _Orm.Update <Person>(p3); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating p4"); p4.Notes = "updated notes p4"; p4.NullableType = null; p4 = _Orm.Update <Person>(p4); #endregion #region Update-Many-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Updating many records"); Dictionary <string, object> updateVals = new Dictionary <string, object>(); updateVals.Add(_Orm.GetColumnName <Person>("Notes"), "Updated during update many!"); _Orm.UpdateMany <Person>(eSelect1, updateVals); #endregion #region Select for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting many, test 1"); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect1); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by ID"); pSelected = _Orm.SelectByPrimaryKey <Person>(3); Console.WriteLine("| Selected: " + pSelected.ToString()); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting first"); pSelected = _Orm.SelectFirst <Person>(eSelect2); Console.WriteLine("| Selected: " + pSelected.ToString()); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting between, test 1"); DbExpression eSelect4 = DbExpression.Between("id", new List <object> { 2, 4 }); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect4); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by persontype"); DbExpression eSelect5 = new DbExpression("persontype", DbOperators.Equals, PersonType.Dog); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect5); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting handsome people"); DbExpression eSelect6 = new DbExpression("ishandsome", DbOperators.Equals, true); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect6); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Selecting by reverse ID order"); DbExpression eSelect7 = new DbExpression("id", DbOperators.GreaterThan, 0); DbResultOrder[] resultOrder = new DbResultOrder[1]; resultOrder[0] = new DbResultOrder("id", DbOrderDirection.Descending); selectedList1 = _Orm.SelectMany <Person>(null, null, eSelect7, resultOrder); Console.WriteLine("| Retrieved: " + selectedList1.Count + " records"); foreach (Person curr in selectedList1) { Console.WriteLine(" | " + curr.ToString()); } #endregion #region Exception for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Catching exception and displaying query"); try { _Orm.Query("SELECT * FROM person ((("); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); Console.WriteLine("Query : " + e.Data["Query"]); } #endregion #region Delete-Records for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Deleting p1"); _Orm.Delete <Person>(p1); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Deleting p2"); _Orm.DeleteByPrimaryKey <Person>(2); for (int i = 0; i < 8; i++) { Console.WriteLine(""); } Console.WriteLine("| Deleting p3 and p4"); DbExpression eDelete = new DbExpression("id", DbOperators.GreaterThan, 2); _Orm.DeleteMany <Person>(eDelete); #endregion } catch (Exception e) { // Get stack trace for the exception with source file information var st = new StackTrace(e, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Console.WriteLine("Stack trace:" + Environment.NewLine + SerializeJson(st, true)); Console.WriteLine("Stack frame: " + Environment.NewLine + SerializeJson(st, true)); Console.WriteLine("Line number: " + line); Console.WriteLine("Exception: " + Environment.NewLine + SerializeJson(e, true)); } Console.WriteLine(""); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }
private static void InitializeGlobals() { ConsoleColor prior = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("| Initializing logging : "); _Logging = new LoggingModule( _Settings.Logging.SyslogServerIp, _Settings.Logging.SyslogServerPort, _Settings.Logging.ConsoleLogging); if (_Settings.Logging.FileLogging && !String.IsNullOrEmpty(_Settings.Logging.Filename)) { if (String.IsNullOrEmpty(_Settings.Logging.FileDirectory)) { _Settings.Logging.FileDirectory = "./"; } if (_Settings.Logging.FileDirectory.Contains("\\")) { _Settings.Logging.FileDirectory = _Settings.Logging.FileDirectory.Replace("\\", "/"); } if (!Directory.Exists(_Settings.Logging.FileDirectory)) { Directory.CreateDirectory(_Settings.Logging.FileDirectory); } _Logging.Settings.FileLogging = FileLoggingMode.FileWithDate; _Logging.Settings.LogFilename = _Settings.Logging.FileDirectory + _Settings.Logging.Filename; } Console.WriteLine("[success]"); Console.Write("| Initializing Komodo daemon : "); _DaemonSettings = _Settings.ToDaemonSettings(); _Daemon = new KomodoDaemon(_DaemonSettings); Console.WriteLine("[success]"); Console.Write("| Initializing database : "); _ORM = new WatsonORM(_Settings.Database.ToDatabaseSettings()); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(ApiKey)); _ORM.InitializeTable(typeof(Index)); _ORM.InitializeTable(typeof(Metadata)); _ORM.InitializeTable(typeof(MetadataDocument)); _ORM.InitializeTable(typeof(Node)); _ORM.InitializeTable(typeof(ParsedDocument)); _ORM.InitializeTable(typeof(Permission)); _ORM.InitializeTable(typeof(SourceDocument)); _ORM.InitializeTable(typeof(TermDoc)); _ORM.InitializeTable(typeof(TermGuid)); _ORM.InitializeTable(typeof(User)); Console.WriteLine("[success]"); Console.Write("| Initializing authentication : "); _Auth = new AuthManager(_ORM); Console.WriteLine("[success]"); Console.Write("| Initializing webserver : "); _Webserver = new WatsonWebserver.Server( _Settings.Server.ListenerHostname, _Settings.Server.ListenerPort, _Settings.Server.Ssl, RequestReceived); _Webserver.Routes.Content.Add("/Assets/", true); _Webserver.Settings.AccessControl.Mode = AccessControlMode.DefaultPermit; _Webserver.Start(); Console.WriteLine( "[success] " + (_Settings.Server.Ssl ? "https://" : "http://") + _Settings.Server.ListenerHostname + ":" + _Settings.Server.ListenerPort); if (_Settings.Server.ListenerHostname.Equals("localhost") || _Settings.Server.ListenerHostname.Equals("127.0.0.1")) { // 1 2 3 4 5 6 7 8 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(""); Console.WriteLine("WARNING: Komodo started on '" + _Settings.Server.ListenerHostname + "'"); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Komodo can only service requests from the local machine. If you wish to serve"); Console.WriteLine("external requests, edit the system.json file and specify a DNS-resolvable"); Console.WriteLine("hostname in the Server.ListenerHostname field."); Console.WriteLine(""); } List <string> adminListeners = new List <string> { "*", "+", "0.0.0.0" }; if (adminListeners.Contains(_Settings.Server.ListenerHostname)) { // 1 2 3 4 5 6 7 8 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(""); Console.WriteLine("NOTICE: Komodo is listening on a wildcard hostname: '" + _Settings.Server.ListenerHostname + "'"); Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine("Komodo must be run with administrative privileges, otherwise it will not be able"); Console.WriteLine("to respond to incoming requests."); Console.WriteLine(""); } Console.ForegroundColor = ConsoleColor.DarkGray; if (_Settings.EnableConsole) { _Console = new ConsoleManager(_Settings, ExitApplication); } Console.ForegroundColor = prior; Console.WriteLine(""); }
private static void InitializeGlobals() { ConsoleColor previous = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("| Initializing logging : "); _Logging = new LoggingModule( _Settings.Syslog.ServerIp, _Settings.Syslog.ServerPort, _Settings.Syslog.ConsoleLogging, (Severity)_Settings.Syslog.MinimumLevel, false, true, true, false, false, false); Console.WriteLine("[success]"); if (_Settings.Syslog.FileLogging) { _Logging.FileLogging = FileLoggingMode.FileWithDate; _Logging.LogFilename = "Kvpbase.StorageServer.Log"; } Console.Write("| Initializing database : "); _ORM = new WatsonORM(_Settings.Database); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(ApiKey)); _ORM.InitializeTable(typeof(AuditLogEntry)); _ORM.InitializeTable(typeof(Container)); _ORM.InitializeTable(typeof(ContainerKeyValuePair)); _ORM.InitializeTable(typeof(ObjectKeyValuePair)); _ORM.InitializeTable(typeof(ObjectMetadata)); _ORM.InitializeTable(typeof(Permission)); _ORM.InitializeTable(typeof(UrlLock)); _ORM.InitializeTable(typeof(UserMaster)); Console.WriteLine("[success]"); Console.Write("| Initializing authentication manager : "); _AuthMgr = new AuthManager(_Settings, _Logging, _ORM); Console.WriteLine("[success]"); Console.Write("| Initializing lock manager : "); _LockMgr = new LockManager(_Settings, _Logging, _ORM); Console.WriteLine("[success]"); Console.Write("| Initializing connection manager : "); _ConnMgr = new ConnectionManager(); Console.WriteLine("[success]"); Console.Write("| Initializing container manager : "); _ContainerMgr = new ContainerManager(_Settings, _Logging, _ORM); Console.WriteLine("[success]"); Console.Write("| Initializing object handler : "); _ObjectHandler = new ObjectHandler(_Settings, _Logging, _ORM, _LockMgr); Console.WriteLine("[success]"); Console.Write("| Initializing webserver : "); _Server = new Server( _Settings.Server.DnsHostname, _Settings.Server.Port, _Settings.Server.Ssl, RequestReceived); _Server.Events.ExceptionEncountered = WebserverException; Console.WriteLine("[success]"); if (_Settings.Server.Ssl) { Console.WriteLine("| https://" + _Settings.Server.DnsHostname + ":" + _Settings.Server.Port); } else { Console.WriteLine("| http://" + _Settings.Server.DnsHostname + ":" + _Settings.Server.Port); } _ConsoleMgr = new ConsoleManager( _Settings, _Logging, _ContainerMgr, _ObjectHandler); Console.WriteLine(""); Console.ForegroundColor = previous; }
private static void InitializeGlobals() { ConsoleColor prior = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGray; // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing logging"); _Logging = new LoggingModule( _Settings.Logging.SyslogServerIp, _Settings.Logging.SyslogServerPort, _Settings.Logging.ConsoleLogging); if (_Settings.Logging.DiskLogging && !String.IsNullOrEmpty(_Settings.Logging.DiskDirectory)) { _Settings.Logging.DiskDirectory = _Settings.Logging.DiskDirectory.Replace("\\", "/"); if (!_Settings.Logging.DiskDirectory.EndsWith("/")) { _Settings.Logging.DiskDirectory += "/"; } if (!Directory.Exists(_Settings.Logging.DiskDirectory)) { Directory.CreateDirectory(_Settings.Logging.DiskDirectory); } _Logging.Settings.FileLogging = FileLoggingMode.FileWithDate; _Logging.Settings.LogFilename = _Settings.Logging.DiskDirectory + "less3.log"; } // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing database"); _ORM = new WatsonORM(_Settings.Database); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(Bucket)); _ORM.InitializeTable(typeof(BucketAcl)); _ORM.InitializeTable(typeof(BucketTag)); _ORM.InitializeTable(typeof(Credential)); _ORM.InitializeTable(typeof(Obj)); _ORM.InitializeTable(typeof(ObjectAcl)); _ORM.InitializeTable(typeof(ObjectTag)); _ORM.InitializeTable(typeof(User)); if (_Settings.Debug.DatabaseQueries) { _ORM.Debug.DatabaseQueries = true; } if (_Settings.Debug.DatabaseResults) { _ORM.Debug.DatabaseResults = true; } if (_Settings.Debug.DatabaseQueries || _Settings.Debug.DatabaseResults) { _ORM.Logger = Logger; } // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing configuration manager"); _Config = new ConfigManager(_Settings, _Logging, _ORM); // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing bucket manager"); _Buckets = new BucketManager(_Settings, _Logging, _Config, _ORM); // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing authentication manager"); _Auth = new AuthManager(_Settings, _Logging, _Config, _Buckets); // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing API handler"); _ApiHandler = new ApiHandler(_Settings, _Logging, _Config, _Buckets, _Auth); // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing admin API handler"); _AdminApiHandler = new AdminApiHandler(_Settings, _Logging, _Config, _Buckets, _Auth); // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing console manager"); _Console = new ConsoleManager(_Settings, _Logging); // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing S3 server interface"); _S3Server = new S3Server( _Settings.Server.DnsHostname, _Settings.Server.ListenerPort, _Settings.Server.Ssl, DefaultRequestHandler); if (_Settings.Server.Ssl) { Console.WriteLine("| https://" + _Settings.Server.DnsHostname + ":" + _Settings.Server.ListenerPort); } else { Console.WriteLine("| http://" + _Settings.Server.DnsHostname + ":" + _Settings.Server.ListenerPort); } // 0 1 2 3 4 5 // 123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("| Initializing S3 server APIs"); _S3Server.Logging.Exceptions = true; _S3Server.Logging.S3Requests = _Settings.Debug.S3Requests; _S3Server.Logger = Logger; if (!String.IsNullOrEmpty(_Settings.Server.BaseDomain)) { Console.WriteLine("| Base domain " + _Settings.Server.BaseDomain); Console.WriteLine(" | Requests must use virtual-hosted URLs, i.e. [bucket].[hostname]/[key]"); Console.WriteLine(" | Run as administrator/root and listen on a wildcard hostname, i.e. '*'"); _S3Server.BaseDomains.Add(_Settings.Server.BaseDomain); } else { Console.WriteLine("| No base domain specified"); Console.WriteLine(" | Requests must use path-style hosted URLs, i.e. [hostname]/[bucket]/[key]"); } _S3Server.PreRequestHandler = PreRequestHandler; _S3Server.PostRequestHandler = PostRequestHandler; _S3Server.Service.ListBuckets = _ApiHandler.ServiceListBuckets; _S3Server.Bucket.Delete = _ApiHandler.BucketDelete; _S3Server.Bucket.DeleteTags = _ApiHandler.BucketDeleteTags; _S3Server.Bucket.Exists = _ApiHandler.BucketExists; _S3Server.Bucket.Read = _ApiHandler.BucketRead; _S3Server.Bucket.ReadAcl = _ApiHandler.BucketReadAcl; _S3Server.Bucket.ReadLocation = _ApiHandler.BucketReadLocation; _S3Server.Bucket.ReadTags = _ApiHandler.BucketReadTags; _S3Server.Bucket.ReadVersions = _ApiHandler.BucketReadVersions; _S3Server.Bucket.ReadVersioning = _ApiHandler.BucketReadVersioning; _S3Server.Bucket.Write = _ApiHandler.BucketWrite; _S3Server.Bucket.WriteAcl = _ApiHandler.BucketWriteAcl; _S3Server.Bucket.WriteTags = _ApiHandler.BucketWriteTags; _S3Server.Bucket.WriteVersioning = _ApiHandler.BucketWriteVersioning; _S3Server.Object.Delete = _ApiHandler.ObjectDelete; _S3Server.Object.DeleteMultiple = _ApiHandler.ObjectDeleteMultiple; _S3Server.Object.DeleteTags = _ApiHandler.ObjectDeleteTags; _S3Server.Object.Exists = _ApiHandler.ObjectExists; _S3Server.Object.Read = _ApiHandler.ObjectRead; _S3Server.Object.ReadAcl = _ApiHandler.ObjectReadAcl; _S3Server.Object.ReadRange = _ApiHandler.ObjectReadRange; _S3Server.Object.ReadTags = _ApiHandler.ObjectReadTags; _S3Server.Object.Write = _ApiHandler.ObjectWrite; _S3Server.Object.WriteAcl = _ApiHandler.ObjectWriteAcl; _S3Server.Object.WriteTags = _ApiHandler.ObjectWriteTags; _S3Server.Start(); Console.ForegroundColor = prior; Console.WriteLine(""); }
private void RunSetup() { #region Variables DateTime timestamp = DateTime.UtcNow; Settings settings = new Settings(); #endregion #region Welcome Console.WriteLine( Environment.NewLine + Environment.NewLine + "oooo .o8 " + Environment.NewLine + "`888 888 " + Environment.NewLine + " 888 oooo .ooooo. ooo. .oo. .oo. .ooooo. .oooo888 .ooooo. " + Environment.NewLine + " 888 .8P' d88' `88b `888P'Y88bP'Y88b d88' `88b d88' `888 d88' `88b " + Environment.NewLine + " 888888. 888 888 888 888 888 888 888 888 888 888 888 " + Environment.NewLine + " 888 `88b. 888 888 888 888 888 888 888 888 888 888 888 " + Environment.NewLine + "o888o o888o `Y8bod8P' o888o o888o o888o `Y8bod8P' `Y8bod88P `Y8bod8P' " + Environment.NewLine + Environment.NewLine + Environment.NewLine); // ________________ 1 2 3 4 5 6 7 // ________________12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.WriteLine("Thank you for using Komodo! We'll put together a basic system configuration"); Console.WriteLine("so you can be up and running quickly."); Console.WriteLine(""); Console.WriteLine(Common.Line(79, "-")); Console.WriteLine(""); #endregion #region Initial-Settings settings.EnableConsole = true; settings.Server = new Settings.ServerSettings(); settings.Server.HeaderApiKey = "x-api-key"; settings.Server.AdminApiKey = "komodoadmin"; settings.Server.ListenerPort = 9090; settings.Server.ListenerHostname = "localhost"; settings.Logging = new Settings.LoggingSettings(); settings.Logging.ConsoleLogging = true; settings.Logging.Header = "komodo"; settings.Logging.SyslogServerIp = "127.0.0.1"; settings.Logging.SyslogServerPort = 514; settings.Logging.MinimumLevel = Severity.Info; settings.Logging.FileLogging = true; settings.Logging.FileDirectory = "./logs/"; settings.Logging.Filename = "Komodo.log"; if (!Directory.Exists("./data/")) { Directory.CreateDirectory("./data/"); } if (!Directory.Exists("./logs/")) { Directory.CreateDirectory("./logs/"); } settings.Database = new DbSettings("./data/komodo.db"); string tempDirectory = "./data/temp/"; settings.TempStorage = new StorageSettings(new DiskSettings(tempDirectory)); if (!Directory.Exists(tempDirectory)) { Directory.CreateDirectory(tempDirectory); } string sourceDirectory = "./data/source/"; settings.SourceDocuments = new StorageSettings(new DiskSettings(sourceDirectory)); if (!Directory.Exists(sourceDirectory)) { Directory.CreateDirectory(sourceDirectory); } string parsedDirectory = "./data/parsed/"; settings.ParsedDocuments = new StorageSettings(new DiskSettings(parsedDirectory)); if (!Directory.Exists(parsedDirectory)) { Directory.CreateDirectory(parsedDirectory); } #endregion #region Initialize-Database-and-Create-Records WatsonORM orm = new WatsonORM(settings.Database.ToDatabaseSettings()); orm.InitializeDatabase(); orm.InitializeTable(typeof(ApiKey)); orm.InitializeTable(typeof(Index)); orm.InitializeTable(typeof(Metadata)); orm.InitializeTable(typeof(MetadataDocument)); orm.InitializeTable(typeof(Node)); orm.InitializeTable(typeof(ParsedDocument)); orm.InitializeTable(typeof(Permission)); orm.InitializeTable(typeof(SourceDocument)); orm.InitializeTable(typeof(TermDoc)); orm.InitializeTable(typeof(TermGuid)); orm.InitializeTable(typeof(User)); DbExpression e = new DbExpression("id", DbOperators.GreaterThan, 0); User user = null; ApiKey apiKey = null; Permission perm = null; Index idx = null; List <User> users = orm.SelectMany <User>(e); if (users == null || users.Count < 1) { Console.WriteLine("| Creating first user 'default'"); user = new User("default", "Default", "*****@*****.**", "default"); user = orm.Insert <User>(user); } else { Console.WriteLine("| Users already exist, not creating default user"); } List <Index> indices = orm.SelectMany <Index>(e); if (indices == null || indices.Count < 1) { Console.WriteLine("| Creating first index 'default'"); idx = new Index(user.GUID, "default"); idx = orm.Insert <Index>(idx); } else { Console.WriteLine("| Indices already exist, not creating default index"); } List <ApiKey> keys = orm.SelectMany <ApiKey>(e); if (keys == null || keys.Count < 1) { Console.WriteLine("| Creating first API key 'default'"); apiKey = new ApiKey("default", user.GUID, true); apiKey = orm.Insert <ApiKey>(apiKey); } else { Console.WriteLine("| API keys already exist, not creating default API key"); } List <Permission> perms = orm.SelectMany <Permission>(e); if (perms == null || perms.Count < 1) { Console.WriteLine("| Creating first permission 'default'"); perm = new Permission(idx.GUID, user.GUID, apiKey.GUID, true, true, true, true, true); perm = orm.Insert <Permission>(perm); } else { Console.WriteLine("| Permissions already exist, not creating default permissions"); } #endregion #region Write-System-JSON File.WriteAllBytes("./system.json", Encoding.UTF8.GetBytes(Common.SerializeJson(settings, true))); #endregion #region Wrap-Up string baseUrl = "http://localhost:" + settings.Server.ListenerPort; // ________________ 1 2 3 4 5 6 7 // ________________12345678901234567890123456789012345678901234567890123456789012345678901234567890 Console.WriteLine(""); Console.WriteLine("All finished!"); Console.WriteLine(""); Console.WriteLine("If you ever want to return to this setup wizard, just re-run the application"); Console.WriteLine("from the terminal with the 'setup' argument."); Console.WriteLine(""); Console.WriteLine("Verify Komodo is running in your browser using the following URL:"); Console.WriteLine(""); Console.WriteLine(" " + baseUrl); Console.WriteLine(""); Console.WriteLine("We've created your first index for you called 'First'. Try POSTing a JSON"); Console.WriteLine("document to the index using the API key 'default' using the URL:"); Console.WriteLine(""); Console.WriteLine(" " + baseUrl + "/default?type=json&name=My+First+Document&x-api-key=default"); Console.WriteLine(""); #endregion }
/// <summary> /// Instantiate the object. /// </summary> /// <param name="dbSettings">Database settings.</param> /// <param name="sourceDocs">Storage settings for source documents.</param> /// <param name="parsedDocs">Storage settings for parsed documents.</param> public KomodoIndices(DbSettings dbSettings, StorageSettings sourceDocs, StorageSettings parsedDocs) { if (dbSettings == null) { throw new ArgumentNullException(nameof(dbSettings)); } if (sourceDocs == null) { throw new ArgumentNullException(nameof(sourceDocs)); } if (parsedDocs == null) { throw new ArgumentNullException(nameof(parsedDocs)); } _SourceDocsStorageSettings = sourceDocs; _ParsedDocsStorageSettings = parsedDocs; _Token = _TokenSource.Token; _DbSettings = dbSettings; _ORM = new WatsonORM(_DbSettings.ToDatabaseSettings()); _ORM.InitializeDatabase(); _ORM.InitializeTable(typeof(ApiKey)); _ORM.InitializeTable(typeof(Index)); _ORM.InitializeTable(typeof(Metadata)); _ORM.InitializeTable(typeof(MetadataDocument)); _ORM.InitializeTable(typeof(Node)); _ORM.InitializeTable(typeof(ParsedDocument)); _ORM.InitializeTable(typeof(Permission)); _ORM.InitializeTable(typeof(SourceDocument)); _ORM.InitializeTable(typeof(TermDoc)); _ORM.InitializeTable(typeof(TermGuid)); _ORM.InitializeTable(typeof(User)); Task.Run(() => MonitorTask(), _Token); }