Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TechDays 2017 sessie 2 Jan de Vries

4Dotnet
October 19, 2017
40

TechDays 2017 sessie 2 Jan de Vries

Creating real life serverless solutions with Azure Functions

4Dotnet

October 19, 2017
Tweet

Transcript

  1. “If your PaaS can efficiently start instances in 20ms that

    run for half a second, then call it serverless.” Adrian Cockcroft - VP Cloud Architecture Strategy AWS
  2. "bindings": [ { "type": "httpTrigger", "route": "{slug}", "methods": [ "get"

    ], "authLevel": "anonymous", "name": "req" } ], Function, User, System & Admin
  3. "bindings": [ { "name": “minifiedUrl", "type": "documentDB", "databaseName": “MinifyRepository", "collectionName":

    “MinifiedUrls", "createIfNotExists": true, "connection": “CosmosDbConnection", "direction": "out" } ],
  4. private readonly ICosmosClient cosmosClient; public CreateUrlHandler() { this.cosmosClient = new

    CosmosClient(); } public CreateUrlHandler(ICosmosClient cosmosClient) { this.cosmosClient = cosmosClient; }
  5. [FunctionName("Get")] public static async Task<HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route =

    "{slug}")] HttpRequestMessage req, string slug, TraceWriter log) { var getUrl = new GetUrlHandler(); var minifiedUrl = await getUrl.Execute(slug); if (minifiedUrl == null) { return req.CreateErrorResponse(HttpStatusCode.NotFound, $"Minified value `{slug}` is not found."); } var response = req.CreateResponse(HttpStatusCode.Redirect); response.Headers.Location = new Uri(minifiedUrl.FullUrl); return response; }
  6. public static class Create { [FunctionName("Create")] public static HttpResponseMessage Run(

    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "create")] HttpRequestMessage req, [DocumentDB("datbaseName", "collectionName", PartitionKey = "MinifiedUrls", CreateIfNotExists = true, ConnectionStringSetting = "minifiedUrlConnectionString")] out dynamic minifiedUrl, TraceWriter log) { string jsonContent = req.Content.ReadAsStringAsync().Result; var data = JsonConvert.DeserializeObject<MinifiedUrl>(jsonContent); var create = new CreateUrlHandler(); minifiedUrl = create.Execute(data); return req.CreateResponse(HttpStatusCode.Created, $"api/{data.MinifiedSlug}"); } }
  7. { "$schema": "http://json.schemastore.org/proxies", "proxies": { "getminifiedredirect": { "matchCondition": { "methods":

    [ "GET" ], "route": "/{slug}" }, "backendUri": "https://%WEBSITE_HOSTNAME%/api/{slug}" } } } Matching conditions