yavsc/cli/Program.cs

131 lines
4.4 KiB
C#

7 years ago
using System;
7 years ago
using Microsoft.Extensions.Configuration;
7 years ago
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
7 years ago
using Microsoft.Extensions.OptionsModel;
7 years ago
7 years ago
using System.Globalization;
using System.Reflection;
// using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
// using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Localization;
7 years ago
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.AspNet.Mvc.Razor;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNet.Razor;
using Microsoft.Extensions.DependencyInjection.Abstractions;
using Microsoft.Extensions.PlatformAbstractions;
using cli.Services;
6 years ago
using Yavsc;
using Yavsc.Models;
using Yavsc.Server.Helpers;
using Yavsc.Services;
using Yavsc.Templates;
7 years ago
namespace cli
7 years ago
{
7 years ago
public class Program
7 years ago
{
7 years ago
public static void Main(string[] args)
{
6 years ago
7 years ago
var host = new WebHostBuilder();
6 years ago
var hostengnine = host
.UseEnvironment("Development")
.UseServer("cli")
7 years ago
.UseStartup<Startup>()
7 years ago
.UseServices(services => {
Console.WriteLine($"> Using {services.Count} services:");
foreach (var s in services) Console.WriteLine($"> * {s.ServiceType}");
})
7 years ago
.Build();
var app = hostengnine.Start();
6 years ago
var mailer = app.Services.GetService<EMailer>();
6 years ago
mailer.AllUserGen(2, nameof(UserOrientedTemplate));
7 years ago
}
7 years ago
}
7 years ago
}
7 years ago
7 years ago
namespace cli
{
public class Startup
7 years ago
{
7 years ago
public string ConnectionString
7 years ago
{
7 years ago
get { return DbHelpers.ConnectionString; }
private set { DbHelpers.ConnectionString = value; }
}
7 years ago
public static SiteSettings SiteSetup { get; private set; }
public static SmtpSettings SmtpSettup { get; private set; }
public static IConfiguration Configuration { get; set; }
7 years ago
7 years ago
public static string HostingFullName { get; private set; }
7 years ago
ILogger logger;
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var devtag = env.IsDevelopment()?"D":"";
var prodtag = env.IsProduction()?"P":"";
var stagetag = env.IsStaging()?"S":"";
7 years ago
7 years ago
HostingFullName = $"{appEnv.RuntimeFramework.FullName} [{env.EnvironmentName}:{prodtag}{devtag}{stagetag}]";
// Set up configuration sources.
var builder = new ConfigurationBuilder()
6 years ago
.AddEnvironmentVariables()
7 years ago
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Configuration = builder.Build();
7 years ago
7 years ago
}
6 years ago
7 years ago
public void ConfigureServices (IServiceCollection services)
{
services.AddOptions();
var siteSettingsconf = Configuration.GetSection("Site");
services.Configure<SiteSettings>(siteSettingsconf);
var smtpSettingsconf = Configuration.GetSection("Smtp");
services.Configure<SmtpSettings>(smtpSettingsconf);
services.AddInstance(typeof(ILoggerFactory), new LoggerFactory());
services.AddTransient(typeof(IEmailSender), typeof(MessageSender));
services.AddTransient(typeof(RazorEngineHost), typeof(YaRazorEngineHost));
7 years ago
7 years ago
7 years ago
services.AddEntityFramework().AddNpgsql().AddDbContext<ApplicationDbContext>();
7 years ago
7 years ago
services.AddTransient((s) => new RazorTemplateEngine(s.GetService<RazorEngineHost>()));
var serviceProvider = services.BuildServiceProvider();
7 years ago
7 years ago
services.AddLogging();
services.AddTransient<EMailer>();
7 years ago
services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
7 years ago
}
7 years ago
public void Configure (IApplicationBuilder app, IHostingEnvironment env,
IOptions<SiteSettings> siteSettings, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
logger = loggerFactory.CreateLogger<Startup>();
6 years ago
logger.LogInformation(env.EnvironmentName);
var cxstr = Configuration["Data:DefaultConnection:ConnectionString"];
DbHelpers.ConnectionString = cxstr;
7 years ago
}
7 years ago
7 years ago
}
7 years ago
}