6

New module: Build Serverless apps with Azure and Go

 2 years ago
source link: https://techcommunity.microsoft.com/t5/azure-developer-community-blog/new-module-build-serverless-apps-with-azure-and-go/ba-p/2662657?WT_mc_id=DOP-MVP-4025064
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

New module: Build Serverless apps with Azure and Go

Published Aug 18 2021 01:53 PM 1,108 Views

Serverless architecture is a type of application development that allows you to run logic in the cloud without having to worry about building server infrastructure. Azure Functions implements a serverless architecture that runs your code on demand without requiring you to manually provision servers.

To author Azure Functions in Go or Rust, for example, you use a feature called custom handlers. Custom handlers allow you to bring almost any language to Azure Functions.

What are custom handlers?

At its core, a custom handler is a web server. The web server receives events from the Functions host. You then have an opportunity to write code in your preferred language to respond to the events.

With custom handlers, you can use any language that supports HTTP primitives. That's nearly any language.

An Azure Function app using Go

To create such an app, you only need to listen to HTTP events + make some minor configurations. Here's an example of what the code looks like:

package main

import (
 "fmt"
 "io/ioutil"
 "log"
 "net/http"
 "os"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "application/json")
  if r.Method == "GET" {
    w.Write([]byte("hello world"))
  } else {
    body, _ := ioutil.ReadAll(r.Body)
    w.Write(body)
  }
}

func main() {
  customHandlerPort, exists := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT")
  if !exists {
    customHandlerPort = "8080"
  }
  mux := http.NewServeMux()
  // mux.HandleFunc("/api/hello", helloHandler)
  fmt.Println("Go server Listening on: ", customHandlerPort)
  log.Fatal(http.ListenAndServe(":"+customHandlerPort, mux))
}

To learn more about how to configure the app properly and work with things like message queues, have a look at this module

Build serverless apps with Go and custom handlers - Learn | Microsoft Docs

You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.

%3CLINGO-SUB%20id%3D%22lingo-sub-2662657%22%20slang%3D%22en-US%22%3ENew%20module%3A%20Build%20Serverless%20apps%20with%20Azure%20and%20Go%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2662657%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EServerless%20architecture%20is%20a%20type%20of%20application%20development%20that%20allows%20you%20to%20run%20logic%20in%20the%20cloud%20without%20having%20to%20worry%20about%20building%20server%20infrastructure.%20Azure%20Functions%20implements%20a%20serverless%20architecture%20that%20runs%20your%20code%20on%20demand%20without%20requiring%20you%20to%20manually%20provision%20servers.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ETo%20author%20Azure%20Functions%20in%20Go%20or%20Rust%2C%20for%20example%2C%20you%20use%20a%20feature%20called%20custom%20handlers.%20Custom%20handlers%20allow%20you%20to%20bring%20almost%20any%20language%20to%20Azure%20Functions.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22what-are-custom-handlers%22%20id%3D%22toc-hId--380210312%22%20id%3D%22toc-hId--380210287%22%3EWhat%20are%20custom%20handlers%3F%3C%2FH2%3E%0A%3CP%3EAt%20its%20core%2C%20a%20custom%20handler%20is%20a%20web%20server.%20The%20web%20server%20receives%20events%20from%20the%20Functions%20host.%20You%20then%20have%20an%20opportunity%20to%20write%20code%20in%20your%20preferred%20language%20to%20respond%20to%20the%20events.%3C%2FP%3E%0A%3CP%3EWith%20custom%20handlers%2C%20you%20can%20use%20any%20language%20that%20supports%20HTTP%20primitives.%20That's%20nearly%20any%20language.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2107302521%22%20id%3D%22toc-hId-2107302546%22%3EAn%20Azure%20Function%20app%20using%20Go%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20create%20such%20an%20app%2C%20you%20only%20need%20to%20listen%20to%20HTTP%20events%20%2B%20make%20some%20minor%20configurations.%20Here's%20an%20example%20of%20what%20the%20code%20looks%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Epackage%20main%0A%0Aimport%20(%0A%20%22fmt%22%0A%20%22io%2Fioutil%22%0A%20%22log%22%0A%20%22net%2Fhttp%22%0A%20%22os%22%0A)%0A%0Afunc%20helloHandler(w%20http.ResponseWriter%2C%20r%20*http.Request)%20%7B%0A%20%20w.Header().Set(%22Content-Type%22%2C%20%22application%2Fjson%22)%0A%20%20if%20r.Method%20%3D%3D%20%22GET%22%20%7B%0A%20%20%20%20w.Write(%5B%5Dbyte(%22hello%20world%22))%0A%20%20%7D%20else%20%7B%0A%20%20%20%20body%2C%20_%20%3A%3D%20ioutil.ReadAll(r.Body)%0A%20%20%20%20w.Write(body)%0A%20%20%7D%0A%7D%0A%0Afunc%20main()%20%7B%0A%20%20customHandlerPort%2C%20exists%20%3A%3D%20os.LookupEnv(%22FUNCTIONS_CUSTOMHANDLER_PORT%22)%0A%20%20if%20!exists%20%7B%0A%20%20%20%20customHandlerPort%20%3D%20%228080%22%0A%20%20%7D%0A%20%20mux%20%3A%3D%20http.NewServeMux()%0A%20%20%2F%2F%20mux.HandleFunc(%22%2Fapi%2Fhello%22%2C%20helloHandler)%0A%20%20fmt.Println(%22Go%20server%20Listening%20on%3A%20%22%2C%20customHandlerPort)%0A%20%20log.Fatal(http.ListenAndServe(%22%3A%22%2BcustomHandlerPort%2C%20mux))%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20learn%20more%20about%20how%20to%20configure%20the%20app%20properly%20and%20work%20with%20things%20like%20message%20queues%2C%20have%20a%20look%20at%20this%20module%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Flearn%2Fmodules%2Fserverless-go%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EBuild%20serverless%20apps%20with%20Go%20and%20custom%20handlers%20-%20Learn%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2662657%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3ETo%20author%20Azure%20Functions%20in%20Go%20or%20Rust%2C%20for%20example%2C%20you%20use%20a%20feature%20called%20custom%20handlers.%20Custom%20handlers%20allow%20you%20to%20bring%20almost%20any%20language%20to%20Azure%20Functions.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2662657%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFunctions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E

Version history
Last update:

‎Aug 18 2021 01:53 PM

Updated by:
Labels

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK