Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dotnet8/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use the ASP.NET Core 8 base image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app

# Use the .NET 8 SDK image for the build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .

RUN dotnet restore Fission.DotNet/Fission.DotNet.csproj

RUN dotnet publish Fission.DotNet/Fission.DotNet.csproj -c Release -o /app

# Final image
FROM base AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "Fission.DotNet.dll"]
26 changes: 26 additions & 0 deletions dotnet8/Fission.DotNet.Common/Fission.DotNet.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<PackageId>Fission.DotNet.Common</PackageId>
<Version>1.1.0</Version>
<Authors>Lorenzo Caldon</Authors>
<Description>Fission dotnet environment common package</Description>
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
<RepositoryUrl>https://github.com/lcsoft77/fission-env-dotnet8</RepositoryUrl>
<PackageOutputPath>./nupkg</PackageOutputPath>
</PropertyGroup>

<PropertyGroup>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions dotnet8/Fission.DotNet.Common/Fission.DotNet.Common.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fission.DotNet.Common", "Fission.DotNet.Common.csproj", "{66CA3524-4798-6315-18DF-47CD0B936395}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{66CA3524-4798-6315-18DF-47CD0B936395}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66CA3524-4798-6315-18DF-47CD0B936395}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66CA3524-4798-6315-18DF-47CD0B936395}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66CA3524-4798-6315-18DF-47CD0B936395}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF6A70F1-84DB-4983-A581-08BC54DF98A5}
EndGlobalSection
EndGlobal
72 changes: 72 additions & 0 deletions dotnet8/Fission.DotNet.Common/FissionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Text;
using System.Text.Json;

namespace Fission.DotNet.Common;

public class FissionContext
{
protected Stream _content;
protected Dictionary<string, object> _arguments;
protected Dictionary<string, string> _headers;
protected Dictionary<string, string> _parameters;

public FissionContext(Stream body, Dictionary<string, object> arguments, Dictionary<string, string> headers, Dictionary<string, string> parameters)
{
if (body == null) throw new ArgumentNullException(nameof(body));
if (arguments == null) throw new ArgumentNullException(nameof(arguments));
if (headers == null) throw new ArgumentNullException(nameof(headers));
if (parameters == null) throw new ArgumentNullException(nameof(parameters));

_content = body;
_arguments = arguments;
_headers = headers;
_parameters = parameters;
}

protected string GetHeaderValue(string key, string defaultValue = null)
{
return _headers.ContainsKey(key) ? _headers[key] : defaultValue;
}

public Dictionary<string, object> Arguments => _arguments;
public Dictionary<string, string> Parameters => _parameters;

public string TraceID => GetHeaderValue("traceparent", Guid.NewGuid().ToString());
public string FunctionName => GetHeaderValue("X-Fission-Function-Name");
public string Namespace => GetHeaderValue("X-Fission-Function-Namespace");
public string ResourceVersion => GetHeaderValue("X-Fission-Function-Resourceversion");
public string UID => GetHeaderValue("X-Fission-Function-Uid");
public string Trigger => GetHeaderValue("Source-Name");
public string ContentType => GetHeaderValue("Content-Type");
public Int32 ContentLength => GetHeaderValue("Content-Length") != null ? Int32.Parse(GetHeaderValue("Content-Length")) : 0;
public Stream Content => _content;

public async Task<string?> ContentAsString()
{
if (_content == null)
{
return null;
}

_content.Position = 0;
using (StreamReader reader = new StreamReader(_content, Encoding.UTF8, leaveOpen: true))
{
return await reader.ReadToEndAsync();
}
}

public async Task<T> ContentAs<T>(JsonSerializerOptions? options = null)
{
if (_content == null)
{
return default;
}

_content.Position = 0;
using (StreamReader reader = new StreamReader(_content, Encoding.UTF8, leaveOpen: true))
{
string content = await reader.ReadToEndAsync();
return JsonSerializer.Deserialize<T>(content, options);
}
}
}
42 changes: 42 additions & 0 deletions dotnet8/Fission.DotNet.Common/FissionHttpContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Text;
using System.Text.Json;

namespace Fission.DotNet.Common;

public class FissionHttpContext : FissionContext
{
private string _method;

public FissionHttpContext(Stream body, string method, Dictionary<string, object> arguments, Dictionary<string, string> headers, Dictionary<string, string> parameters) : base(body, arguments, headers, parameters)
{
_method = method;
}

public Dictionary<string, string> Headers => _headers;
public string Url
{
get
{
var urlHeader = GetHeaderValue("X-Fission-Full-Url");

if (urlHeader != null)
{
if (urlHeader.Contains("?"))
{
urlHeader = urlHeader.Substring(0, urlHeader.IndexOf("?"));
}

return urlHeader;
}
else
{
return "/";
}
}
}
public string Method => _method;
public string Host => GetHeaderValue("X-Forwarded-Host");
public int Port => _headers.ContainsKey("X-Forwarded-Port") ? Int32.Parse(GetHeaderValue("X-Forwarded-Port")) : 0;
public string UserAgent => GetHeaderValue("User-Agent");
}
15 changes: 15 additions & 0 deletions dotnet8/Fission.DotNet.Common/FissionMqContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Fission.DotNet.Common;

public class FissionMqContext : FissionContext
{
public FissionMqContext(Stream body, Dictionary<string, object> arguments, Dictionary<string, string> headers, Dictionary<string, string> parameters) : base(body, arguments, headers, parameters)
{

}

public string Topic => GetHeaderValue("Topic");
public string ErrorTopic => GetHeaderValue("Errortopic");
public string ResponseTopic => GetHeaderValue("Resptopic");
}
16 changes: 16 additions & 0 deletions dotnet8/Fission.DotNet.Common/ICorsPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Fission.DotNet.Common
{
public interface ICorsPolicy
{
void AllowAnyOrigin();
void AllowAnyHeader();
void AllowAnyMethod();
void AllowCredentials();

void WithOrigin(string[] origin);
void WithHeader(string[] header);
void WithMethod(string[] method);
}
}
14 changes: 14 additions & 0 deletions dotnet8/Fission.DotNet.Common/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Fission.DotNet.Common
{
public interface ILogger
{
void LogInformation(string message);
void LogDebug(string message);
void LogWarning(string message);
void LogError(string message);
void LogCritical(string message);
void LogError(string message, Exception exception);
}
}
46 changes: 46 additions & 0 deletions dotnet8/Fission.DotNet.Common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Fission.DotNet.Common

This project is a common library for the .NET environment of [Fission](https://fission.io/), a serverless plugin for Kubernetes. The library provides common functionalities and utilities to facilitate the development of serverless functions with Fission on .NET.

## Main Features

- **Common Utilities**: Provides a set of common utilities to simplify the development of serverless functions.
- **.NET 8 Compatibility**: Designed to work with **.NET 8 on Linux**, offering an updated and improved experience.
- **Multi-Assembly Management**: Supports projects composed of multiple linked assemblies, simplifying the deployment and integration process.

## Inspiration

This project is inspired by the official Fission environment for .NET Core 2.0 but focuses on improvements and updates requested by the community, allowing developers to work with newer versions of the .NET framework and complex projects that include multiple assemblies.

## Usage

1. **Add the library to your project**:
Add the NuGet package `Fission.DotNet.Common` to your .NET project.

```bash
dotnet add package Fission.DotNet.Common

2. **Create the project**:
- Create a **class library project** in .NET.
- Add the NuGet package `Fission.DotNet.Common` to your project.
- Create a class with the following function:

```csharp
using Fission.DotNet.Common;

public class MyFunction
{
public object Execute(FissionContext input)
{
return "Hello World";
}
}
```
3. **Compression**: Compress the assemblies and related files into a ZIP file.

4. **Deploy to Fission**: Use this library to deploy your project to Fission, leveraging the ability to manage multiple linked assemblies. After compressing your project into a ZIP file, you can create the function in Fission with the following command:

```bash
fission fn create --name <function_name> --env dotnet8 --code <your_project.zip> --entrypoint <name_of_assembly_without_extension>:<namespace>:<classname>
```
Replace `<function_name>` with the name of your function and `<your_project.zip>` with the path to your ZIP file.
44 changes: 44 additions & 0 deletions dotnet8/Fission.DotNet/Adapter/FissionLoggerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Fission.DotNet.Common;

namespace Fission.DotNet.Adapter;

public class FissionLoggerAdapter : Common.ILogger
{
private Microsoft.Extensions.Logging.ILogger _logger;

public FissionLoggerAdapter(Microsoft.Extensions.Logging.ILogger logger)
{
this._logger = logger;
}

public void LogCritical(string message)
{
_logger.LogCritical(message);
}

public void LogDebug(string message)
{
_logger.LogDebug(message);
}

public void LogError(string message, Exception exception)
{
_logger.LogError(exception, message);
}

public void LogError(string message)
{
_logger.LogError(message);
}

public void LogInformation(string message)
{
_logger.LogInformation(message);
}

public void LogWarning(string message)
{
_logger.LogWarning(message);
}
}
Loading