-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
183 lines (165 loc) · 4.94 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
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
180
181
182
183
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using TradeStation.Interfaces;
using TradeStation.Configuration;
using TradeStation.Configuration.ValidationRules;
using TradeStation.Handlers;
using TradeStation.Models.Common;
using TradeStation.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMemoryCache();
// Configuration
builder.Services.Configure<TradeStationOptions>(
builder.Configuration.GetSection(TradeStationOptions.ConfigurationSection));
// Add validator as singleton
builder.Services.AddSingleton<IValidator<TradeStationOptions>, TradeStationOptionsValidation>();
// Configure options validation at startup
builder.Services.PostConfigure<TradeStationOptions>(options =>
{
var validator = builder.Services.BuildServiceProvider()
.GetRequiredService<IValidator<TradeStationOptions>>();
var result = validator.Validate(options);
if (!result.IsValid)
{
throw new OptionsValidationException(
nameof(TradeStationOptions),
typeof(TradeStationOptions),
result.Errors.Select(x => x.ErrorMessage));
}
});
// Add HttpClient with base configuration
builder.Services.AddHttpClient<ITradeStationClient, TradeStationClient>((serviceProvider, client) =>
{
var options = serviceProvider.GetRequiredService<IOptions<TradeStationOptions>>().Value;
client.BaseAddress = new Uri(options.BaseUrl);
client.DefaultRequestHeaders.Add("Accept", "application/json");
});
// Register services
builder.Services.AddScoped<ITradeStationClient, TradeStationClient>();
builder.Services.AddScoped<IResourceHandler, ResourceHandler>();
builder.Services.AddScoped<IToolHandler, ToolHandler>();
builder.Services.AddScoped<TradeStationServer>();
// Add CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("AllowAll");
// API Endpoints
app.MapGet("/api/resources", async (
TradeStationServer server,
CancellationToken cancellationToken) =>
{
try
{
var resources = server.ListResources();
return Results.Ok(resources);
}
catch (Exception ex)
{
return Results.Problem(
title: "Error retrieving resources",
detail: ex.Message,
statusCode: 500);
}
})
.WithName("GetResources")
.Produces<IEnumerable<Resource>>(200)
.ProducesProblem(500);
app.MapGet("/api/tools", async (
TradeStationServer server,
CancellationToken cancellationToken) =>
{
try
{
var tools = server.ListTools();
return Results.Ok(tools);
}
catch (Exception ex)
{
return Results.Problem(
title: "Error retrieving tools",
detail: ex.Message,
statusCode: 500);
}
})
.WithName("GetTools")
.Produces<IEnumerable<Tool>>(200)
.ProducesProblem(500);
app.MapPost("/api/resource", async (
TradeStationServer server,
Uri uri,
CancellationToken cancellationToken) =>
{
try
{
var result = await server.HandleResourceCallAsync(uri, cancellationToken);
return Results.Ok(result);
}
catch (ArgumentException ex)
{
return Results.BadRequest(new { error = ex.Message });
}
catch (Exception ex)
{
return Results.Problem(
title: "Error handling resource call",
detail: ex.Message,
statusCode: 500);
}
})
.WithName("CallResource")
.Produces<string>(200)
.ProducesProblem(500)
.ProducesValidationProblem(400);
app.MapPost("/api/tool", async (
TradeStationServer server,
[FromBody] ToolRequest request,
CancellationToken cancellationToken) =>
{
try
{
var result = await server.HandleToolCallAsync(
request.Name,
request.Arguments,
cancellationToken);
return Results.Ok(result);
}
catch (ArgumentException ex)
{
return Results.BadRequest(new { error = ex.Message });
}
catch (Exception ex)
{
return Results.Problem(
title: "Error handling tool call",
detail: ex.Message,
statusCode: 500);
}
})
.WithName("CallTool")
.Produces<IEnumerable<ToolResponse>>(200)
.ProducesProblem(500)
.ProducesValidationProblem(400);
// Add health check endpoint
app.MapGet("/health", () => Results.Ok(new { Status = "Healthy" }))
.WithName("HealthCheck")
.Produces<object>(200);
app.Run();