Skip to content

Instantly share code, notes, and snippets.

View ninjarobot's full-sized avatar

Dave Curylo ninjarobot

  • Microsoft @Azure
  • Atlanta, GA
View GitHub Profile
@ninjarobot
ninjarobot / littlesuave.sh
Created August 22, 2018 13:35
Script to make a build a tiny running Suave server
#!/bin/bash
dotnet new console -n littleSuaveApp -lang F#;
cd littleSuaveApp;
dotnet add package suave;
awk '/open System/{print "open Suave"}; /0/{print " startWebServer { defaultConfig with bindings = [ HttpBinding.createSimple HTTP \"0.0.0.0\" 8080 ] } (Successful.OK \"Hello World!\")"}1' Program.fs > tmp.fs;
mv tmp.fs Program.fs;
dotnet publish -c Release;
@ninjarobot
ninjarobot / ExampleTypedWebParts.md
Last active January 17, 2019 13:24
Composing web handlers with type safety in Suave or Giraffe

Type Safe Composition of HTTP Handlers in Suave and Giraffe

Suave and Giraffe are functional web frameworks that work based on composition of handlers. A handler is a function that accepts an HttpContext and returns an Async<HttpContext option>, fitting nicely into an HTTP server's protocol of accepting HTTP messages with a request and some metadata, then returning a message with that metadata and a response. This makes the WebPart in Suave, for example.

While the frameworks themselves only allow you to put a WebPart into the processing pipeline, it is entirely up to you how you compose functions together to get that WebPart. When you compose WebPart A and WebPart B (or HttpHandler A and HttpHandler B), you are using the a compose operator - >=>. Take a look at the function:

let compose (first : 'a -> Async<'b option>) (second : 'b -> Async
@ninjarobot
ninjarobot / cracklib-from-fsharp.fs
Created September 23, 2019 14:52
Using cracklib from F#
open System
open System.Runtime.InteropServices
[<DllImport("libcrack.so.2", CallingConvention=CallingConvention.Cdecl)>]
extern nativeint FascistCheck(string pw, string dictPath)
[<EntryPoint>]
let main argv =
while true do
Console.WriteLine "Enter a potential password:"
@ninjarobot
ninjarobot / deploy.json
Created November 26, 2019 16:17
Azure Container with Managed Identity and Private Network
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vnetName": {
"type": "String",
"defaultValue": "priv-net"
},
"addressPrefix": {
"type": "String",
@ninjarobot
ninjarobot / azurts-fsadvent-post.md
Last active December 13, 2019 23:44
Azure Alerts to Slack with F# and azurts!

Azure Alerts to Slack with F#

If you have applications in Azure, there is a good chance you're making use of Azure Monitor with Application Insights or Log Analytics. These can provide useful diagnostic information, healthchecks, and alerting for VM's, serverless function apps, containers, and other services. The alerts can send email and call webhooks, but for the most flexibility, delivering them to an Azure Function allows for robust processing with custom F# code. With the azurts library, this can be used to filter alerts based on content and deliver them to different Slack channels.

Azure to Slack

Composing a Webhook

Azurts uses a "railway oriented programming" technique that is popular in several F# libraries to compose a series of ho

@ninjarobot
ninjarobot / DivisionBySubtraction.fs
Created January 13, 2020 21:00
Division by subtraction example in F#
type DivisionProblem =
{
Dividend : int
Divisor : int
}
type DivisionSolution =
{
Quotient : int
Remainder : int
Divisor : int
@ninjarobot
ninjarobot / deployment_cleanup.sh
Created February 28, 2020 20:55
Cleanup ARM deployments
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: deployment-cleanup.sh <subscription_id> <resource_group>"
exit 1
fi
az group deployment list --subscription $1 -g $2 --query '[].{name:name,ts:properties.timestamp}[*].name' -o tsv | tail -n +100 | xargs -n1 az group deployment delete --subscription $1 -g $2 -n
@ninjarobot
ninjarobot / RoadToFSharp.md
Last active June 2, 2020 09:21
How I ended up as an F# developer.

My Road to F#

I came from the OOP world, most of my professional work was in Java or C#. The languages were similar, and I could use them interchangably as I built software in each of them for several years. With enough experience in both, I was comfortable with either ecosystem and could generally be productive in either. Sometimes the job required one or the other. Sometimes the target OS constrained my choice to Java, which frustrated me a bit, because the languages were so similar, why would the runtime try to restrict a choice? I enjoyed writing server applications on Unix in college, and Linux afterwards, because the machines were so easy to configure compared to Windows machines that took a few hours to install and configure to get my software running. On Linux, I could type a handful of commands into a newly installed system and be up and running.

Sometime around 2005, Java had stagnated while C# was flourishing. They both released generics, but then C# came out with new features like

@ninjarobot
ninjarobot / ACI_in_VNet_with_MSI.json
Last active October 13, 2020 14:03
Creating an Azure container instance in a vnet using managed identity
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"resources": [
{
"apiVersion": "2018-11-30",
"dependsOn": [],
"location": "eastus",
@ninjarobot
ninjarobot / etcdDeployment.fsx
Created November 22, 2020 13:48
Deploying etcd on Azure with Farmer
open System
open Farmer
open Farmer.Builders
let deploymentLocation = Location.EastUS
let etcdDataStorage = storageAccount {
name "etcddata"
add_file_share_with_quota "data" 5<Gb>
}