Code Monkey home page Code Monkey logo

azure-function-custom-handler-with-golang's Introduction

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

azure-function-custom-handler-with-golang's People

Contributors

microsoft-github-operations[bot] avatar microsoftopensource avatar tomomi-code avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.