From 63151ad72484c8365250338d9c99b3cd9d7fcd17 Mon Sep 17 00:00:00 2001 From: Max Nullov Date: Sun, 9 Feb 2025 16:47:08 +0300 Subject: [PATCH] Runtime checks and clearing --- RestySqlite.http | 6 ------ Utils.cs | 29 ++++++++++++++++++++++++++++- WebApi.cs | 6 ++++++ conf.toml | 6 +++--- test.db | Bin 8192 -> 0 bytes 5 files changed, 37 insertions(+), 10 deletions(-) delete mode 100644 RestySqlite.http delete mode 100644 test.db diff --git a/RestySqlite.http b/RestySqlite.http deleted file mode 100644 index 7ba43b6..0000000 --- a/RestySqlite.http +++ /dev/null @@ -1,6 +0,0 @@ -@RestySqlite_HostAddress = http://localhost:5063 - -GET {{RestySqlite_HostAddress}}/weatherforecast/ -Accept: application/json - -### diff --git a/Utils.cs b/Utils.cs index 30985f5..0d4bbd5 100644 --- a/Utils.cs +++ b/Utils.cs @@ -4,8 +4,35 @@ namespace RestySqlite { public class Utils { - public static dynamic ReadConfig(string key, string? category = null) + public static dynamic? ReadConfig(string key, string? category = null) { + if (!File.Exists("conf.toml")) + { + TomlTable toml = new TomlTable + { + ["title"] = "Configuration file for API", + + ["api"] = + { + ["port"] = 5417, + ["handle"] = "api", + }, + ["sql"] = + { + ["database"] = "" + } + }; + + using(StreamWriter writer = File.CreateText("conf.toml")) + { + toml.WriteTo(writer); + // Remember to flush the data if needed! + writer.Flush(); + } + + Console.WriteLine("Config created please check it."); + return null; + } using(StreamReader reader = File.OpenText("conf.toml")) { TomlTable table = TOML.Parse(reader); diff --git a/WebApi.cs b/WebApi.cs index f439d07..e5aa89e 100644 --- a/WebApi.cs +++ b/WebApi.cs @@ -17,6 +17,12 @@ namespace RestySqlite return; } + if (!File.Exists($"{dbName}.db")) + { + Console.WriteLine($"Database {dbName}.db not found in folder!"); + return; + } + string handle = Utils.ReadConfig("handle", "api"); int port = Utils.ReadConfig("port", "api"); diff --git a/conf.toml b/conf.toml index fe52cca..0520ad1 100644 --- a/conf.toml +++ b/conf.toml @@ -1,8 +1,8 @@ title = "Configuration file for API" [api] - port=5417 - handle="api" +port = 5417 +handle = "api" [sql] - database="test" \ No newline at end of file +database = "test" diff --git a/test.db b/test.db deleted file mode 100644 index 14ac18bdd3e86b1c9328b53dd95a7cbe423375a7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8192 zcmeI#%}T>S5C`zx8p%mkPdSvxhzFIFUVMR8U9`0=8gnp+huCi0Kt8ld{JMGQo9LVP zCccQ9KreyP7x)jE$?i-7%kQ>#w}UWOG))(AkkcvKWt_7P5o65M+0eNPt{28?qu=Fz zQ<$v%`Ld(G;^K$t2LS;HKmY;|fB*y_009U<00Izzz&Zh+o0etquf05Yid2!QMOKx@ zxhuP#B(HlmkW{tkIGj*#ccutP@{GnFX1DrWQA+-{V=%P3L_^@F#pHg0l* O*-k&5Cv=gj+VCHd7+605