-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathProgram.cs
77 lines (64 loc) · 2.17 KB
/
Program.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
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using VueCliMiddleware;
namespace AspNetCoreVueStarter
{
public class Program
{
public static void Main(string[] args)
{
if (!CommandLine.Arguments.TryGetOptions(args, true, out string mode, out ushort port, out bool https)) return;
if (mode == "kill") {
Console.WriteLine($"Killing process serving port {port}...");
PidUtils.KillPort(port, true, true);
return;
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
if (https) app.UseHttpsRedirection();
app.UseStaticFiles();
if (!app.Environment.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (app.Environment.IsDevelopment())
{
if (mode == "start")
{
spa.UseVueCli(npmScript: "serve", port: port, forceKill: true, https: https);
}
if (mode == "attach")
{
spa.UseProxyToSpaDevelopmentServer($"{(https ? "https" : "http")}://localhost:{port}");
}
}
});
app.Run();
}
}
}