You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi/Program.cs

226 lines
8.4 KiB

using System;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Ombi.Store.Context;
using Ombi.Store.Entities;
using CommandLine;
using CommandLine.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
5 years ago
using Microsoft.Extensions.DependencyInjection;
using Ombi.Extensions;
using Ombi.Helpers;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Ombi
{
public static class Program
{
private static string UrlArgs { get; set; }
5 years ago
public static async Task Main(string[] args)
{
8 years ago
Console.Title = "Ombi";
var host = string.Empty;
var storagePath = string.Empty;
var baseUrl = string.Empty;
var demo = false;
var migrate = false;
var result = Parser.Default.ParseArguments<Options>(args)
.WithParsed(o =>
{
host = o.Host;
storagePath = o.StoragePath;
baseUrl = o.BaseUrl;
demo = o.Demo;
migrate = o.Migrate;
7 years ago
}).WithNotParsed(err =>
{
foreach (var e in err)
{
Console.WriteLine(e);
}
});
Console.WriteLine(HelpOutput(result));
UrlArgs = host;
var urlValue = string.Empty;
var instance = StartupSingleton.Instance;
var demoInstance = DemoSingleton.Instance;
demoInstance.Demo = demo;
instance.StoragePath = storagePath ?? string.Empty;
5 years ago
var services = new ServiceCollection();
services.ConfigureDatabases(null);
5 years ago
using (var provider = services.BuildServiceProvider())
{
5 years ago
var settingsDb = provider.GetRequiredService<SettingsContext>();
if (migrate)
{
Console.WriteLine("Migrate in progress...");
var migrationTasks = new List<Task>();
var externalDb = provider.GetRequiredService<ExternalContext>();
var ombiDb = provider.GetRequiredService<OmbiContext>();
migrationTasks.Add(settingsDb.Database.MigrateAsync());
migrationTasks.Add(ombiDb.Database.MigrateAsync());
migrationTasks.Add(externalDb.Database.MigrateAsync());
Task.WaitAll(migrationTasks.ToArray());
Console.WriteLine("Migrate complete.");
Environment.Exit(0);
}
var config = await settingsDb.ApplicationConfigurations.ToListAsync();
5 years ago
var url = config.FirstOrDefault(x => x.Type == ConfigurationTypes.Url);
var dbBaseUrl = config.FirstOrDefault(x => x.Type == ConfigurationTypes.BaseUrl);
var securityToken = config.FirstOrDefault(x => x.Type == ConfigurationTypes.SecurityToken);
await CheckSecurityToken(securityToken, settingsDb, instance);
5 years ago
if (url == null)
{
5 years ago
url = new ApplicationConfiguration
{
5 years ago
Type = ConfigurationTypes.Url,
Value = "http://*:5000"
};
using (var tran = await settingsDb.Database.BeginTransactionAsync())
{
5 years ago
settingsDb.ApplicationConfigurations.Add(url);
await settingsDb.SaveChangesAsync();
await tran.CommitAsync();
}
5 years ago
urlValue = url.Value;
}
5 years ago
if (!url.Value.Equals(host))
{
5 years ago
url.Value = UrlArgs;
using (var tran = await settingsDb.Database.BeginTransactionAsync())
{
await settingsDb.SaveChangesAsync();
await tran.CommitAsync();
}
5 years ago
urlValue = url.Value;
}
else if (string.IsNullOrEmpty(urlValue))
{
urlValue = host;
}
5 years ago
if (dbBaseUrl == null)
{
5 years ago
if (baseUrl.HasValue() && baseUrl.StartsWith("/"))
{
5 years ago
dbBaseUrl = new ApplicationConfiguration
{
Type = ConfigurationTypes.BaseUrl,
Value = baseUrl
};
using (var tran = await settingsDb.Database.BeginTransactionAsync())
5 years ago
{
settingsDb.ApplicationConfigurations.Add(dbBaseUrl);
await settingsDb.SaveChangesAsync();
await tran.CommitAsync();
5 years ago
}
}
}
5 years ago
else if (baseUrl.HasValue() && !baseUrl.Equals(dbBaseUrl.Value))
{
dbBaseUrl.Value = baseUrl;
using (var tran = await settingsDb.Database.BeginTransactionAsync())
{
await settingsDb.SaveChangesAsync();
await tran.CommitAsync();
}
}
5 years ago
Console.WriteLine($"We are running on {urlValue}");
5 years ago
CreateHostBuilder(args).Build().Run();
}
}
private static async Task CheckSecurityToken(ApplicationConfiguration securityToken, SettingsContext ctx, StartupSingleton instance)
{
if (securityToken == null || string.IsNullOrEmpty(securityToken.Value))
{
securityToken = new ApplicationConfiguration
{
Type = ConfigurationTypes.SecurityToken,
Value = Guid.NewGuid().ToString("N")
};
using (var tran = await ctx.Database.BeginTransactionAsync())
{
ctx.ApplicationConfigurations.Add(securityToken);
await ctx.SaveChangesAsync();
await tran.CommitAsync();
}
}
instance.SecurityKey = securityToken.Value;
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(serverOptions =>
{
// Set properties and call methods on options
});
webBuilder.PreferHostingUrls(true)
.UseUrls(UrlArgs)
.UseStartup<Startup>();
});
private static string HelpOutput(ParserResult<Options> args)
{
var result = new StringBuilder();
7 years ago
result.AppendLine("Hello, welcome to Ombi");
result.AppendLine("Valid options are:");
result.AppendLine(HelpText.AutoBuild(args, null, null));
return result.ToString();
}
}
public class Options
{
[Option("host", Required = false, HelpText =
"Set to a semicolon-separated (;) list of URL prefixes to which the server should respond. For example, http://localhost:123." +
" Use \"localhost\" to indicate that the server should listen for requests on any IP address or hostname using the specified port and protocol (for example, http://localhost:5000). " +
"The protocol (http:// or https://) must be included with each URL. Supported formats vary between servers.", Default = "http://*:5000")]
public string Host { get; set; }
[Option("storage", Required = false, HelpText = "Storage path, where we save the logs and database")]
public string StoragePath { get; set; }
[Option("baseurl", Required = false, HelpText = "The base URL for reverse proxy scenarios")]
public string BaseUrl { get; set; }
[Option("demo", Required = false, HelpText = "Demo mode, you will never need to use this, fuck that fruit company...")]
public bool Demo { get; set; }
[Option("migrate", Required = false, HelpText = "Will run the migrations then exit the application")]
public bool Migrate { get; set; }
}
}