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

193 lines
6.6 KiB

using System;
using System.IO;
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.AspNetCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
5 years ago
using Microsoft.Extensions.DependencyInjection;
using Ombi.Extensions;
using Ombi.Helpers;
5 years ago
using Ombi.Store.Context.MySql;
using Ombi.Store.Context.Sqlite;
namespace Ombi
{
public class Program
{
private static string UrlArgs { get; set; }
5 years ago
public static void 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 result = Parser.Default.ParseArguments<Options>(args)
.WithParsed(o =>
{
host = o.Host;
storagePath = o.StoragePath;
baseUrl = o.BaseUrl;
demo = o.Demo;
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 = StoragePathSingleton.Instance;
var demoInstance = DemoSingleton.Instance;
demoInstance.Demo = demo;
instance.StoragePath = storagePath ?? string.Empty;
// Check if we need to migrate the settings
DeleteSchedules();
//CheckAndMigrate();
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>();
5 years ago
var config = settingsDb.ApplicationConfigurations.ToList();
var url = config.FirstOrDefault(x => x.Type == ConfigurationTypes.Url);
var dbBaseUrl = config.FirstOrDefault(x => x.Type == ConfigurationTypes.BaseUrl);
if (url == null)
{
5 years ago
url = new ApplicationConfiguration
{
5 years ago
Type = ConfigurationTypes.Url,
Value = "http://*:5000"
};
5 years ago
using (var tran = settingsDb.Database.BeginTransaction())
{
5 years ago
settingsDb.ApplicationConfigurations.Add(url);
settingsDb.SaveChanges();
tran.Commit();
}
5 years ago
urlValue = url.Value;
}
5 years ago
if (!url.Value.Equals(host))
{
5 years ago
url.Value = UrlArgs;
5 years ago
using (var tran = settingsDb.Database.BeginTransaction())
{
5 years ago
settingsDb.SaveChanges();
tran.Commit();
}
5 years ago
urlValue = url.Value;
}
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
};
5 years ago
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.ApplicationConfigurations.Add(dbBaseUrl);
settingsDb.SaveChanges();
tran.Commit();
}
}
}
5 years ago
else if (baseUrl.HasValue() && !baseUrl.Equals(dbBaseUrl.Value))
{
dbBaseUrl.Value = baseUrl;
5 years ago
using (var tran = settingsDb.Database.BeginTransaction())
{
5 years ago
settingsDb.SaveChanges();
tran.Commit();
}
}
5 years ago
Console.WriteLine($"We are running on {urlValue}");
5 years ago
CreateHostBuilder(args).Build().Run();
}
}
private static void DeleteSchedules()
{
try
{
if (File.Exists("Schedules.db"))
{
File.Delete("Schedules.db");
}
}
catch (Exception)
{
}
}
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 \"*\" to indicate that the server should listen for requests on any IP address or hostname using the specified port and protocol (for example, http://*: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; }
}
}