Skip to content

Commit

Permalink
Merge pull request #42 from aau-giraf/staging
Browse files Browse the repository at this point in the history
Infisical setup and new folder structure
  • Loading branch information
Dressi123 authored Oct 9, 2024
2 parents 9277830 + a43db59 commit 809f491
Show file tree
Hide file tree
Showing 37 changed files with 647 additions and 248 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/TagMain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ jobs:
- name: Build Docker image
run: |
docker build -t dressi123/foodplanner-api:prod .
working-directory: ./foodplanner-api
# Step 4: Push Docker image
- name: Push Docker image
run: |
docker push dressi123/foodplanner-api:prod
working-directory: ./foodplanner-api
28 changes: 26 additions & 2 deletions .github/workflows/buildTest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ on:
pull_request:
branches:
- staging
- main

jobs:
build:

build-and-test-dotnet:
runs-on: ubuntu-latest

steps:
Expand All @@ -22,3 +22,27 @@ jobs:
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal

test-docker-build:
needs: build-and-test-dotnet
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: false
4 changes: 4 additions & 0 deletions .github/workflows/enforcer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ jobs:
check_branch:
runs-on: ubuntu-latest
steps:
- name: Branch info
run: |
echo "Base branch: ${{ github.base_ref }}"
echo "Head branch: ${{ github.head_ref }}"
- name: Check branch
if: github.base_ref == 'main' && github.head_ref != 'staging'
run: |
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ jobs:
- name: Build Docker image
run: |
docker build -t dressi123/foodplanner-api:staging .
working-directory: ./foodplanner-api
# Step 4: Push Docker image
- name: Push Docker image
run: |
docker push dressi123/foodplanner-api:staging
working-directory: ./foodplanner-api
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ ehthumbs.db
Desktop.ini

# Ignore build directories
foodplanner-api/bin/
foodplanner-api/obj/
FoodplannerApi/bin/
FoodplannerApi/obj/

# Ignore private files
private.txt
private.txt

#Secrets
FoodplannerApi/appsettings.Development.json
/.vs
13 changes: 7 additions & 6 deletions foodplanner-api/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY *.csproj ./
RUN dotnet restore
COPY FoodplannerApi/*.csproj ./FoodplannerApi/
COPY FoodplannerDataAccessSql/*.csproj ./FoodplannerDataAccessSql/
COPY FoodplannerModels/*.csproj ./FoodplannerModels/
COPY FoodplannerServices/*.csproj ./FoodplannerServices/
RUN dotnet restore FoodplannerApi

# Copy the rest of the application code
COPY . .

# Publish the application
RUN dotnet publish -c Release -o out
RUN dotnet publish FoodplannerApi -c Release -o out

# Build the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
Expand All @@ -20,7 +23,5 @@ COPY --from=build /app/out ./
# Expose the port your application will run on
EXPOSE 80

ENV ASPNETCORE_ENVIRONMENT=Development

# Start the application
ENTRYPOINT ["dotnet", "foodplanner-api.dll"]
CMD ["dotnet", "FoodplannerApi.dll"]
42 changes: 42 additions & 0 deletions FoodplannerApi/Controller/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;

namespace FoodplannerApi.Controller
{
[Route("api/[controller]/[action]")]
public abstract class BaseController : ControllerBase
{
public string Language
{
get
{
var lang = Request.Headers["Accept-Language"].ToString();
return string.IsNullOrEmpty(lang) ? "*" : lang;
}
}

protected StatusCodeResult NotAllowed()
{
return StatusCode(403);
}

protected ObjectResult NotAllowed(object value)
{
return StatusCode(403, value);
}

protected StatusCodeResult NotAuthorized()
{
return StatusCode(401);
}

protected ObjectResult NotAuthorized(object value)
{
return StatusCode(401, value);
}

protected string GetControllerName()
{
return $"{{\"Controller:\":\"{this.GetType().FullName}\"}}";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
using foodplanner_api.Models;
using foodplanner_api.Service;
using FoodplannerModels.Account;
using FoodplannerServices;
using FoodplannerServices.Account;
using Microsoft.AspNetCore.Mvc;

namespace foodplanner_api.Controller;
namespace FoodplannerApi.Controller;

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase {
public class UsersController : BaseController {
private readonly UserService _userService;

public UsersController(UserService userService){
_userService = userService;
}

[HttpGet]
public async Task<IActionResult> GetAllUsers(){
public async Task<IActionResult> GetAll(){
var users = await _userService.GetAllUsersAsync();
return Ok(users);
}

[HttpGet("{id}")]
public async Task<IActionResult> GetUsersById(int id){
var users = await _userService.GetAllUsersByIdAsync(id);
public async Task<IActionResult> Get(int id){
var users = await _userService.GetUserByIdAsync(id);
if (User == null){
return NotFound();
}
return Ok(users);
}

[HttpPost]
public async Task<IActionResult> CreateUser([FromBody] User user){
public async Task<IActionResult> Create([FromBody] User user){
var result = await _userService.CreateUserAsync(user);
if (result > 0){
return CreatedAtAction(nameof(GetUsersById), new { id = user.Id }, user);
return CreatedAtAction(nameof(Get), new { id = user.Id }, user);
}
return BadRequest();
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateUser(int id, [FromBody] User user){
public async Task<IActionResult> Update(int id, [FromBody] User user){
if (id != user.Id){
return BadRequest();
}
Expand All @@ -50,7 +49,7 @@ public async Task<IActionResult> UpdateUser(int id, [FromBody] User user){
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteUser(int id){
public async Task<IActionResult> Delete(int id){
var result = await _userService.DeleteUserAsync(id);
if (result > 0){
return NoContent();
Expand Down
26 changes: 26 additions & 0 deletions FoodplannerApi/FoodplannerApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>FoodplannerApi</RootNamespace>
<UserSecretsId>5fd097b4-5a48-43fe-892a-2924ca909938</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Infisical.Sdk" Version="2.3.7" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Npgsql" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FoodplannerDataAccessSql\FoodplannerDataAccessSql.csproj" />
<ProjectReference Include="..\FoodplannerServices\FoodplannerServices.csproj" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions FoodplannerApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using FoodplannerApi;
using Npgsql;
using FoodplannerDataAccessSql.Account;
using FoodplannerDataAccessSql;
using FoodplannerModels;
using FoodplannerModels.Account;
using FoodplannerServices;
using FoodplannerServices.Account;

var builder = WebApplication.CreateBuilder(args);

//Add environment variables for Infisical and configure SecretsLoader
builder.Configuration.AddEnvironmentVariables(prefix: "INFISICAL_");
SecretsLoader.Configure(builder.Configuration, builder.Environment.EnvironmentName);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();



builder.Services.AddSingleton(serviceProvider => {
var host = SecretsLoader.GetSecret("DB_HOST");
var port = SecretsLoader.GetSecret("DB_PORT");
var database = SecretsLoader.GetSecret("DB_NAME");
var username = SecretsLoader.GetSecret("DB_USER");
var password = SecretsLoader.GetSecret("DB_PASS");

return new PostgreSQLConnectionFactory(host, port, database, username, password);
});

builder.Services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
builder.Services.AddScoped(typeof(IUserRepository), typeof(UserRepository));

builder.Services.AddScoped<UserService>();
builder.Services.AddAutoMapper(typeof(UserProfile));

builder.Services.AddControllers();

var app = builder.Build();



// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.MapControllers();

app.MapGet("/test", () => {
var connectionString = SecretsLoader.GetSecret("DB_CONNECTION_STRING");
return Results.Text($"Connection string: {connectionString}");
})
.WithName("GetTest")
.WithOpenApi();


// New endpoint to test database connection
app.MapGet("/test-db-connection", async (PostgreSQLConnectionFactory connectionFactory) => {
try
{
using (var connection = connectionFactory.Create())
{
await connection.OpenAsync();
return Results.Ok("Database connection successful.");
}
}
catch (NpgsqlException ex)
{
return Results.Problem($"Database connection failed: {ex.Message}");
}
})
.WithName("TestDbConnection")
.WithOpenApi();

// Configure the application to listen on all network interfaces
app.Urls.Add("http://0.0.0.0:80");

app.Run();
Loading

0 comments on commit 809f491

Please sign in to comment.