Skip to content

Commit

Permalink
Merge pull request #8 from jbonnett92/master
Browse files Browse the repository at this point in the history
API Gateway now working with Consul and Swagger implemented
  • Loading branch information
madslundt authored Oct 12, 2020
2 parents 7862ef1 + ea27a53 commit 1a8e761
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 17 deletions.
1 change: 1 addition & 0 deletions ApiGateway/Ocelot/ApiGateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="MMLib.SwaggerForOcelot" Version="2.5.1" />
<PackageReference Include="Ocelot" Version="16.0.1" />
<PackageReference Include="Ocelot.Administration" Version="16.0.1" />
<PackageReference Include="Ocelot.Provider.Consul" Version="16.0.1" />
Expand Down
35 changes: 34 additions & 1 deletion ApiGateway/Ocelot/Configuration/ocelot.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,38 @@
"ExceptionsAllowedBeforeBreaking": 3,
"DurationOfBreak": 10000,
"TimeoutValue": 5000
}
},

"SwaggerEndPoints": [
{
"Key": "users",
"Config": [
{
"Name": "Users API",
"Version": "v1",
"Url": "http://users_service/swagger/v1/swagger.json"
}
]
},
{
"Key": "movies",
"Config": [
{
"Name": "Movies API",
"Version": "v1",
"Url": "http://movies_service/swagger/v1/swagger.json"
}
]
},
{
"Key": "reviews",
"Config": [
{
"Name": "Reviews API",
"Version": "v1",
"Url": "http://reviews_service/swagger/v1/swagger.json"
}
]
}
]
}
12 changes: 8 additions & 4 deletions ApiGateway/Ocelot/Configuration/ocelot.movies.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "movies"
},
{
"DownstreamPathTemplate": "/api/movies/{movieId}/reviews/{everything}",
Expand All @@ -18,7 +19,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "movies"
},
{
"DownstreamPathTemplate": "/api/movies",
Expand All @@ -28,7 +30,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "movies"
},
{
"DownstreamPathTemplate": "/api/movies/{everything}",
Expand All @@ -38,7 +41,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "movies"
}
]
}
6 changes: 4 additions & 2 deletions ApiGateway/Ocelot/Configuration/ocelot.reviews.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "reviews"
},
{
"DownstreamPathTemplate": "/api/reviews/{everything}",
Expand All @@ -18,7 +19,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "reviews"
}
]
}
19 changes: 13 additions & 6 deletions ApiGateway/Ocelot/Configuration/ocelot.users.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "users"
},
{
"DownstreamPathTemplate": "/api/users/{userId}/movies/{everything}",
Expand All @@ -18,7 +19,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "users"
},

{
Expand All @@ -29,7 +31,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "users"
},
{
"DownstreamPathTemplate": "/api/users/{userId}/reviews/{everything}",
Expand All @@ -39,18 +42,21 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "users"
},

{
"DownstreamPathTemplate": "/api/users",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/users",
"UpstreamHttpMethod": [ "Post", "Get" ],
"ServiceName": "UsersService",
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "users"
},
{
"DownstreamPathTemplate": "/api/users/{everything}",
Expand All @@ -60,7 +66,8 @@
"FileCacheOptions": { "TtlSeconds": 15 },
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
},
"SwaggerKey": "users"
}
]
}
2 changes: 1 addition & 1 deletion ApiGateway/Ocelot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Expand Down
5 changes: 5 additions & 0 deletions ApiGateway/Ocelot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Startup(IWebHostEnvironment env)
public void ConfigureServices(IServiceCollection services)
{
services
.AddSwaggerForOcelot(Configuration)
.AddOcelot(Configuration)
.AddConsul()
.AddConfigStoredInConsul();
Expand All @@ -63,6 +64,10 @@ public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();

app.UseSwaggerForOcelotUI(opt => {
opt.PathToSwaggerGenerator = "/swagger/docs";
});

app
.UseOcelot()
.Wait();
Expand Down
4 changes: 2 additions & 2 deletions ApiGateway/Ocelot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
}
},
"AllowedHosts": "*",

"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Type": "PollConsul",
"Host": "http://consul",
"Scheme": "http",
"Host": "consul",
"Port": 8500,
"ConfigurationKey": "ApiGateway_1",
"PollingInterval": 100
Expand Down
1 change: 1 addition & 0 deletions Compose/docker-compose.ocelot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ services:
- users_service
- movies_service
- reviews_service
- consul
depends_on:
- consul
3 changes: 3 additions & 0 deletions Compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ services:
image: consul
container_name: consul
restart: on-failure
volumes:
- consul-data:/consul/config
ports:
- "8500:8500"
networks:
- network-microservice

volumes:
elasticsearch-data:
consul-data:
15 changes: 15 additions & 0 deletions Src/MoviesService/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
app.UseDeveloperExceptionPage();
}

UpdateDatabase(app);

app
.UseLogging(Configuration, loggerFactory)
.UseSwagger(Configuration)
Expand All @@ -63,5 +65,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF

app.UseSubscribeAllEvents();
}

private static void UpdateDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<DatabaseContext>())
{
context.Database.Migrate();
}
}
}
}
}
1 change: 1 addition & 0 deletions Src/MoviesService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"ConsulOptions": {
"ConsulAddress": "http://consul:8500",
"ServiceAddress": "http://movies_service",
"ServiceName": "MoviesService",
"DisableAgentCheck": false,
"Tags": []
},
Expand Down
15 changes: 15 additions & 0 deletions Src/ReviewsService/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
app.UseDeveloperExceptionPage();
}

UpdateDatabase(app);

app
.UseLogging(Configuration, loggerFactory)
.UseSwagger(Configuration)
Expand All @@ -63,5 +65,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF

app.UseSubscribeAllEvents();
}

private static void UpdateDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<DatabaseContext>())
{
context.Database.Migrate();
}
}
}
}
}
3 changes: 2 additions & 1 deletion Src/ReviewsService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"AllowedHosts": "*",

"ConnectionStrings": {
"AppConnection": "Server=localhost,1433;Database=reviews_db;User=sa;Password=pass123!;"
"AppConnection": "Server=db,1433;Database=reviews_db;User=sa;Password=pass123!;"
},

"MessageBrokersOptions": {
Expand Down Expand Up @@ -51,6 +51,7 @@
"ConsulOptions": {
"ConsulAddress": "http://consul:8500",
"ServiceAddress": "http://reviews_service",
"ServiceName": "ReviewsService",
"DisableAgentCheck": false,
"Tags": []
},
Expand Down
12 changes: 12 additions & 0 deletions Src/UsersService/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,17 @@ public UserController(IQueryBus queryBus, ICommandBus commandBus)

return Ok(result);
}

[HttpGet, Route("")]
public async Task<ActionResult<GetUsersQuery.Result>> CreateUser(CancellationToken cancellationToken)
{
var query = new GetUsersQuery.Query
{
};

var result = await _queryBus.Send(query, cancellationToken);

return Ok(result);
}
}
}
Loading

0 comments on commit 1a8e761

Please sign in to comment.