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.
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.
It is recommended that you use a devcontainer.
- Docker is installed and running on your machine
- Visual Studio Code(VScode) is installed as a code editor
- Make sure your docker is running
- Open this project with VScode
- Click the green icon in bottom left labelled "Open in Container"
- Open the terminal in VScode
- Run
make build
- Make sure the
app
file is created the root folder
- Open the terminal in VScode
- Run
make start
- Comfirm the endpoint URLs appear in the terminal
-
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>'
-
Sample request for
/drinks
endpoint## GET curl --location --request GET 'http://localhost:7071/api/drinks'
- Open the terminal in VScode
- Run
make test
- Run
make coverage
- Open the terminal in VScode
- Run
make lint
There are two different methods to deploy this project.
Please follow Publish the project to Azure
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]
- Publish: Code
- Runtime stack: Custom Handler
- Version: custom
[Hosting]
- Operating System: Linux
- 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.
- Replace the
.github
folder and its contents withpipelines/.github
- Add credentials in Github secret
- Create a Service Principal and Add the Service Principal in Secrets as
AZURE_CREDENTIALS
- Add the Azure function name in Secrets as
FUNCTION_APP
- Create a Service Principal and Add the Service Principal in Secrets as
- Update variables in
azure-pipelines.yml
$(AZURE_SUBSCRIPTION)
with your subscription (format:subscription_name(subscription_id)
)$(FUNCTION_APP)
with the name of your Azure function
- In Azure DevOps, Navigate to Pipelines > New pipeline
- Connect: Azure Repos Git(YAML) option
- Select: Your repo
- Configure: Existing Azure Pipelines YAML file
- Set
main
branch and/pipeline/azure-pipelines.yml
path - Hit
Run
button. You may see "Permission needed" error at the first time. If you have this error, please grant permission to the subscription.
- In Gitlab, Navigate to Settings > CI/CD
- In the General pipelines, put
pipeline/.gitlab-ci.yml
in the CI/CD configuration file textbox. - In Variables, create
APPLICATION_ID
,APPLICATION_SECRET
andTENANT_ID
variables. You can find the values in the Azure portal.
- In the General pipelines, put
- In the
.gitlab-ci.yml
file- Update the
REPO_NAME
value with your Gitlab repositiory. - Update the
FUNCTION_APP
value in.gitlab-ci.yml
file with the name of your Azure function.
- Update the