Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Azure Web App configuration in a static web page

Tags:

I have a simple set up:

  • Azure Web App, running a static react app
  • Azure Functions App, the API layer that accesses the database and that is called from the static web app

Both Web App and Functions App have a deployment slot feature, where you deploy in a separate slot first and if everything works well, you can swap the artifact in your slot and the current version, with no downtime. I really want to use this to its fullest.

I'd like to use the Web App configuration to inject the root uri of the API, have it point to the API in the corresponding slot. So the production-staging static site, should point to the production-staging API.

But here's the main problem: I cannot access the Web App configuration from my react app. I have to insert the root uri at build time, which disables the swap feature for the Web App (since it would still be pointing to staging).

Accessing the configuration works fine for the Functions App; I'm assuming because it's running node.

like image 590
Anthony De Smet Avatar asked Apr 25 '20 08:04

Anthony De Smet


People also ask

How do I connect to Azure app Configuration?

In the upper-left corner of the home page, select Create a resource. In the Search services and marketplace box, enter App Configuration and select Enter. Select App Configuration from the search results, and then select Create. Select the Azure subscription that you want to use to test App Configuration.

How do I edit Azure web config?

If you want to change web. config files you can use the Azure portal, there is a tool called "App Service Editor" in preview or Kudu that lets you edit any of the files you've deployed.

Where is Azure web config file?

By default Sitefinity connection string is saved in folder App_Data/Sitefinity/Configuration/DataConfig.


1 Answers

The Web App Configuration are available as environment variables on the server. You won't be able to access those variables within your static react app that is running on the client.

You will need some kind of middleware that is able to read and expose the environment variables through an API.

You can use ASP.NET Core with the React project template to create both, an ASP.NET Core project that acts as an API and a standard CRA React project to act as a UI, but with the convenience of hosting both in a single app project that can be built and published as a single unit. (Source).

Then you will have to write a little controller that exposes the configurations. Here an example:

public class MyOptions
{
    public string ApiUri { get; set; }
}

[ApiController]
[Route("[controller]")]
public class ConfigurationController : ControllerBase
{
    private readonly MyOptions _options;

    public ConfigurationController(IOptions<MyOptions> options)
    {
        _options = options.Value;
    }

    [HttpGet]
    public MyOptions GetConfigurations()
    {
        return _options;
    }
}

You also need to configure the options within the startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
    services.AddControllers();
}

Now you can set your initial value within the appsettings.json:

{
  "MyOptions": {
    "ApiUri" :  "https://myapp.domain.com/api" 
  }
}

And you are also able to overwrite the options using the Azure Web App Configurations (the middleware is configured to also use environment variables and that environment variables overwrite appsettings.json)

Now the last thing you have to do is to retrieve the settings within your static UI using:

window.location.host + "/api/configuration"
like image 76
Martin Brandl Avatar answered Oct 04 '22 00:10

Martin Brandl