# API for a resume
This project is an API built using (Minimal API for .NET 6
- .NET SDK 6.0.402+
- Tutorial Build a web API with minimal API, ASP.NET Core, and .NET 6
- Documentation: Create a minimal web API with ASP.NET Core
To run the Water consumption API, do the following:
-
Install EF Core tools
dotnet tool install --global dotnet-ef
-
Apply migrations to database (will create the DB, it's tables and give it initial data)
dotnet ef database update
-
Start the API
dotnet run
This API has the following endpoints:
-
/
, default route responding with "hello world" -
/Consumptionsecure
, a route representing a resume, responding with a JSON:[ { "Id": 1, "DateTime" : "", "Consumption": 100 }, { "Id": 2, "DateTime" : "", "Consumption": 110 } ]
-
/swagger
, an Open API endpoint that represents the docs of the API
To deploy this API, there are the following things you need to do:
- Provision of the database.
- Generate SQL scripts from Entity Framework and run those on your provisioned database in Azure.
- Deploy the API to Azure App Service.
- Navigate to Azure portal, https://portal.azure.com
- Provision an Azure SQL Server
- Under said server, provision a database
Make note of the connection string, you will use it later and to your app deployment.
Next, you want the table structure and data from your C# code but in a SQL format. Here's how:
-
Run this command in the console:
dotnet ef migrations script
save the output, you will need to run it in your Azure portal.
-
Navigate to the portal:
portal.azure.com
-
Locate your database resource
-
Select they query editor and login in to your database.
-
Paste the SQL code that you generated earlier, this will create your tables and insert your data.
Last step is open up the Database server
- Go to Networking and check the option "Allow Azure services and resource to access this server"
This will allow your web app on Azure to access the database as it creates a rule in the firewall.
There are two different ways to deploy we will mention, select one of them:
- Azure CLI
- Using Visual Studio Code Azure extension
-
Open appsettings.json and add section like so:
{ "ConnectionStrings": { "DB" : "<replace with Azure DB connection string value>" } }
add the connection string value you copied from Azure database.
-
Add a
secret
property and a value of your choice to the JSON like so:{ "ConnectionStrings": { "DB" : "<replace with Azure DB connection string value>" }, "secret": "<replace with the secret value you want, make it long and complicated>" }
https://learn.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-rest-api
To deploy the API, here's what you need to do:
-
Login with
az
:az login
-
Create a resource group (if you don't already have one)
az group create --name myResourceGroup --location "West Europe"
-
Create an app service plan:
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku FREE
-
Create the web app (acts like a placeholder for your app)
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name "replace with your app name" --deployment-local-git
make a note of the variable value for
deploymentLocalGitUrl
, you will use it later. -
Deploy the code to the app
az webapp config appsettings set --name "replace with your app name" --resource-group myResourceGroup --settings DEPLOYMENT_BRANCH='main'
-
Add an Azure remote
git remote add azure <deploymentLocalGitUrl-from-create-step>
-
Push to the Azure remote you just configured
git push azure main
That's it, that deploys your source code to your web app on Azure.
Make sure you have the relevant Azure extension:
- Install Azure Tools, https://code.visualstudio.com/docs/azure/extensions that will include App Service that you will need for deployment
- In Visual Studio Code, select the Azure icon in the left menu bar.
- In resources, select the subscription you want to use.
- Right click "App Services" node and select "Create New Web app" and follow the instructions
- Once the web app is created, right click it and select "Deploy to Web App", this will deploy your source code
- In the portal, go to your web app resource, and select the CORS menu.
- Add "*" to allowed origins, this will allow any IP to make calls towards the API (that's a great option for open APIs but for company APIs you might want to be more restrictive like companydomain.com for example)