Fixed deployment
This commit is contained in:
3
src/backend/server/.dockerignore
Normal file
3
src/backend/server/.dockerignore
Normal file
@@ -0,0 +1,3 @@
|
||||
Dockerfile
|
||||
bin/
|
||||
obj/
|
@@ -23,5 +23,8 @@ RUN dotnet publish "Todo.Api.csproj" -c Release -o /app/publish
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
|
||||
ENV ASPNETCORE_URLS=http://+:80
|
||||
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "Todo.Api.dll"]
|
||||
|
@@ -11,12 +11,10 @@ namespace Todo.Api
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
public static void Main(string[] args) =>
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
private static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
||||
}
|
||||
|
@@ -16,7 +16,12 @@
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||
"MONGODB__Username": "root",
|
||||
"MONGODB__Password": "example",
|
||||
"MONGODB__Database": "todo",
|
||||
"MONGODB__Host": "localhost",
|
||||
"MONGODB__Port": "27017"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ namespace Todo.Api
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo.Api", Version = "v1" });
|
||||
});
|
||||
|
||||
services.AddPersistence();
|
||||
services.AddPersistence(Configuration);
|
||||
services.AddSignalR();
|
||||
}
|
||||
|
||||
|
@@ -7,5 +7,4 @@ namespace Todo.Core;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
|
||||
}
|
@@ -2,17 +2,31 @@ global using System;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Linq;
|
||||
global using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Todo.Core.Interfaces.Persistence;
|
||||
using Todo.Persistence.Mongo;
|
||||
using Todo.Persistence.Mongo.Repositories;
|
||||
|
||||
namespace Todo.Persistence
|
||||
{
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddPersistence(this IServiceCollection services) =>
|
||||
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
|
||||
var options = configuration.GetRequiredSection("MONGODB");
|
||||
Console.WriteLine(options.Value);
|
||||
|
||||
services
|
||||
.AddOptions<MongoDbOptions>()
|
||||
.Bind(options)
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
return services
|
||||
.AddSingleton<MongoDbConnectionHandler>()
|
||||
.AddScoped<IUserRepository, UserRepository>()
|
||||
.AddScoped<ITodoRepository, TodoRepository>();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MongoDB.Driver;
|
||||
using Todo.Persistence.Mongo.Repositories.Dtos;
|
||||
|
||||
@@ -10,8 +11,8 @@ namespace Todo.Persistence.Mongo
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var conn = new MongoClient("mongodb://root:example@localhost:27017");
|
||||
var database = conn.GetDatabase("todo");
|
||||
var connectionHandler = app.ApplicationServices.GetRequiredService<MongoDbConnectionHandler>();
|
||||
var database = connectionHandler.CreateDatabaseConnection();
|
||||
|
||||
await CreateIndexes(database);
|
||||
}).Wait(10000);
|
||||
|
@@ -0,0 +1,30 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Todo.Persistence.Mongo;
|
||||
|
||||
public class MongoDbConnectionHandler
|
||||
{
|
||||
private readonly IOptionsMonitor<MongoDbOptions> _optionsMonitor;
|
||||
|
||||
public MongoDbConnectionHandler(IOptionsMonitor<MongoDbOptions> optionsMonitor)
|
||||
{
|
||||
_optionsMonitor = optionsMonitor;
|
||||
}
|
||||
|
||||
public IMongoDatabase CreateDatabaseConnection()
|
||||
{
|
||||
var options = _optionsMonitor.CurrentValue;
|
||||
|
||||
return CreateConnectionFromOptions(options);
|
||||
}
|
||||
|
||||
private static IMongoDatabase CreateConnectionFromOptions(MongoDbOptions options)
|
||||
{
|
||||
var conn = new MongoClient(
|
||||
$"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}");
|
||||
var database = conn.GetDatabase(options.Database);
|
||||
|
||||
return database;
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Todo.Persistence.Mongo;
|
||||
|
||||
public class MongoDbOptions
|
||||
{
|
||||
public const string MongoDb = "MongoDb";
|
||||
|
||||
[Required] public string Username { get; set; }
|
||||
[Required] public string Password { get; set; }
|
||||
[Required] public string Host { get; set; }
|
||||
[Required] public string Port { get; set; }
|
||||
[Required] public string Database { get; set; }
|
||||
}
|
@@ -8,10 +8,9 @@ public class TodoRepository : ITodoRepository
|
||||
{
|
||||
private readonly IMongoCollection<MongoTodo> _todosCollection;
|
||||
|
||||
public TodoRepository()
|
||||
public TodoRepository(MongoDbConnectionHandler mongoDbConnectionHandler)
|
||||
{
|
||||
var conn = new MongoClient("mongodb://root:example@localhost:27017");
|
||||
var database = conn.GetDatabase("todo");
|
||||
var database = mongoDbConnectionHandler.CreateDatabaseConnection();
|
||||
|
||||
_todosCollection = database.GetCollection<MongoTodo>("todos");
|
||||
}
|
||||
|
@@ -9,11 +9,9 @@ namespace Todo.Persistence.Mongo.Repositories
|
||||
{
|
||||
private readonly IMongoCollection<MongoUser> _usersCollection;
|
||||
|
||||
public UserRepository()
|
||||
public UserRepository(MongoDbConnectionHandler mongoDbConnectionHandler)
|
||||
{
|
||||
var conn = new MongoClient("mongodb://root:example@localhost:27017");
|
||||
var database = conn.GetDatabase("todo");
|
||||
|
||||
var database = mongoDbConnectionHandler.CreateDatabaseConnection();
|
||||
_usersCollection = database.GetCollection<MongoUser>("users");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user