Moved projects to their own separate subdirectories
This commit is contained in:
146
API/YABA.API/Controllers/BookmarksController.cs
Normal file
146
API/YABA.API/Controllers/BookmarksController.cs
Normal file
@ -0,0 +1,146 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.JsonPatch;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.ViewModels.Bookmarks;
|
||||
using YABA.API.ViewModels.Tags;
|
||||
using YABA.Common.DTOs.Bookmarks;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[Authorize]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class BookmarksController : ControllerBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IBookmarkService _bookmarkService;
|
||||
|
||||
public BookmarksController(IMapper mapper, IBookmarkService bookmarkService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_bookmarkService = bookmarkService;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(BookmarkResponse), (int)HttpStatusCode.Created)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> Create([FromBody] CreateBookmarkRequestDTO request)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var result = await _bookmarkService.CreateBookmark(request);
|
||||
|
||||
if(result == null) return BadRequest();
|
||||
|
||||
return CreatedAtAction(nameof(Create), _mapper.Map<BookmarkResponse>(result));
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(typeof(BookmarkResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> UpdateBookmark(int id, [FromBody] UpdateBookmarkRequestDTO request)
|
||||
{
|
||||
var result = await _bookmarkService.UpdateBookmark(id, request);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return Ok(_mapper.Map<BookmarkResponse>(result));
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(typeof(BookmarkResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> PatchBookmark(int id, [FromBody] JsonPatchDocument<PatchBookmarkRequest> request)
|
||||
{
|
||||
if (request == null || !ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var entryToEdit = await _bookmarkService.Get(id);
|
||||
if(entryToEdit == null) return NotFound();
|
||||
|
||||
var entryToEditAsPatchRequest = _mapper.Map<PatchBookmarkRequest>(entryToEdit);
|
||||
request.ApplyTo(entryToEditAsPatchRequest, ModelState);
|
||||
|
||||
var updateRequest = _mapper.Map<UpdateBookmarkRequestDTO>(entryToEditAsPatchRequest);
|
||||
var result = await _bookmarkService.UpdateBookmark(id, updateRequest);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<BookmarkResponse>), (int)HttpStatusCode.OK)]
|
||||
public IActionResult GetAll(bool showHidden = false)
|
||||
{
|
||||
var result = _bookmarkService.GetAll(showHidden);
|
||||
return Ok(_mapper.Map<IEnumerable<BookmarkResponse>>(result));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(typeof(BookmarkResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> Get(int id)
|
||||
{
|
||||
var result = await _bookmarkService.Get(id);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return Ok(_mapper.Map<BookmarkResponse>(result));
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
var result = await _bookmarkService.DeleteBookmark(id);
|
||||
|
||||
if (!result.HasValue) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> DeleteBookmarks([FromBody] DeleteBookmarksRequest request)
|
||||
{
|
||||
if (request.Ids == null || !request.Ids.Any()) return BadRequest();
|
||||
|
||||
var result = await _bookmarkService.DeleteBookmarks(request.Ids);
|
||||
|
||||
if(result == null) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("Hide")]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> HideBookmarks([FromBody] HideBookmarksRequest request)
|
||||
{
|
||||
if (request.Ids == null || !request.Ids.Any()) return BadRequest();
|
||||
|
||||
var result = await _bookmarkService.HideBookmarks(request.Ids);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("Tags")]
|
||||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||||
public IActionResult GetBookmarkTags(bool showHidden = false)
|
||||
{
|
||||
var result = _bookmarkService.GetAllBookmarkTags(showHidden);
|
||||
return Ok(_mapper.Map<IEnumerable<TagResponse>>(result));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
40
API/YABA.API/Controllers/MiscController.cs
Normal file
40
API/YABA.API/Controllers/MiscController.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.Settings;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[Authorize, Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class MiscController : ControllerBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IMiscService _miscService;
|
||||
|
||||
public MiscController(
|
||||
IMapper mapper,
|
||||
IMiscService miscService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_miscService = miscService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[DevOnly]
|
||||
[Route("GetWebsiteMetaData")]
|
||||
[ProducesResponseType(typeof(GetWebsiteMetaDataResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public IActionResult GetWebsiteMetaData(string url)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url)) return BadRequest();
|
||||
|
||||
var response = _miscService.GetWebsiteMetaData(url);
|
||||
return Ok(_mapper.Map<GetWebsiteMetaDataResponse>(response));
|
||||
}
|
||||
}
|
||||
}
|
||||
110
API/YABA.API/Controllers/TagsController.cs
Normal file
110
API/YABA.API/Controllers/TagsController.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.ViewModels.Tags;
|
||||
using YABA.Common.DTOs.Tags;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[Authorize]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class TagsController : ControllerBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ITagsService _tagsService;
|
||||
|
||||
public TagsController(IMapper mapper, ITagsService tagsService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_tagsService = tagsService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<TagResponse>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> GetAll()
|
||||
{
|
||||
var result = await _tagsService.GetAll();
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return Ok(_mapper.Map<IEnumerable<TagResponse>>(result));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(typeof(TagResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> Get(int id)
|
||||
{
|
||||
var result = await _tagsService.Get(id);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return Ok(_mapper.Map<TagResponse>(result));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(IEnumerable<TagResponse>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> CreateTag([FromBody]CreateTagDTO request)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var result = await _tagsService.CreateTag(request);
|
||||
|
||||
if (result == null) return BadRequest();
|
||||
|
||||
return Ok(_mapper.Map<TagResponse>(result));
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(typeof(IEnumerable<TagResponse>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> UpdateTag(int id, [FromBody]UpdateTagDTO request)
|
||||
{
|
||||
var result = await _tagsService.UpdateTag(id, request);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return Ok(_mapper.Map<TagResponse>(result));
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(typeof(IEnumerable<TagResponse>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> PatchTag(int id, [FromBody] UpdateTagDTO request) => await UpdateTag(id, request);
|
||||
|
||||
|
||||
[HttpDelete]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
[ProducesResponseType ((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> DeleteTags([FromBody] DeleteTagsRequest request)
|
||||
{
|
||||
if(request.Ids == null || !request.Ids.Any()) return BadRequest();
|
||||
|
||||
var result = await _tagsService.DeleteTags(request.Ids);
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("Hide")]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> HideTags([FromBody] HideTagsRequest request)
|
||||
{
|
||||
if (request.Ids == null || !request.Ids.Any()) return BadRequest();
|
||||
|
||||
var result = await _tagsService.HideTags(request.Ids);
|
||||
if (result == null) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
42
API/YABA.API/Controllers/TestController.cs
Normal file
42
API/YABA.API/Controllers/TestController.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.Settings;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[DevOnly]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class TestController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<TestController> _logger;
|
||||
|
||||
public TestController(ILogger<TestController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("TestLog")]
|
||||
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public IActionResult TestLog()
|
||||
{
|
||||
var testObject = new { id = 1, name = "Test Message" };
|
||||
_logger.LogDebug("Testing debug: {@TestObject}", testObject);
|
||||
return Ok(testObject);
|
||||
}
|
||||
|
||||
[HttpGet("TestLogError")]
|
||||
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public IActionResult TestLogError()
|
||||
{
|
||||
var testObject = new { id = 1, name = "Test Message" };
|
||||
throw new Exception();
|
||||
return Ok(testObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
API/YABA.API/Controllers/UsersController.cs
Normal file
44
API/YABA.API/Controllers/UsersController.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.Extensions;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[Authorize]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UsersController(IMapper mapper, IUserService userService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(UserResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
public async Task<IActionResult> Register()
|
||||
{
|
||||
var authProviderId = this.GetAuthProviderId();
|
||||
|
||||
if (string.IsNullOrEmpty(authProviderId)) return NotFound();
|
||||
|
||||
var isRegistered = _userService.IsUserRegistered(authProviderId);
|
||||
|
||||
if (isRegistered) return NoContent();
|
||||
|
||||
var registedUser = await _userService.RegisterUser(authProviderId);
|
||||
return Ok(_mapper.Map<UserResponse>(registedUser));
|
||||
}
|
||||
}
|
||||
}
|
||||
22
API/YABA.API/Dockerfile
Normal file
22
API/YABA.API/Dockerfile
Normal file
@ -0,0 +1,22 @@
|
||||
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
EXPOSE 443
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
|
||||
WORKDIR /src
|
||||
COPY ["YABA.API/YABA.API.csproj", "YABA.API/"]
|
||||
RUN dotnet restore "YABA.API/YABA.API.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/YABA.API"
|
||||
RUN dotnet build "YABA.API.csproj" -c Release -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "YABA.API.csproj" -c Release -o /app/publish
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "YABA.API.dll"]
|
||||
28
API/YABA.API/Extensions/ControllerExtensions.cs
Normal file
28
API/YABA.API/Extensions/ControllerExtensions.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using YABA.Common.Extensions;
|
||||
using YABA.Common.Lookups;
|
||||
|
||||
namespace YABA.API.Extensions
|
||||
{
|
||||
public static class ControllerExtensions
|
||||
{
|
||||
public static string GetAuthProviderId(this ControllerBase controller)
|
||||
{
|
||||
return controller.User.Identity.GetCustomClaim(ClaimsLookup.AuthProviderId);
|
||||
}
|
||||
|
||||
public static int GetUserId(this ControllerBase controller)
|
||||
{
|
||||
var isValidUserId = int.TryParse(controller.User.Identity.GetCustomClaim(ClaimsLookup.UserId), out int userId);
|
||||
return isValidUserId ? userId : 0;
|
||||
}
|
||||
|
||||
public static string GetIpAddress(this ControllerBase controller)
|
||||
{
|
||||
if (controller.Request.Headers.ContainsKey("X-Forwarded-For"))
|
||||
return controller.Request.Headers["X-Forwarded-For"];
|
||||
|
||||
return controller.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
41
API/YABA.API/Middlewares/AddCustomClaimsMiddleware.cs
Normal file
41
API/YABA.API/Middlewares/AddCustomClaimsMiddleware.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System.Security.Claims;
|
||||
using YABA.API.Extensions;
|
||||
using YABA.Common.Extensions;
|
||||
using YABA.Common.Lookups;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.API.Middlewares
|
||||
{
|
||||
public class AddCustomClaimsMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public AddCustomClaimsMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext httpContext, IUserService userService)
|
||||
{
|
||||
if (httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
|
||||
{
|
||||
var userAuthProviderId = httpContext.User.Identity.GetAuthProviderId();
|
||||
|
||||
if (!string.IsNullOrEmpty(userAuthProviderId))
|
||||
{
|
||||
var userId = userService.GetUserId(userAuthProviderId);
|
||||
|
||||
if(userId <= 0)
|
||||
{
|
||||
var registedUser = await userService.RegisterUser(userAuthProviderId);
|
||||
userId = registedUser.Id;
|
||||
}
|
||||
|
||||
httpContext.User.Identities.FirstOrDefault().AddClaim(new Claim(ClaimsLookup.UserId.GetClaimName(), userId.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
await _next(httpContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
using Serilog.Context;
|
||||
using YABA.Common.Extensions;
|
||||
|
||||
namespace YABA.API.Middlewares
|
||||
{
|
||||
public class AddCustomLoggingPropertiesMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public AddCustomLoggingPropertiesMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext httpContext)
|
||||
{
|
||||
if(httpContext.Request.Path.HasValue && httpContext.Request.Path.Value.Contains("/api"))
|
||||
{
|
||||
LogContext.PushProperty("UserId", httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.GetUserId() : "Anonymous");
|
||||
LogContext.PushProperty("RemoteIpAddress", httpContext.Connection.RemoteIpAddress.MapToIPv4());
|
||||
}
|
||||
|
||||
await _next(httpContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
130
API/YABA.API/Program.cs
Normal file
130
API/YABA.API/Program.cs
Normal file
@ -0,0 +1,130 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Versioning;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Security.Claims;
|
||||
using YABA.API.Middlewares;
|
||||
using YABA.API.Settings;
|
||||
using YABA.API.Settings.Swashbuckle;
|
||||
using YABA.Data.Configuration;
|
||||
using YABA.Data.Context;
|
||||
using YABA.Service.Configuration;
|
||||
using Serilog;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var configuration = builder.Configuration;
|
||||
|
||||
// Add services to the container.
|
||||
var auth0Section = configuration.GetSection("Authentication").GetSection("Auth0");
|
||||
var auth0Settings = auth0Section.Get<Auth0Settings>();
|
||||
var domain = $"https://{auth0Settings.Domain}/";
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = domain;
|
||||
options.Audience = auth0Settings.Identifier;
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
NameClaimType = ClaimTypes.NameIdentifier
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddApiVersioning(setup =>
|
||||
{
|
||||
setup.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
setup.AssumeDefaultVersionWhenUnspecified = true;
|
||||
setup.ReportApiVersions = true;
|
||||
setup.ApiVersionReader = new UrlSegmentApiVersionReader();
|
||||
});
|
||||
|
||||
// Add services to the container
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddServiceProjectDependencyInjectionConfiguration(configuration);
|
||||
builder.Services.AddDataProjectDependencyInjectionConfiguration(configuration);
|
||||
builder.Services.AddControllers().AddNewtonsoftJson();
|
||||
builder.Services.AddHealthChecks();
|
||||
|
||||
// Add AutoMapper profiles
|
||||
var mapperConfiguration = new MapperConfiguration(mapperConfiguration =>
|
||||
{
|
||||
mapperConfiguration.AddProfile(new YABA.API.Settings.AutoMapperProfile());
|
||||
mapperConfiguration.AddProfile(new YABA.Service.Configuration.AutoMapperProfile());
|
||||
});
|
||||
|
||||
IMapper mapper = mapperConfiguration.CreateMapper();
|
||||
builder.Services.AddSingleton(mapper);
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(
|
||||
c =>
|
||||
{
|
||||
c.SwaggerDoc(
|
||||
"v1",
|
||||
new OpenApiInfo
|
||||
{
|
||||
Title = "YABA.API",
|
||||
Version = "v1"
|
||||
});
|
||||
c.OperationFilter<RemoveVersionParameterFilter>();
|
||||
c.DocumentFilter<ReplaceVersionWithExactValueInPathFilter>();
|
||||
c.ResolveConflictingActions(apiDescription => apiDescription.First());
|
||||
}
|
||||
);
|
||||
|
||||
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||
{
|
||||
options.ForwardedHeaders =
|
||||
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||
});
|
||||
|
||||
// Add Serilog
|
||||
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.FromLogContext().CreateLogger();
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Run database migrations
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var yabaDbContext = scope.ServiceProvider.GetRequiredService<YABAReadWriteContext>();
|
||||
yabaDbContext.Database.Migrate();
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseForwardedHeaders();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
// Add custom middlewares
|
||||
app.UseMiddleware<AddCustomClaimsMiddleware>();
|
||||
app.UseMiddleware<AddCustomLoggingPropertiesMiddleware>();
|
||||
|
||||
app.UseCors(x => x
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader());
|
||||
|
||||
var webClientUrl = configuration.GetSection("WebClient").GetValue<string>("Url");
|
||||
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().WithOrigins(webClientUrl));
|
||||
app.MapHealthChecks("/Pulse");
|
||||
|
||||
app.Run();
|
||||
38
API/YABA.API/Properties/launchSettings.json
Normal file
38
API/YABA.API/Properties/launchSettings.json
Normal file
@ -0,0 +1,38 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:7922",
|
||||
"sslPort": 44310
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"YABA.API": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:7032;http://localhost:5032",
|
||||
"dotnetRunMessages": true
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"publishAllPorts": true,
|
||||
"useSSL": true
|
||||
}
|
||||
}
|
||||
}
|
||||
10
API/YABA.API/Settings/Auth0Settings.cs
Normal file
10
API/YABA.API/Settings/Auth0Settings.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace YABA.API.Settings
|
||||
{
|
||||
public class Auth0Settings
|
||||
{
|
||||
public string Domain { get; set; }
|
||||
public string ClientSecret { get; set; }
|
||||
public string ClientId { get; set; }
|
||||
public string Identifier { get; set; }
|
||||
}
|
||||
}
|
||||
26
API/YABA.API/Settings/AutoMapperProfile.cs
Normal file
26
API/YABA.API/Settings/AutoMapperProfile.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using AutoMapper;
|
||||
using System.Net;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.API.ViewModels.Bookmarks;
|
||||
using YABA.API.ViewModels.Tags;
|
||||
using YABA.Common.DTOs;
|
||||
using YABA.Common.DTOs.Bookmarks;
|
||||
using YABA.Common.DTOs.Tags;
|
||||
|
||||
namespace YABA.API.Settings
|
||||
{
|
||||
public class AutoMapperProfile : Profile
|
||||
{
|
||||
public AutoMapperProfile()
|
||||
{
|
||||
CreateMap<UserDTO, UserResponse>();
|
||||
CreateMap<BookmarkDTO, BookmarkResponse>()
|
||||
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => WebUtility.HtmlDecode(src.Title)))
|
||||
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => WebUtility.HtmlDecode(src.Description)));
|
||||
CreateMap<WebsiteMetaDataDTO, GetWebsiteMetaDataResponse>();
|
||||
CreateMap<BookmarkDTO, PatchBookmarkRequest>();
|
||||
CreateMap<PatchBookmarkRequest, UpdateBookmarkRequestDTO>();
|
||||
CreateMap<TagDTO, TagResponse>().ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
34
API/YABA.API/Settings/DevOnlyAttribute.cs
Normal file
34
API/YABA.API/Settings/DevOnlyAttribute.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace YABA.API.Settings
|
||||
{
|
||||
// Source: https://stackoverflow.com/questions/56495475/asp-net-core-its-possible-to-configure-an-action-in-controller-only-in-developm
|
||||
public class DevOnlyAttribute : Attribute, IFilterFactory
|
||||
{
|
||||
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
|
||||
{
|
||||
return new DevOnlyAttributeImpl(serviceProvider.GetRequiredService<IWebHostEnvironment>());
|
||||
}
|
||||
|
||||
public bool IsReusable => true;
|
||||
|
||||
private class DevOnlyAttributeImpl : Attribute, IAuthorizationFilter
|
||||
{
|
||||
public DevOnlyAttributeImpl(IWebHostEnvironment hostingEnv)
|
||||
{
|
||||
HostingEnv = hostingEnv;
|
||||
}
|
||||
|
||||
private IWebHostEnvironment HostingEnv { get; }
|
||||
|
||||
public void OnAuthorization(AuthorizationFilterContext context)
|
||||
{
|
||||
if (!HostingEnv.IsDevelopment())
|
||||
{
|
||||
context.Result = new NotFoundResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace YABA.API.Settings.Swashbuckle
|
||||
{
|
||||
public class RemoveVersionParameterFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var versionParameter = operation.Parameters.Single(p => p.Name == "version");
|
||||
operation.Parameters.Remove(versionParameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace YABA.API.Settings.Swashbuckle
|
||||
{
|
||||
public class ReplaceVersionWithExactValueInPathFilter : IDocumentFilter
|
||||
{
|
||||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
|
||||
{
|
||||
var paths = new OpenApiPaths();
|
||||
foreach (var path in swaggerDoc.Paths)
|
||||
{
|
||||
paths.Add(path.Key.Replace("v{version}", swaggerDoc.Info.Version), path.Value);
|
||||
}
|
||||
swaggerDoc.Paths = paths;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
API/YABA.API/ViewModels/Bookmarks/BookmarkResponse.cs
Normal file
17
API/YABA.API/ViewModels/Bookmarks/BookmarkResponse.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using YABA.API.ViewModels.Tags;
|
||||
|
||||
namespace YABA.API.ViewModels.Bookmarks
|
||||
{
|
||||
public class BookmarkResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public DateTimeOffset CreatedOn { get; set; }
|
||||
public DateTimeOffset LastModified { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Note { get; set; }
|
||||
public bool IsHidden { get; set; }
|
||||
public string Url { get; set; }
|
||||
public IEnumerable<TagResponse> Tags { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
namespace YABA.API.ViewModels.Bookmarks
|
||||
{
|
||||
public class DeleteBookmarksRequest
|
||||
{
|
||||
public IEnumerable<int> Ids { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
namespace YABA.API.ViewModels.Bookmarks
|
||||
{
|
||||
public class HideBookmarksRequest
|
||||
{
|
||||
public IEnumerable<int> Ids { get; set; }
|
||||
}
|
||||
}
|
||||
11
API/YABA.API/ViewModels/Bookmarks/PatchBookmarkRequest.cs
Normal file
11
API/YABA.API/ViewModels/Bookmarks/PatchBookmarkRequest.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace YABA.API.ViewModels.Bookmarks
|
||||
{
|
||||
public class PatchBookmarkRequest
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Note { get; set; }
|
||||
public bool IsHidden { get; set; }
|
||||
public string? Url { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
namespace YABA.API.ViewModels.Bookmarks
|
||||
{
|
||||
public class UpdateBookmarkTagRequest
|
||||
{
|
||||
public List<string> Tags { get; set; }
|
||||
}
|
||||
}
|
||||
8
API/YABA.API/ViewModels/GetWebsiteMetaDataResponse.cs
Normal file
8
API/YABA.API/ViewModels/GetWebsiteMetaDataResponse.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace YABA.API.ViewModels
|
||||
{
|
||||
public class GetWebsiteMetaDataResponse
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
}
|
||||
7
API/YABA.API/ViewModels/Tags/DeleteTagsRequest.cs
Normal file
7
API/YABA.API/ViewModels/Tags/DeleteTagsRequest.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace YABA.API.ViewModels.Tags
|
||||
{
|
||||
public class DeleteTagsRequest
|
||||
{
|
||||
public IEnumerable<int> Ids { get; set; }
|
||||
}
|
||||
}
|
||||
7
API/YABA.API/ViewModels/Tags/HideTagsRequest.cs
Normal file
7
API/YABA.API/ViewModels/Tags/HideTagsRequest.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace YABA.API.ViewModels.Tags
|
||||
{
|
||||
public class HideTagsRequest
|
||||
{
|
||||
public IEnumerable<int> Ids { get; set; }
|
||||
}
|
||||
}
|
||||
9
API/YABA.API/ViewModels/Tags/TagResponse.cs
Normal file
9
API/YABA.API/ViewModels/Tags/TagResponse.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace YABA.API.ViewModels.Tags
|
||||
{
|
||||
public class TagResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool IsHidden { get; set; }
|
||||
}
|
||||
}
|
||||
11
API/YABA.API/ViewModels/UserResponse.cs
Normal file
11
API/YABA.API/ViewModels/UserResponse.cs
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
namespace YABA.API.ViewModels
|
||||
{
|
||||
public class UserResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public DateTimeOffset CreatedOn { get; set; }
|
||||
public DateTimeOffset LastModified { get; set; }
|
||||
}
|
||||
}
|
||||
45
API/YABA.API/YABA.API.csproj
Normal file
45
API/YABA.API/YABA.API.csproj
Normal file
@ -0,0 +1,45 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>726eb626-1514-45b8-8521-cd7353303edb</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.13" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.14" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.14">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
|
||||
<PackageReference Include="Serilog" Version="2.12.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
|
||||
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
|
||||
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.1.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\YABA.Common\YABA.Common.csproj" />
|
||||
<ProjectReference Include="..\YABA.Data\YABA.Data.csproj" />
|
||||
<ProjectReference Include="..\YABA.Service\YABA.Service.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Logs\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
8
API/YABA.API/appsettings.Development.json
Normal file
8
API/YABA.API/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
46
API/YABA.API/appsettings.json
Normal file
46
API/YABA.API/appsettings.json
Normal file
@ -0,0 +1,46 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": "Information",
|
||||
"Using": [
|
||||
"Serilog.Sinks.Grafana.Loki"
|
||||
],
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/log.json",
|
||||
"rollingInterval": "Day",
|
||||
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Name": "GrafanaLoki",
|
||||
"Args": {
|
||||
"uri": "https://loki.iwanaga.moe",
|
||||
"labels": [
|
||||
{
|
||||
"key": "job",
|
||||
"value": "YABA.API"
|
||||
},
|
||||
{
|
||||
"key": "environment",
|
||||
"value": "localhost"
|
||||
}
|
||||
],
|
||||
"propertiesAsLabels": [ "job", "environment" ]
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId"]
|
||||
},
|
||||
"WebClient": {
|
||||
"Url": "https://localhost:3000"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user