Program.cs
1 using ArgsUniform; 2 using System.Reflection; 3 4 namespace MarketInsights 5 { 6 public class Program 7 { 8 public static void Main(string[] args) 9 { 10 var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args); 11 var config = uniformArgs.Parse(true); 12 var cts = new CancellationTokenSource(); 13 var appState = new AppState(config); 14 15 Console.CancelKeyPress += (s, e) => 16 { 17 appState.Log.Log("Stopping..."); 18 cts.Cancel(); 19 e.Cancel = true; 20 }; 21 22 var connector = GethConnector.GethConnector.Initialize(appState.Log); 23 if (connector == null) throw new Exception("Invalid Geth information"); 24 25 var updater = new Updater(appState, connector.GethNode, connector.CodexContracts, cts.Token); 26 27 var builder = WebApplication.CreateBuilder(args); 28 29 var listenPort = Environment.GetEnvironmentVariable("APIPORT"); 30 if (string.IsNullOrEmpty(listenPort)) listenPort = "31090"; 31 32 builder.WebHost.ConfigureKestrel((context, options) => 33 { 34 options.ListenAnyIP(Convert.ToInt32(listenPort)); 35 }); 36 37 builder.Services.AddSingleton(appState); 38 39 builder.Services.AddControllers(); 40 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle 41 builder.Services.AddEndpointsApiExplorer(); 42 builder.Services.AddSwaggerGen(s => 43 { 44 var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; 45 s.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); 46 }); 47 48 var app = builder.Build(); 49 50 // Configure the HTTP request pipeline. 51 if (app.Environment.IsDevelopment()) 52 { 53 app.UseSwagger(); 54 app.UseSwaggerUI(); 55 } 56 57 app.UseHttpsRedirection(); 58 59 app.UseAuthorization(); 60 61 app.MapControllers(); 62 63 Console.WriteLine("MarketInsights listening on port " + listenPort); 64 65 updater.Run(); 66 app.Run(); 67 } 68 69 private static void PrintHelp() 70 { 71 Console.WriteLine("WebAPI for generating market overview for Codex network. Comes with OpenAPI swagger endpoint."); 72 73 var nl = Environment.NewLine; 74 Console.WriteLine($"Required environment variables: {nl}" + 75 $"'GETH_HOST'{nl}", 76 $"'GETH_HTTP_PORT'{nl}", 77 $"'CODEXCONTRACTS_MARKETPLACEADDRESS'{nl}", 78 $"'CODEXCONTRACTS_TOKENADDRESS'{nl}", 79 $"'CODEXCONTRACTS_ABI'{nl}"); 80 } 81 } 82 }