Seif Lotfy: Playing with .NET (dotnet) and IronFunctions |
Again if you missed it, IronFunctions is open-source, lambda compatible, on-premise, language agnostic, server-less compute service.
While AWS Lambda only supports Java, Python and Node.js, Iron Functions allows you to use any language you desire by running your code in containers.
With Microsoft being one of the biggest players in open source and .NET going cross-platform it was only right to add support for it in the IronFunctions's fn tool.
The following demos a .NET function that takes in a URL for an image and generates a MD5 checksum hash for it:
Make sure you downloaded and installed dotnet. Now create an empty dotnet project in the directory of your function:
dotnet new
By default dotnet creates a Program.cs
file with a main method. To make it work with IronFunction's fn
tool please rename it to func.cs
.
mv Program.cs func.cs
Now change the code as you desire to do whatever magic you need it to do. In our case the code takes in a URL for an image and generates a MD5 checksum hash for it. The code is the following:
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
// if nothing is being piped in, then exit
if (!IsPipedInput())
return;
var input = Console.In.ReadToEnd();
var stream = DownloadRemoteImageFile(input);
var hash = CreateChecksum(stream);
Console.WriteLine(hash);
}
private static bool IsPipedInput()
{
try
{
bool isKey = Console.KeyAvailable;
return false;
}
catch
{
return true;
}
}
private static byte[] DownloadRemoteImageFile(string uri)
{
var request = System.Net.WebRequest.CreateHttp(uri);
var response = request.GetResponseAsync().Result;
var stream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
return ms.ToArray();
}
}
private static string CreateChecksum(byte[] stream)
{
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash(stream);
var sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < hash.Length; i++)
{
sBuilder.Append(hash[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
}
}
Note: IO with an IronFunction is done via stdin and stdout. This code
Let's first init our code to become IronFunctions deployable:
fn init /
Since IronFunctions relies on Docker to work (we will add rkt support soon) the
is required to publish to docker hub. The
is the identifier of the function.
In our case we will use dotnethash
as the
, so the command will look like:
fn init seiflotfy/dotnethash
When running the command it will create the func.yaml
file required by functions, which can be built by running:
fn push
This will create a docker image and push the image to docker.
To publish to IronFunctions run ...
fn routes create
where is (no surprise here) the name of the app, which can encompass many functions.
This creates a full path in the form of http://
In my case, I will call the app myapp
:
fn routes create myapp
Now you can
fn call
or
curl http://:/r//
So in my case
echo http://lorempixel.com/1920/1920/ | fn call myapp /dotnethash
or
curl -X POST -d 'http://lorempixel.com/1920/1920/' http://localhost:8080/r/myapp/dotnethash
You can find the whole code in the examples on GitHub. Feel free to join the Iron.io Team on Slack.
Feel free to write your own examples in any of your favourite programming languages such as Lua or Elixir and create a PR :)
http://seif.codes/playing-with-net-dotnet-and-ironfunctions/
Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |