Skip to content

Commit f6df73a

Browse files
Fix CORS
1 parent e2f2e9c commit f6df73a

File tree

5 files changed

+42
-66
lines changed

5 files changed

+42
-66
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ This is a small demo project to quickly setup a mix of containerized micro-servi
1616

1717
The project consists of
1818

19-
- An API Gateway, written in .NET Core, exposes port `8080`
20-
- A Radom Number Generator Service, written in .NET Core, exposes port `8090`
21-
- A Web Frontend, written in Angular, exposes port `5000`
19+
- An API Gateway, written in .NET Core, exposes port `8080` (`8080` when in a Container)
20+
- A Radom Number Generator Service, written in .NET Core, exposes port `8090` (`8080` when in a Container)
21+
- A Web Frontend, written in Angular, exposes port `4200` (`8080` when in a Container)
2222

2323
## Make it run
2424

env/docker/docker-compose.yaml

+4-20
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ services:
77
context: ../../src/api
88
dockerfile: Dockerfile
99
ports:
10-
- "8080:8080"
10+
- "5100:8080"
1111
environment:
1212
- Database=CosmosDB
1313
- RandomApiUrl=http://random:8090
14-
- Cors="http://localhost:5000"
14+
- Cors=http://localhost:8080
1515
env_file:
1616
- .env
1717
depends_on:
@@ -21,8 +21,6 @@ services:
2121
build:
2222
context: ../../src/random
2323
dockerfile: Dockerfile
24-
ports:
25-
- "8090:8090"
2624
env_file:
2725
- .env
2826

@@ -31,22 +29,8 @@ services:
3129
context: ../../src/web
3230
dockerfile: Dockerfile
3331
ports:
34-
- "5000:8080"
32+
- "8080:8080"
3533
environment:
36-
- API_URL=http://localhost:8080
34+
- API_URL=http://localhost:5100
3735
depends_on:
3836
- api
39-
40-
# cosmosdb:
41-
# image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
42-
# hostname: cosmosdb
43-
# mem_limit: 3g
44-
# cpus: 2.0
45-
# ports:
46-
# - "8081:8081"
47-
# - "10251:10251"
48-
# - "10252:10252"
49-
# - "10254:10254"
50-
# environment:
51-
# - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
52-
# - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true

src/api/MicroCommunication.Api/Startup.cs

+26-20
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ public void ConfigureServices(IServiceCollection services)
9393
Console.WriteLine("Using Azure Application Insights");
9494
}
9595

96+
// CORS
97+
services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
98+
{
99+
var cors = string.IsNullOrEmpty(Configuration["Cors"])
100+
? "http://localhost:5000"
101+
: Configuration["Cors"];
102+
Console.WriteLine("CORS: " + Configuration["Cors"]);
103+
104+
builder
105+
.SetIsOriginAllowedToAllowWildcardSubdomains()
106+
.WithOrigins(Configuration["Cors"])
107+
.AllowAnyMethod()
108+
.AllowAnyHeader()
109+
.AllowCredentials();
110+
}));
111+
96112
// Enforce lowercase routes
97113
services.AddRouting(options => options.LowercaseUrls = true);
98114

@@ -144,19 +160,6 @@ public void ConfigureServices(IServiceCollection services)
144160
});
145161
}
146162
});
147-
148-
// CORS
149-
services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
150-
{
151-
builder
152-
.WithOrigins("http://localhost:5000")
153-
.AllowAnyMethod()
154-
.AllowAnyHeader()
155-
.AllowCredentials();
156-
157-
if (!string.IsNullOrEmpty(Configuration["Cors"]))
158-
builder.WithOrigins(Configuration["Cors"]);
159-
}));
160163
}
161164

162165
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -176,27 +179,30 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
176179
});
177180
}
178181

182+
app.UseSwagger();
183+
app.UseSwaggerUI(c =>
184+
{
185+
c.SwaggerEndpoint("/swagger/1.0/swagger.json", "Version 1.0");
186+
c.RoutePrefix = string.Empty;
187+
});
188+
179189
app.UseRouting();
180190

181191
app.UseCors("CorsPolicy");
182192

183193
app.UseAuthorization();
184194

195+
app.UseOpenTelemetryPrometheusScrapingEndpoint();
185196
app.UseEndpoints(endpoints =>
186197
{
187198
endpoints.MapControllers();
188199
endpoints.MapHealthChecks("/healthz");
189200
endpoints.MapHub<ChatHub>("/chat");
190201
});
191202

192-
app.UseOpenTelemetryPrometheusScrapingEndpoint();
193203

194-
app.UseSwagger();
195-
app.UseSwaggerUI(c =>
196-
{
197-
c.SwaggerEndpoint("/swagger/1.0/swagger.json", "Version 1.0");
198-
c.RoutePrefix = string.Empty;
199-
});
204+
205+
200206
}
201207
}
202208
}

src/random/MicroCommunication.Random/Startup.cs

+7-22
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,6 @@ public void ConfigureServices(IServiceCollection services)
105105
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
106106
c.IncludeXmlComments(xmlPath);
107107
});
108-
109-
// CORS
110-
services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
111-
{
112-
builder
113-
.WithOrigins("http://localhost:4200")
114-
.AllowAnyMethod()
115-
.AllowAnyHeader()
116-
.AllowCredentials();
117-
118-
if (!string.IsNullOrEmpty(Configuration["Cors"]))
119-
builder.WithOrigins(Configuration["Cors"]);
120-
}));
121108
}
122109

123110
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -128,9 +115,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
128115
app.UseDeveloperExceptionPage();
129116
}
130117

131-
app.UseRouting();
118+
app.UseSwagger();
119+
app.UseSwaggerUI(c =>
120+
{
121+
c.SwaggerEndpoint("/swagger/1.0/swagger.json", "Version 1.0");
122+
c.RoutePrefix = string.Empty;
123+
});
132124

133-
app.UseCors("CorsPolicy");
125+
app.UseRouting();
134126

135127
app.UseAuthorization();
136128

@@ -139,13 +131,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
139131
endpoints.MapHealthChecks("/healthz");
140132
endpoints.MapControllers();
141133
});
142-
143-
app.UseSwagger();
144-
app.UseSwaggerUI(c =>
145-
{
146-
c.SwaggerEndpoint("/swagger/1.0/swagger.json", "Version 1.0");
147-
c.RoutePrefix = string.Empty;
148-
});
149134
}
150135
}
151136
}

src/web/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ RUN npm run build -- --configuration production --output-path=/dist
1717
#######################################################
1818
# Step 2: Run the build outcome in a container #
1919
#######################################################
20-
FROM nginx:alpine
20+
FROM nginx:1.21.0-alpine
2121

2222
# Copy the build outputs over to this container
2323
COPY nginx.conf /etc/nginx/nginx.conf
@@ -36,5 +36,6 @@ RUN echo "envsubst < /usr/share/nginx/html/assets/appsettings.template.js > /usr
3636

3737
# Switch to new user
3838
USER node
39+
EXPOSE 8080
3940

4041
CMD ["/bin/sh", "run.sh"]

0 commit comments

Comments
 (0)