using Microsoft.AspNetCore.Mvc; namespace RestySqlite { public class WebApi { public static void Main() { string? dbName = Utils.ReadConfig("database", "sql") ?? throw new NullReferenceException("Database name is null!"); if (dbName.Length == 0) { Console.WriteLine("You should specify database name in conf.toml!"); return; } if (!File.Exists($"{dbName}.db")) { Console.WriteLine($"Database {dbName}.db not found in folder!"); Utils.CreateDatabase(dbName); } string? handle = Utils.ReadConfig("handle", "api") ?? throw new NullReferenceException("API handle is null!"); int port = Utils.ReadConfig("port", "api"); RunServer(dbName, handle, port); } static public void RunServer(string dbName, string handle, int port) { var builder = WebApplication.CreateBuilder(); builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.Listen(System.Net.IPAddress.Any, port); // Listen on all network interfaces on port 5000 }); var app = builder.Build(); var repo = new DatabaseRepository(new DatabaseWrapper(dbName)); var group = app.MapGroup(handle); group.MapGet("/", async() => { return await repo.ListTables(); }); group.MapGet("/{tableName}", async(string tableName) => { return await repo.ListColumns(tableName); }); group.MapGet("/{tableName}/{columnName}", async(string tableName, string columnName) => { return await repo.ListColumnValues(tableName, columnName); } ); group.MapGet("/{tableName}/{id:int}", async(string tableName, int id) => { return await repo.FetchValue(tableName, id); }); group.MapPost("/sqlcommand", async(HttpContext context) => { using var reader = new StreamReader(context.Request.Body); string command = await reader.ReadToEndAsync(); return await repo.ProcessCommand(command); }); app.Run(); } } }