Skip to content

Azure-Samples/azure-function-custom-handler-with-golang

Repository files navigation

azure-function-custom-handler-with-golang

This sample shows how to host multiple Azure functions in Golang. To learn more about this sample please check out the full description in this blog article.

readme-azf-design-diagram

There is a function "drink" which can be used to create, update, or delete your favorite drink, and a "drinks" method to retrieve all the previously saved drinks.

How to run this project

It is recommended that you use a devcontainer.

Prerequisites

  1. Docker is installed and running on your machine
  2. Visual Studio Code(VScode) is installed as a code editor

Launch devcontainer

  1. Make sure your docker is running
  2. Open this project with VScode
  3. Click the green icon in bottom left labelled "Open in Container"

Build the project

  1. Open the terminal in VScode
  2. Run make build
  3. Make sure the app file is created the root folder

Run the project

  1. Open the terminal in VScode
  2. Run make start
  3. Comfirm the endpoint URLs appear in the terminal

Send a HTTP request

  1. Sample request for /drink endpoint

    ## POST
    curl --location --request POST 'http://localhost:7071/api/drink' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "name":"Water",
        "price":1
    }'
    
    ## GET
    curl --location --request GET 'http://localhost:7071/api/drink' \
    --header 'id: <uuid>'
    
    ## DELETE
    curl --location --request DELETE 'http://localhost:7071/api/drink' \
    --header 'id: <uuid>'
  2. Sample request for /drinks endpoint

    ## GET
    curl --location --request GET 'http://localhost:7071/api/drinks'

Run unit tests

  1. Open the terminal in VScode
  2. Run make test
  3. Run make coverage

Run lint

  1. Open the terminal in VScode
  2. Run make lint

Deploy

There are two different methods to deploy this project.

Method 1: Using VSCode

Please follow Publish the project to Azure

Method 2: Using a Pipeline

This repo contains yaml pipelines for Github, Azure DevOps and Gitlab. The pipelines folder contains the yaml for Github, Azure Pipelines and Gitlab. Please choose the appropriate yaml for your choice of platform.

To deploy this project using a pipeline, you need to set up the Azure Function in Azure yourself in advance.

How to set up a custom handler Azure Function

Start by searching for Function App in the Azure Portal and click Create. The important settings for this are below, other settings you can use default or your own preferences.

[Basic]

  1. Publish: Code
  2. Runtime stack: Custom Handler
  3. Version: custom

[Hosting]

  1. Operating System: Linux
  2. Plan type: Consumption (Serverless)

For some of these inputs it is important to keep them secure, so please consider using an secret management tool or service such as Azure Key Vault.

Github

  1. Replace the .github folder and its contents with pipelines/.github
  2. Add credentials in Github secret
    1. Create a Service Principal and Add the Service Principal in Secrets as AZURE_CREDENTIALS
    2. Add the Azure function name in Secrets as FUNCTION_APP

Azure DevOps

  1. Update variables in azure-pipelines.yml
    1. $(AZURE_SUBSCRIPTION) with your subscription (format: subscription_name(subscription_id))
    2. $(FUNCTION_APP) with the name of your Azure function
  2. In Azure DevOps, Navigate to Pipelines > New pipeline
    1. Connect: Azure Repos Git(YAML) option
    2. Select: Your repo
    3. Configure: Existing Azure Pipelines YAML file
    4. Set main branch and /pipeline/azure-pipelines.yml path
    5. Hit Run button. You may see "Permission needed" error at the first time. If you have this error, please grant permission to the subscription.

Gitlab

  1. In Gitlab, Navigate to Settings > CI/CD
    1. In the General pipelines, put pipeline/.gitlab-ci.yml in the CI/CD configuration file textbox.
    2. In Variables, create APPLICATION_ID, APPLICATION_SECRET and TENANT_ID variables. You can find the values in the Azure portal.
  2. In the .gitlab-ci.yml file
    1. Update the REPO_NAME value with your Gitlab repositiory.
    2. Update the FUNCTION_APP value in .gitlab-ci.yml file with the name of your Azure function.

Resources

About

Example of an Azure Function App golang custom handler and multiple functions

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •