project reorganized
This commit is contained in:
23
WatchIt.WebAPI/WatchIt.WebAPI/Dockerfile
Normal file
23
WatchIt.WebAPI/WatchIt.WebAPI/Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["WatchIt.WebAPI/WatchIt.WebAPI/WatchIt.WebAPI.csproj", "WatchIt.WebAPI/WatchIt.WebAPI/"]
|
||||
RUN dotnet restore "WatchIt.WebAPI/WatchIt.WebAPI/WatchIt.WebAPI.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/WatchIt.WebAPI/WatchIt.WebAPI"
|
||||
RUN dotnet build "WatchIt.WebAPI.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "WatchIt.WebAPI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "WatchIt.WebAPI.dll"]
|
||||
161
WatchIt.WebAPI/WatchIt.WebAPI/Program.cs
Normal file
161
WatchIt.WebAPI/WatchIt.WebAPI/Program.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.WebAPI.Services.Controllers.Accounts;
|
||||
using WatchIt.WebAPI.Services.Controllers.Genres;
|
||||
using WatchIt.WebAPI.Services.Controllers.Movies;
|
||||
using WatchIt.WebAPI.Services.Utility.Configuration;
|
||||
using WatchIt.WebAPI.Services.Utility.Tokens;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
using WatchIt.WebAPI.Validators;
|
||||
using WatchIt.WebAPI.WorkerServices;
|
||||
|
||||
namespace WatchIt.WebAPI;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
WebApplication app = WebApplication.CreateBuilder(args)
|
||||
.SetupAuthentication()
|
||||
.SetupDatabase()
|
||||
.SetupWorkerServices()
|
||||
.SetupServices()
|
||||
.SetupApplication()
|
||||
.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private static WebApplicationBuilder SetupAuthentication(this WebApplicationBuilder builder)
|
||||
{
|
||||
AuthenticationBuilder authenticationBuilder = builder.Services.AddAuthentication(x =>
|
||||
{
|
||||
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
});
|
||||
authenticationBuilder.AddJwtBearer(x =>
|
||||
{
|
||||
x.RequireHttpsMetadata = false;
|
||||
x.SaveToken = true;
|
||||
x.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration.GetValue<string>("Authentication:Key")!)),
|
||||
ValidateAudience = true,
|
||||
ValidAudience = "access",
|
||||
ValidIssuer = builder.Configuration.GetValue<string>("Authentication:Issuer"),
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.FromMinutes(1),
|
||||
};
|
||||
x.Events = new JwtBearerEvents
|
||||
{
|
||||
OnAuthenticationFailed = context =>
|
||||
{
|
||||
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
||||
{
|
||||
context.Response.Headers.Append("Token-Expired", "true");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
};
|
||||
});
|
||||
authenticationBuilder.AddJwtBearer("refresh", x =>
|
||||
{
|
||||
x.RequireHttpsMetadata = false;
|
||||
x.SaveToken = true;
|
||||
x.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration.GetValue<string>("Authentication:Key")!)),
|
||||
ValidateAudience = true,
|
||||
ValidIssuer = builder.Configuration.GetValue<string>("Authentication:Issuer"),
|
||||
ValidAudience = "refresh",
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.FromMinutes(1)
|
||||
};
|
||||
x.Events = new JwtBearerEvents
|
||||
{
|
||||
OnAuthenticationFailed = context =>
|
||||
{
|
||||
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
||||
{
|
||||
context.Response.Headers.Append("Token-Expired", "true");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static WebApplicationBuilder SetupDatabase(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddDbContext<DatabaseContext>(x => x.UseLazyLoadingProxies().UseNpgsql(builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Singleton);
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static WebApplicationBuilder SetupWorkerServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddHostedService<DeleteExpiredRefreshTokensService>();
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static WebApplicationBuilder SetupServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
// Utility
|
||||
builder.Services.AddSingleton<IConfigurationService, ConfigurationService>();
|
||||
builder.Services.AddSingleton<ITokensService, TokensService>();
|
||||
builder.Services.AddSingleton<IUserService, UserService>();
|
||||
|
||||
// Controller
|
||||
builder.Services.AddSingleton<IAccountsControllerService, AccountsControllerService>();
|
||||
builder.Services.AddSingleton<IGenresControllerService, GenresControllerService>();
|
||||
builder.Services.AddSingleton<IMoviesControllerService, MoviesControllerService>();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static WebApplicationBuilder SetupApplication(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddValidatorsFromAssembly(Assembly.GetAssembly(typeof(CustomValidators)));
|
||||
builder.Services.AddFluentValidationAutoValidation();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
41
WatchIt.WebAPI/WatchIt.WebAPI/Properties/launchSettings.json
Normal file
41
WatchIt.WebAPI/WatchIt.WebAPI/Properties/launchSettings.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:10207",
|
||||
"sslPort": 44335
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5179",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7160;http://localhost:5179",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
WatchIt.WebAPI/WatchIt.WebAPI/WatchIt.WebAPI.csproj
Normal file
36
WatchIt.WebAPI/WatchIt.WebAPI/WatchIt.WebAPI.csproj
Normal file
@@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Controllers\WatchIt.WebAPI.Controllers.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.Configuration\WatchIt.WebAPI.Services.Utility.Configuration.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.Tokens\WatchIt.WebAPI.Services.Utility.Tokens.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Validators\WatchIt.WebAPI.Validators.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.WorkerServices\WatchIt.WebAPI.WorkerServices.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
36
WatchIt.WebAPI/WatchIt.WebAPI/appsettings.json
Normal file
36
WatchIt.WebAPI/WatchIt.WebAPI/appsettings.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
|
||||
},
|
||||
"Console": {
|
||||
"FormatterOptions": {
|
||||
"TimestampFormat": "[yyyy-MM-dd HH:mm:ss] "
|
||||
}
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"Default": "Host=localhost;Database=watchit;Username=watchit;Password=Xdv2Etchavbuuho"
|
||||
},
|
||||
"RootUser": {
|
||||
"Username": "root",
|
||||
"Email": "root@watch.it",
|
||||
"Password": "bECdHfbus2QHr4QQjApM"
|
||||
},
|
||||
"Authentication": {
|
||||
"Key": "testkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytestkeytest",
|
||||
"Issuer": "WatchIt",
|
||||
"Tokens": {
|
||||
"RefreshToken": {
|
||||
"NormalLifetime": 1440,
|
||||
"ExtendedLifetime": 10080
|
||||
},
|
||||
"AccessToken": {
|
||||
"NormalLifetime": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user