Skip to content

Commit

Permalink
Merge pull request #620 from caioavidal/feature/webapi-improvements
Browse files Browse the repository at this point in the history
feat: webapi improvements
  • Loading branch information
caioavidal authored Dec 20, 2024
2 parents 31abf5c + 9a31bc3 commit 9005391
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 278 deletions.
17 changes: 11 additions & 6 deletions data/.env.example → .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ POSTGRESQL_HOST=postgres
POSTGRESQL_PORT=5432
POSTGRESQL_PGAGMIN_EMAIL=postgres@mail.com

# Server
# Server - Databases can be (SQLITE, INMEMORY, POSTGRESQL)
ACTIVE_DATABASE=POSTGRESQL
SERVER_LOGIN_PORT=7181
SERVER_GAME_PORT=7182
SERVER_LOGIN_PORT=7171
SERVER_GAME_PORT=7172
SERVER_GAME_NAME=OpenCoreMMO
SERVER_GAME_IP=127.0.0.1

# GrayLog
GRAYLOG_HOSTNAME_OR_ADDRESS=127.0.0.1
# Webapi
WEBAPI_CERTIFICATE_PASSWORD=opencoremmo
WEBAPI_CERTIFICATE_PATH=/app/Certificates/aspnetapp.pfx

# GrayLog - SHA2 Password = admin
GRAYLOG_HOSTNAME_OR_ADDRESS=graylog
GRAYLOG_PORT=12201
GRAYLOG_HOSTNAME_OVERRIDE=OpenCoreMMO
GRAYLOG_FACILITY=Standalone
GRAYLOG_FACILITY_STANDALONE=Standalone
GRAYLOG_FACILITY_WEBAPI=WebAPI
GRAYLOG_SECRET=somepasswordpepper
GRAYLOG_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ c4/icon.png
/src/GameWorldSimulator/NeoServer.Game.Items/Properties/launchSettings.json
/src/Loaders/NeoServer.OTBM/Properties/launchSettings.json
/data/.env
*.env
55 changes: 43 additions & 12 deletions compose.server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ services:
dockerfile: ./src/Standalone/Dockerfile
environment:
ASPNETCORE_ENVIRONMENT: "Production"
DOCKER: true
SQLITE_DATABASE: "${SQLITE_DATABASE}"
POSTGRESQL_DATABASE: "${POSTGRESQL_DATABASE}"
POSTGRESQL_USER: "${POSTGRESQL_USER}"
POSTGRESQL_PASSWORD: "${POSTGRESQL_PASSWORD}"
POSTGRESQL_HOST: "${POSTGRESQL_HOST}"
POSTGRESQL_PORT: "${POSTGRESQL_PORT}"
ACTIVE_DATABASE: "${ACTIVE_DATABASE}"
SERVER_LOGIN_PORT: "${SERVER_LOGIN_PORT}"
SERVER_GAME_PORT: "${SERVER_GAME_PORT}"
SERVER_GAME_NAME: "${SERVER_GAME_NAME}"
SERVER_GAME_IP: "${SERVER_GAME_IP}"
GRAYLOG_HOSTNAME_OR_ADDRESS: "${GRAYLOG_HOSTNAME_OR_ADDRESS}"
GRAYLOG_PORT: "${GRAYLOG_PORT}"
GRAYLOG_HOSTNAME_OVERRIDE: "${GRAYLOG_HOSTNAME_OVERRIDE}"
GRAYLOG_FACILITY: "${GRAYLOG_FACILITY_STANDALONE}"
ports:
- "${SERVER_LOGIN_PORT}:${SERVER_LOGIN_PORT}"
- "${SERVER_GAME_PORT}:${SERVER_GAME_PORT}"
Expand All @@ -33,17 +47,34 @@ services:
depends_on:
- postgres

# webapi-compose:
# container_name: webapi
# build:
# context: ./src/WebAPI/
# dockerfile: Dockerfile
# environment:
# ASPNETCORE_ENVIRONMENT: "Production"
# ports:
# - "80:80"
# networks:
# - opencoremmo
webapi:
container_name: webapi
build:
context: .
dockerfile: ./src/WebAPI/Dockerfile
environment:
ASPNETCORE_ENVIRONMENT: "Production"
ASPNETCORE_URLS: "https://+;http://+"
ASPNETCORE_Kestrel__Certificates__Default__Password: "${WEBAPI_CERTIFICATE_PASSWORD}"
ASPNETCORE_Kestrel__Certificates__Default__Path: "${WEBAPI_CERTIFICATE_PATH}"
SQLITE_DATABASE: "${SQLITE_DATABASE}"
POSTGRESQL_DATABASE: "${POSTGRESQL_DATABASE}"
POSTGRESQL_USER: "${POSTGRESQL_USER}"
POSTGRESQL_PASSWORD: "${POSTGRESQL_PASSWORD}"
POSTGRESQL_HOST: "${POSTGRESQL_HOST}"
POSTGRESQL_PORT: "${POSTGRESQL_PORT}"
ACTIVE_DATABASE: "${ACTIVE_DATABASE}"
GRAYLOG_HOSTNAME_OR_ADDRESS: "${GRAYLOG_HOSTNAME_OR_ADDRESS}"
GRAYLOG_PORT: "${GRAYLOG_PORT}"
GRAYLOG_HOSTNAME_OVERRIDE: "${GRAYLOG_HOSTNAME_OVERRIDE}"
GRAYLOG_FACILITY: "${GRAYLOG_FACILITY_WEBAPI}"
ports:
- "443:443"
- "80:80"
networks:
- opencoremmo
depends_on:
- postgres

networks:
opencoremmo:
Expand Down
24 changes: 0 additions & 24 deletions src/Shared/NeoServer.IoC/Modules/EnvironmentInjection.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Standalone/IoC/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static IServiceProvider BuildConfigurations()
var configuration = ConfigurationInjection.GetConfiguration();

builder
.AddEnvironments()
.AddConfigurations(configuration)
.AddLogger(configuration);

Expand Down
Binary file added src/WebAPI/Certificates/aspnetapp.pfx
Binary file not shown.
33 changes: 16 additions & 17 deletions src/WebAPI/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
ARG ASPNET_VERSION="9.0"
ARG SDK_VERSION="9.0"
ARG BASE_ADRESS="mcr.microsoft.com/dotnet"
# Use an official .NET 9 SDK as a parent image
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build

FROM $BASE_ADRESS/aspnet:$ASPNET_VERSION AS base
# Set the working directory to /app
WORKDIR /app

EXPOSE 80
# Copy the project files to the container
COPY src/. .

FROM $BASE_ADRESS/sdk:$SDK_VERSION AS build
# Restore dependencies
RUN dotnet restore WebAPI/NeoServer.Web.API.csproj

WORKDIR /src
# Build the project
RUN dotnet publish WebAPI/NeoServer.Web.API.csproj -c Release -o out

COPY *.csproj ./WebAPI
# Use an official .NET 9 runtime as a parent image
FROM mcr.microsoft.com/dotnet/aspnet:9.0

RUN dotnet restore -v m ./WebAPI

WORKDIR /src/WebAPI
RUN dotnet build NeoServer.Web.API.csproj -c Release -o /app/build --no-restore
# Set the working directory to /app
WORKDIR /app

FROM build AS publish
RUN dotnet publish NeoServer.Web.API.csproj -c Release -o /app/publish --no-restore
# Copy the published app to the container
COPY --from=build /app/out .

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# Set the entrypoint to run the console app
ENTRYPOINT ["dotnet", "NeoServer.Web.API.dll"]
59 changes: 34 additions & 25 deletions src/WebAPI/NeoServer.Web.API.csproj
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>c71a823c-0b62-4db1-9942-ad2bf4c080b0</UserSecretsId>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>c71a823c-0b62-4db1-9942-ad2bf4c080b0</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0"/>
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0"/>
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.JsonMultipartFormDataSupport" Version="2.0.0"/>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="NeoServer.WebApi.Tests" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.JsonMultipartFormDataSupport" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\NeoServer.IoC\NeoServer.Shared.IoC.csproj"/>
<ProjectReference Include="..\Shared\NeoServer.Web.Shared\NeoServer.Web.Shared.csproj"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\NeoServer.IoC\NeoServer.Shared.IoC.csproj" />
<ProjectReference Include="..\Shared\NeoServer.Web.Shared\NeoServer.Web.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Certificates\aspnetapp.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
113 changes: 97 additions & 16 deletions src/WebAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,104 @@
using Serilog;
using System.Net;
using Microsoft.OpenApi.Models;
using NeoServer.Shared.IoC.Modules;
using NeoServer.Web.API.HttpFilters;
using NeoServer.Web.API.IoC.Modules;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Extensions;
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Integrations;

namespace NeoServer.Web.API;
var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
var environment = builder.Environment;

public static class Program
// Configure appsettings
builder.Configuration
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();

// Add services to the container
var services = builder.Services;

services.AddHttpContextAccessor();
services.AddBehaviours();
services.AddServicesApi();
services.AddAutoMapperProfiles();

services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardLimit = 2;
options.KnownProxies.Add(IPAddress.Parse("127.0.0.1"));
options.KnownProxies.Add(IPAddress.Parse("0.0.0.0"));
options.ForwardedForHeaderName = "X-Forwarded-For-My-Custom-Header-Name";
});

services.AddSwaggerGen(c =>
{
public static void Main(string[] args)
c.SwaggerDoc("v1", new OpenApiInfo { Title = "NeoServer.API", Version = "v1" });

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
CreateHostBuilder(args).Build().Run();
}
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});

private static IHostBuilder CreateHostBuilder(string[] args)
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); })
.ConfigureLogging(logging =>
{
new OpenApiSecurityScheme
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseSerilog();
}
}
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header
},
new List<string>()
}
});

c.OperationFilter<SwaggerJsonIgnoreFilter>();
});

services.AddJsonMultipartFormDataSupport(JsonSerializerChoice.Newtonsoft);

services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
});

services.AddLogger(configuration);
services.AddDatabases(configuration);
services.AddRepositories();

var app = builder.Build();

// Configure the HTTP request pipeline
app.UseSwagger();
app.UseSwaggerUI();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());

app.MapControllers();

app.Run();

public partial class Program { }
Loading

0 comments on commit 9005391

Please sign in to comment.