Skip to content

Commit

Permalink
feat: initial TextOutputFormatter impl & ASP.NET Core API example
Browse files Browse the repository at this point in the history
  • Loading branch information
berezovskyi committed Nov 9, 2024
1 parent bcb3565 commit a80048c
Show file tree
Hide file tree
Showing 16 changed files with 506 additions and 21 deletions.
30 changes: 30 additions & 0 deletions OSLC4Net_SDK/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
3 changes: 2 additions & 1 deletion OSLC4Net_SDK/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<ItemGroup>
<!-- For removal -->
<PackageVersion Include="log4net" Version="2.0.17" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageVersion Include="NuGet.Build.Tasks.Pack" Version="6.11.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="System.Json" Version="4.7.1" />

<!-- depedencyManagement / convergence -->
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
Expand All @@ -40,5 +40,6 @@
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace OSLC4NetExamples.Server.NetCoreApi.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
[Route("oslc")]
public OSLC4Net.Core.Model.ServiceProvider Oslc()
{
var sp = new OSLC4Net.Core.Model.ServiceProvider();
sp.SetAbout(new Uri(Request.GetEncodedUrl()));
sp.SetDescription("test me");
return sp;
}

[HttpGet(Name = "GetWeatherForecast")]
[Route("forecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Examples/Directory.Build.props", "Examples/"]
COPY ["Examples/OSLC4NetExamples.Server.NetCoreApi/OSLC4NetExamples.Server.NetCoreApi.csproj", "Examples/OSLC4NetExamples.Server.NetCoreApi/"]
RUN dotnet restore "./Examples/OSLC4NetExamples.Server.NetCoreApi/OSLC4NetExamples.Server.NetCoreApi.csproj"
COPY . .
WORKDIR "/src/Examples/OSLC4NetExamples.Server.NetCoreApi"
RUN dotnet build "./OSLC4NetExamples.Server.NetCoreApi.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./OSLC4NetExamples.Server.NetCoreApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "OSLC4NetExamples.Server.NetCoreApi.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>9d68bd64-651d-4baa-9904-6369c55b51cc</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
<PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OSLC4Net.Server.Providers\OSLC4Net.Server.Providers.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@OSLC4NetExamples.Server.NetCoreApi_HostAddress = http://localhost:5065

GET {{OSLC4NetExamples.Server.NetCoreApi_HostAddress}}/weatherforecast/
Accept: application/json

###
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using OSLC4Net.Server.Providers;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(options =>
{
options.InputFormatters.Insert(0, new OslcRdfInputFormatter());
options.OutputFormatters.Insert(0, new OslcRdfOutputFormatter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

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

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5065"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7270;http://localhost:5065"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:23478",
"sslPort": 44387
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace OSLC4NetExamples.Server.NetCoreApi;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,14 @@ private Type GetMemberType(Type type)
return null;
}

private static bool ImplementsGenericType(Type genericType, Type typeToTest)
public static bool ImplementsGenericType(Type genericType, Type typeToTest)
{
var isParentGeneric = genericType.IsGenericType;

return ImplementsGenericType(genericType, typeToTest, isParentGeneric);
}

private static bool ImplementsGenericType(Type genericType, Type typeToTest,
public static bool ImplementsGenericType(Type genericType, Type typeToTest,
bool isParentGeneric)
{
if (typeToTest == null)
Expand All @@ -449,7 +449,7 @@ private static bool ImplementsGenericType(Type genericType, Type typeToTest,
return ImplementsGenericType(genericType, typeToTest.BaseType, isParentGeneric);
}

private static Type[] GetChildClassParameterArguments(Type genericType, Type typeToTest)
public static Type[] GetChildClassParameterArguments(Type genericType, Type typeToTest)
{
var isParentGeneric = genericType.IsGenericType;

Expand All @@ -469,12 +469,12 @@ private static Type[] GetChildClassParameterArguments(Type genericType, Type typ
}
}

private static bool ImplementsICollection(Type type)
public static bool ImplementsICollection(Type type)
{
return type.IsGenericType && typeof(ICollection<>) == type.GetGenericTypeDefinition();
}

private class NonClosingStreamWriter : StreamWriter
public class NonClosingStreamWriter : StreamWriter
{
public NonClosingStreamWriter(Stream stream)
: base(stream)
Expand Down
Loading

0 comments on commit a80048c

Please sign in to comment.