Search code examples
azuredatabase-connectionsvelteazure-static-web-app

How to change the dataApiPort option during Azure Static Web Application deployment using SWA CLI?


I am trying to deploy a Svelte web application to an Azure Static Web Application (SWA) using the swa CLI tool. I have created a serverless "Cosmos DB NoSQL" database and a "Static Web App" resource on Azure. The simple web application deployment was successful and I managed to link the Cosmos DB with the Static Web App using the SWA CLI tool on my Linux (Debian 12) machine.

Deployment steps

However, I could not deploy the application on my Windows (workplace) machine. I had been following the Database connection Azure Cosmos DB tutorials. First, I exported the connection string from the database:

export DATABASE_CONNECTION_STRING='<YOUR_CONNECTION_STRING>'

Next, I installed the swa CLI tool globally:

npm install -g @azure/static-web-apps-cli

I am using the /build folder for deployment hence I built the app with the npm run build command. Here is the content of the svelte.config.js config file:

import adapter from '@sveltejs/adapter-static';

export default {
    kit: {
        adapter: adapter({
            pages: 'build',
            assets: 'build',
            fallback: 'index.html',
            precompress: false,
            strict: true
        }),
        // This option is necessary for the GitHub page deployment
        paths: {
            base: process.argv.includes('dev') ? '' : process.env.BASE_PATH
        }
    }
};

But, when I start the SWA emulator with the following command:

swa start ./build --data-api-location swa-db-connections

On Windows (I did not get any error on Linux), I got the following error message:

[dataApi] Information: Microsoft.DataApiBuilder 0.9.7+e560142426d1c080b9fd7b7fabff51a276f6bf61
[dataApi] Information: User provided config file: staticwebapp.database.config.json
[dataApi] Loading config file from staticwebapp.database.config.json.
[dataApi] Information: Loaded config file: staticwebapp.database.config.json       
[dataApi] Information: Setting default minimum LogLevel: Error for Production mode.
[dataApi] Starting the runtime engine...
[dataApi] Redirecting to https is disabled.
[dataApi] Loading config file from staticwebapp.database.config.json.
[dataApi] info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[63]
[dataApi]       User profile is available. Using 'C:\Users\z0190983\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
[swa] 
[swa] Using workflow file:
[swa]   C:\Users\z0190983\ws\personal\pomodoro-svelte\.github\workflows\azure-static-web-apps.yml
[dataApi] Unable to launch the runtime due to: System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.
[dataApi]  ---> Microsoft.AspNetCore.Connections.AddressInUseException: Only one usage of each socket address (protocol/network address/port) is normally permitted.
[dataApi]  ---> System.Net.Sockets.SocketException (10048): Only one usage of each socket address (protocol/network address/port) is normally permitted.
[dataApi]    at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
[dataApi]    at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
[dataApi]    at System.Net.Sockets.Socket.Bind(EndPoint localEP)

I checked the with netstat -na | findstr :5000, and indeed it is listening. If I execute the swa --print-config command, I will see that the dataApiPort is the 5000 port:

Options:
 - port: 4280
 - host: localhost
 - apiPort: 7071
 - dataApiPort: 5000
 - appLocation: .
 - apiLocation: <undefined>
 - dataApiLocation: swa-db-connections
 - outputLocation: build
...

Now, the question is how can I change the dataApiPort options to another port? I tried using the swa start --port 5000 with no vain. I would really appreciate any help and guidance!

Note: You can also clone the GitHub repository to quickly reproduce the problem.

Configuration files (Optional)

The configuration files are the exact copy from the tutorial but here are they, just in case. Under the swa-db-connections folder, this is the content of the staticwebapp.database.config.json file:

{
    "$schema": "https://github.com/Azure/data-api-builder/releases/download/v0.9.7/dab.draft.schema.json",
    "data-source": {
        "database-type": "cosmosdb_nosql",
        "connection-string": "@env('DATABASE_CONNECTION_STRING')",
        "options": {
            "database": "MyTestPersonDatabase",
            "schema": "staticwebapp.database.schema.gql"
        }
    },
    "runtime": {
        "graphql": {
            "enabled": true,
            "path": "/graphql",
            "allow-introspection": true
        },
        "host": {
            "cors": {
                "origins": ["http://localhost:4280"],
                "allow-credentials": false
            },
            "authentication": {
                "provider": "StaticWebApps"
            },
            "mode": "production"
        }
    },
    "entities": {
        "Person": {
            "source": "MyTestPersonContainer",
            "permissions": [
                {
                    "actions": ["*"],
                    "role": "anonymous"
                }
            ]
        }
    }
}

The content of the staticwebapp.database.schema.gql file:

type Person @model {
    id: ID
    Name: String
}

Solution

  • I solved the problem by killing the process that occupied the 5000 port. I checked the process ID with this command:

    netstat -ano | findstr :8080
    
      TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       6764
      TCP    [::1]:5000             [::]:0                 LISTENING       6764
    

    I looked at the 6764 pid with tasklist /fi "pid eq 6764" command and it was the OneApp.IGCC.WinService.exe process. It is a service from Intel and I do not know what it is doing but according to the Stop the service OneApp.IGCC.WinService forum post:

    it is not necessary to have that service running

    Therefore I killed the process:

    taskkill /pid 6764 /F
    

    And the deployment was successful.

    It is not very elegant... because every time I need to kill this process. Alternatively, there is the OneApp.IGCC.WinService using port 5000 post from the Intel community to change the port permanently:

    Here's what I did:

    1. Open the Registry Editor (as always, be careful in there).
    2. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\igccservice.
    3. Double-click the ImagePath key.
    4. Append after the quotes: --urls http://127.0.0.1:50000 Click OK. Restart the service.
    5. Click OK.
    6. Restart the service.