|
22 | 22 | }); |
23 | 23 | } |
24 | 24 |
|
25 | | -// Add authentication for production |
| 25 | +// Add authentication and authorization only in Production |
26 | 26 | if (builder.Environment.IsProduction()) |
27 | 27 | { |
28 | 28 | // Configure authentication to use HTTPS URLs |
|
39 | 39 | } |
40 | 40 | }; |
41 | 41 | }); |
42 | | - |
| 42 | + |
43 | 43 | builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) |
44 | 44 | .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")); |
45 | | - |
| 45 | + |
46 | 46 | builder.Services.AddAuthorization(options => |
47 | 47 | { |
48 | 48 | // Require authentication by default |
49 | 49 | options.FallbackPolicy = options.DefaultPolicy; |
50 | | - |
| 50 | + |
51 | 51 | // Add role-based authorization |
52 | 52 | options.AddPolicy("PlatformAdmin", policy => |
53 | 53 | policy.RequireRole("platform.admin")); |
54 | | - |
| 54 | + |
55 | 55 | options.AddPolicy("Authenticated", policy => |
56 | 56 | policy.RequireAuthenticatedUser()); |
57 | 57 | }); |
58 | | - |
| 58 | + |
59 | 59 | builder.Services.AddControllersWithViews(options => |
60 | 60 | { |
61 | 61 | var policy = new AuthorizationPolicyBuilder() |
62 | 62 | .RequireAuthenticatedUser() |
63 | 63 | .Build(); |
64 | 64 | options.Filters.Add(new AuthorizeFilter(policy)); |
65 | 65 | }).AddDapr(); |
66 | | - |
| 66 | + |
67 | 67 | builder.Services.AddRazorPages() |
68 | 68 | .AddMicrosoftIdentityUI(); |
69 | 69 | } |
70 | 70 | else |
71 | 71 | { |
| 72 | + // Development: No authentication/authorization |
72 | 73 | builder.Services.AddRazorPages(); |
73 | 74 | builder.Services.AddControllers().AddDapr(); |
74 | 75 | } |
|
88 | 89 | builder.Services.AddApplicationInsightsTelemetry(); |
89 | 90 | } |
90 | 91 |
|
91 | | -// Configure GraphQL client |
92 | | -builder.Services.AddHttpClient("GraphQL", (sp, client) => |
93 | | -{ |
94 | | - var cfg = sp.GetRequiredService<IConfiguration>(); |
95 | | - var baseUrl = cfg["DAB_GRAPHQL_URL"] ?? ""; |
96 | | - if (!string.IsNullOrWhiteSpace(baseUrl)) |
97 | | - { |
98 | | - client.BaseAddress = new Uri(baseUrl); |
99 | | - } |
100 | | -}); |
| 92 | +// Configure HotChocolate GraphQL server |
| 93 | +builder.Services.AddGraphQLServer() |
| 94 | + .AddQueryType<Stamps.ManagementPortal.GraphQL.Query>() |
| 95 | + .AddSubscriptionType<Stamps.ManagementPortal.GraphQL.Subscription>(); |
101 | 96 |
|
102 | | -// Configure data service with Dapr capabilities |
103 | | -var dabGraphQLUrl = builder.Configuration["DAB_GRAPHQL_URL"]; |
104 | | -var useGraphQL = !string.IsNullOrWhiteSpace(dabGraphQLUrl); |
105 | | -var useDapr = !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("DAPR_HTTP_PORT")); |
| 97 | +// Add HotChocolate in-memory subscription support |
| 98 | +builder.Services.AddInMemorySubscriptions(); |
106 | 99 |
|
107 | | -Console.WriteLine($"Service Configuration - UseGraphQL: {useGraphQL}, UseDapr: {useDapr}, DAB_URL: '{dabGraphQLUrl}'"); |
108 | | - |
109 | | -if (useGraphQL && useDapr) |
110 | | -{ |
111 | | - // Use Dapr-enabled data service for enhanced debugging and resilience |
112 | | - builder.Services.AddScoped<Stamps.ManagementPortal.Services.GraphQLDataService>(); |
113 | | - builder.Services.AddScoped<Stamps.ManagementPortal.Services.IDataService, Stamps.ManagementPortal.Services.DaprDataService>(); |
114 | | -} |
115 | | -else if (useGraphQL) |
116 | | -{ |
117 | | - // Use direct GraphQL service |
118 | | - builder.Services.AddScoped<Stamps.ManagementPortal.Services.IDataService, Stamps.ManagementPortal.Services.GraphQLDataService>(); |
119 | | -} |
120 | | -else |
121 | | -{ |
122 | | - // Use in-memory service for development |
123 | | - Console.WriteLine("Using InMemoryDataService for development"); |
124 | | - builder.Services.AddScoped<Stamps.ManagementPortal.Services.IDataService, Stamps.ManagementPortal.Services.InMemoryDataService>(); |
125 | | -} |
| 100 | +// Register TaskEventPublisher |
| 101 | +builder.Services.AddSingleton<Stamps.ManagementPortal.Services.ITaskEventPublisher, Stamps.ManagementPortal.Services.TaskEventPublisher>(); |
| 102 | +// Use in-memory service for development |
| 103 | +Console.WriteLine("Using InMemoryDataService for development"); |
| 104 | +builder.Services.AddScoped<Stamps.ManagementPortal.Services.IDataService, Stamps.ManagementPortal.Services.InMemoryDataService>(); |
126 | 105 |
|
127 | 106 | // Configure Azure Infrastructure Service |
128 | 107 | builder.Services.AddScoped<Stamps.ManagementPortal.Services.IAzureInfrastructureService, Stamps.ManagementPortal.Services.AzureInfrastructureService>(); |
|
135 | 114 |
|
136 | 115 | var app = builder.Build(); |
137 | 116 |
|
| 117 | +// Map HotChocolate GraphQL endpoint |
| 118 | +app.MapGraphQL("/graphql"); |
| 119 | + |
138 | 120 | // Configure the HTTP request pipeline |
139 | 121 | if (!app.Environment.IsDevelopment()) |
140 | 122 | { |
|
148 | 130 | app.UseForwardedHeaders(); |
149 | 131 | } |
150 | 132 |
|
| 133 | + |
151 | 134 | app.UseHttpsRedirection(); |
152 | 135 | app.UseStaticFiles(); |
153 | 136 |
|
154 | 137 | app.UseRouting(); |
155 | 138 |
|
156 | | -// Add authentication middleware for production |
| 139 | +// Enable WebSockets for GraphQL subscriptions |
| 140 | +app.UseWebSockets(); |
| 141 | + |
| 142 | +// Add authentication middleware for production only |
157 | 143 | if (app.Environment.IsProduction()) |
158 | 144 | { |
159 | 145 | app.UseAuthentication(); |
|
0 commit comments