-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMoYuCodeApp.cs
More file actions
179 lines (149 loc) · 5.8 KB
/
MoYuCodeApp.cs
File metadata and controls
179 lines (149 loc) · 5.8 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.FileProviders;
using MoYuCode.Api;
using MoYuCode.Data;
using MoYuCode.Hubs;
using MoYuCode.Services.A2a;
using MoYuCode.Services.Codex;
using MoYuCode.Services.Jobs;
using MoYuCode.Services.Sessions;
using MoYuCode.Services.Shell;
using Serilog;
using Serilog.Events;
namespace MoYuCode;
public static class MoYuCodeApp
{
public const string DefaultUrl = "http://0.0.0.0:9110";
public static WebApplication Create(string[]? args, out bool usingDefaultUrl)
{
args ??= Array.Empty<string>();
var builder = WebApplication.CreateBuilder(args);
var logDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"myyucode",
"logs");
Directory.CreateDirectory(logDirectory);
var errorLogPath = Path.Combine(logDirectory, "myyucode-error-.log");
builder.Host.UseSerilog((context, _, loggerConfiguration) =>
{
loggerConfiguration
.ReadFrom.Configuration(context.Configuration)
.WriteTo.File(
errorLogPath,
restrictedToMinimumLevel: LogEventLevel.Error,
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 14);
});
var hasUrls = !string.IsNullOrWhiteSpace(builder.Configuration["urls"])
|| !string.IsNullOrWhiteSpace(builder.Configuration["ASPNETCORE_URLS"]);
var hasKestrelEndpoints = builder.Configuration.GetSection("Kestrel:Endpoints").GetChildren().Any();
usingDefaultUrl = !hasUrls && !hasKestrelEndpoints;
if (usingDefaultUrl)
{
builder.WebHost.UseUrls(DefaultUrl);
}
builder.Services.AddOpenApi();
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
policy.SetIsOriginAllowed(_ => true)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
// Add SignalR
builder.Services.AddSignalR();
var appDataRoot = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".myyucode");
Directory.CreateDirectory(appDataRoot);
builder.Services.AddSingleton<JsonDataStore>(sp => new JsonDataStore(appDataRoot));
builder.Services.AddHttpClient();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<JobManager>();
builder.Services.AddSingleton<PowerShellLauncher>();
builder.Services.AddSingleton<TerminalWindowLauncher>();
builder.Services.AddSingleton<TerminalMuxSessionManager>();
builder.Services.AddSingleton<A2aTaskManager>();
builder.Services.AddSingleton<CodexAppServerClient>();
builder.Services.AddSingleton<CodexSessionManager>();
// Session management services
builder.Services.AddSingleton<SessionManager>();
builder.Services.AddSingleton<SessionMessageRepository>();
builder.Services.AddWebSockets((o) =>
{
});
var app = builder.Build();
try
{
var embeddedWebRoot = new ManifestEmbeddedFileProvider(typeof(Program).Assembly, "wwwroot");
app.UseDefaultFiles(new DefaultFilesOptions
{
FileProvider = embeddedWebRoot,
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = embeddedWebRoot,
});
app.MapFallback(async context =>
{
if (context.Request.Path.StartsWithSegments("/api")
|| context.Request.Path.StartsWithSegments("/media")
|| context.Request.Path.StartsWithSegments("/terminal")
|| context.Request.Path.StartsWithSegments("/a2a")
|| context.Request.Path.StartsWithSegments("/.well-known"))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
var requestPath = context.Request.Path.Value ?? string.Empty;
if (Path.HasExtension(requestPath))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
var indexFile = embeddedWebRoot.GetFileInfo("index.html");
if (!indexFile.Exists)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
context.Response.ContentType = "text/html; charset=utf-8";
await using var stream = indexFile.CreateReadStream();
await stream.CopyToAsync(context.Response.Body);
});
}
catch (Exception)
{
}
finally
{
}
app.UseCors();
app.UseSerilogRequestLogging();
app.UseWebSockets();
// Map all endpoints with /api prefix
app.MapMyYuCodeApis();
app.MapA2a();
app.MapMedia();
app.MapTerminal();
app.MapInfo();
app.MapSessionsEndpoints();
// Map SignalR hub
app.MapHub<ChatHub>("/api/chat");
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
if (usingDefaultUrl)
{
app.Logger.LogInformation("Default URL binding active: {Url}", DefaultUrl);
}
return app;
}
}