This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStartup.cs
164 lines (140 loc) · 6.77 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using RevitToIfcScheduler.Context;
using RevitToIfcScheduler.Models;
using Hangfire;
using Hangfire.Dashboard;
using Hangfire.SqlServer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using RevitToIfcScheduler.Utilities;
using Newtonsoft.Json.Serialization;
using System.IO;
using System.Reflection;
namespace RevitToIfcScheduler
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
AppConfig.Services = services;
AppConfig.ClientId = Configuration.GetValue<string>("ClientId");
AppConfig.ClientSecret = Configuration.GetValue<string>("ClientSecret");
AppConfig.LogPath = Configuration.GetValue<string>("LogPath") ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
AppConfig.FilePath = Configuration.GetValue<string>("FilePath", "/Files");
AppConfig.AppId = Configuration.GetValue<string>("AppId", "revit-to-ifc");
AppConfig.SendGridApiKey = Configuration.GetValue<string>("SendGridApiKey");
AppConfig.FromEmail = Configuration.GetValue<string>("FromEmail");
AppConfig.ToEmail = Configuration.GetValue<string>("ToEmail");
AppConfig.TwoLegScope = Configuration.GetValue<string>("TwoLegScope", "account:read data:read data:create data:write bucket:read bucket:create");
AppConfig.ThreeLegScope = Configuration.GetValue<string>("ThreeLegScope", "user:read data:read data:create");
AppConfig.IncludeShallowCopies = Configuration.GetValue<bool>("IncludeShallowCopies", true);
AppConfig.ForgeBaseUrl = Configuration.GetValue<string>("ForgeBaseUrl", "https://developer.api.autodesk.com");
AppConfig.SqlDB = Configuration.GetConnectionString("SqlDB");
AppConfig.DataProtector = DataProtectionProvider.Create("RevitToIfc").CreateProtector("User");
AppConfig.BucketKey = Configuration.GetValue<string>("BucketKey", $"{AppConfig.AppId}-{AppConfig.ClientId}".ToLower()).Substring(0, 35);
AppConfig.AdminEmails = new List<string>();
var adminEmails = Configuration.GetValue<string>("AdminEmails", "").Split(';').ToList();
foreach (var email in adminEmails)
{
AppConfig.AdminEmails.Add(email.ToLower());
}
services.AddHangfire(config => config
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(AppConfig.SqlDB, new SqlServerStorageOptions
{
SlidingInvisibilityTimeout = TimeSpan.FromHours(2),
}));
services.AddHangfireServer();
services.AddDbContext<RevitIfcContext>(options =>
options.UseSqlServer(
AppConfig.SqlDB,
b => b.MigrationsAssembly(typeof(RevitIfcContext).Assembly.GetName().ToString())));
var dbContext = services.BuildServiceProvider().GetService<RevitIfcContext>();
dbContext.Database.Migrate();
dbContext.Dispose();
//Add service for accessing current HttpContext
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddControllers();
services.AddMvcCore().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
Log.Information("Started Server");
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHangfireServer(new BackgroundJobServerOptions()
{
Queues = new[]{"default"},
WorkerCount = 5
}
);
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
{
Authorization = new []{ new MyAuthorizationFilter() },
AppPath = "/settings"
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
BackgroundJob.Enqueue(()=>Forge.CheckOrCreateTransientBucket(AppConfig.BucketKey));
}
}
public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
ServiceProvider provider = (AppConfig.Services as ServiceCollection).BuildServiceProvider();
Context.RevitIfcContext dbRevitIfcContext = provider.GetService<Context.RevitIfcContext>();
return Authentication.IsAuthorized(context.GetHttpContext(), dbRevitIfcContext);
}
}
}