|
| 1 | +# 💻 Exercise 2: Working with Azure Static Web App CLI |
| 2 | + |
| 3 | +In this exercise, you'll learn how to run the **Portal** project in Codespaces and how to use the Azure Static Web Apps CLI to handle communication between the frontend and backend. |
| 4 | + |
| 5 | +## What is Azure Static Web App CLI? |
| 6 | + |
| 7 | +The **[Azure Static Web Apps (SWA) CLI](https://github.com/Azure/static-web-apps-cli/?WT.mc_id=academic-101248-cyzanon)** is an open-source command-line tool that simplifies local development and deployment to Azure Static Web Apps. It allows you to run your app locally or in a dev container, which in this case is GitHub Codespace, and then deploy your app to a production environment with just one command. |
| 8 | + |
| 9 | +## How does the communication between frontend and backend work? |
| 10 | + |
| 11 | +When we use the Azure Static Web CLI, we can establish communication between the Back-End and Front-End through the file: `swa-cli.config.json`. |
| 12 | + |
| 13 | +This file can be generated once you have globally installed the package: **[@azure/static-web-apps-cli](https://www.npmjs.com/package/@azure/static-web-apps-cli)**, by executing the following command inside the folder: **Portal**. |
| 14 | + |
| 15 | +```bash |
| 16 | +swa init |
| 17 | +``` |
| 18 | + |
| 19 | +Please see the step-by-step creation of the file in the image below: |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Once the file is generated according to the project settings, please observe in the code below that the `swa-cli.config.json` file has the following properties: |
| 24 | + |
| 25 | +<details><summary><b>packages/portal/swa-cli.config.json</b></summary> |
| 26 | +<br/> |
| 27 | + |
| 28 | + ```json |
| 29 | + { |
| 30 | + "$schema": "https://aka.ms/azure/static-web-apps-cli/schema", |
| 31 | + "configurations": { |
| 32 | + "contoso-real-estate": { |
| 33 | + "appLocation": ".", |
| 34 | + "apiLocation": "../api", |
| 35 | + "outputLocation": "dist/contoso-app", |
| 36 | + "appBuildCommand": "npm run build", |
| 37 | + "apiBuildCommand": "npm run build", |
| 38 | + "run": "npm start", |
| 39 | + "appDevserverUrl": "http://localhost:4200", |
| 40 | + "apiDevserverUrl": "http://127.0.01:7071" |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + ``` |
| 45 | +</details> |
| 46 | +<br/> |
| 47 | + |
| 48 | +The backend is located in the `packages/api` folder and it is defined in the `apiLocation` property. The frontend is located in the `packages/portal` folder and it is defined in the `appLocation` property. In both cases, the `appdevserverUrl` and `apiDevserverUrl` properties point to the URLs of the development servers. |
| 49 | + |
| 50 | +The Contoso Real Estate project API was developed in Azure Functions and Azure Cosmos DB (MongoDB API). The Azure Static Web Apps CLI automatically runs the Azure Functions emulator when it detects that an Azure Functions API is used in the project. |
| 51 | + |
| 52 | +## How to run the `portal` project? |
| 53 | + |
| 54 | +We do not recommend running this project locally, as it requires a number of configurations to work. To optimize your learning, we recommend using **[Codespaces](https://github.com/features/codespaces)** which will create a development container for you. And the best part of all: without you having to install anything on your machine. |
| 55 | + |
| 56 | +> Codespaces offers 60 free hours of usage per month. After this period, you will be charged an hourly rate. To learn more, visit [Codespaces](https://github.com/features/codespaces). |
| 57 | +
|
| 58 | +To run the project in your Visual Studio Code in Codespaces, follow the steps below: |
| 59 | + |
| 60 | +1. As all the scenarios of the project are coupled, so that we can visualize what was created in the portal, it will be necessary to perform the following steps: |
| 61 | + - Open the Visual Studio Code terminal and, at the root of the Contoso project, run the commands `npm install && npm start` to install the project dependencies and start the servers. |
| 62 | + |
| 63 | + > _Note: Codespaces will show a number of windows on the right side of the screen as it starts up all the servers. This is expected and normal._ |
| 64 | +
|
| 65 | +2. Once all the development servers have started, the following URLs will be available: |
| 66 | + |
| 67 | +| Application | URL | Port | |
| 68 | +| -------------- | -------------------------------------------------------- | ---- | |
| 69 | +| Portal | https://YOUR-REPO-4280.preview.app.github.dev:4280 | 4280 | |
| 70 | +| Blog | https://YOUR-REPO-3000.preview.app.github.dev:3000 | 3000 | |
| 71 | +| Strapi CMS | https://YOUR-REPO-1337.preview.app.github.dev:1337/admin | 1337 | |
| 72 | +| Serverless API | https://YOUR-REPO-7071.preview.app.github.dev:7071/api/ | 7071 | |
| 73 | +| Stripe API | https://YOUR-REPO-4242.preview.app.github.dev:4242 | 4242 | |
| 74 | + |
| 75 | +> _Note: The URLs above are just examples. The URLs will be different for your fork. The ports, however, will be the same._ |
| 76 | +
|
| 77 | +3. To view the project, go to the `Ports` tab of the terminal and click on the portal link, which will be port `4280` (the default port of the ASWA), to see the portal home page. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +If you want to see the portal, go to the `Ports` tab of the terminal and click on the portal link, which will be port `4280`, to see the portal home page. The page will be blank, as you need to integrate the API with the portal. |
| 82 | + |
| 83 | +The example below shows the portal home page with the sample data. |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +### Understanding the `Portal` project execution |
| 88 | + |
| 89 | +As explained in the previous item, the Contoso project was developed in a **Composable Architecture** model; that is, it is composed of several components, where each component is responsible for a specific functionality. **For this reason, the execution of this project is coupled.** |
| 90 | + |
| 91 | +**How can we check this?** |
| 92 | + |
| 93 | +Open the `package.json` file at the root of the Contoso project and observe the scripts below: |
| 94 | + |
| 95 | +<details><summary><b>packages/portal/package.json</b></summary><br/> |
| 96 | + |
| 97 | + ```json |
| 98 | + "scripts": { |
| 99 | + "start": "concurrently npm:start:* --kill-others", |
| 100 | + "start:services": "docker compose up", |
| 101 | + "start:api": "npm run start --workspace=api", |
| 102 | + "start:website": "npm run start:swa --workspace=portal", |
| 103 | + "test": "npm run test -ws --if-present", |
| 104 | + "build": "npm run build -ws --if-present", |
| 105 | + "format": "prettier --write .", |
| 106 | + "format:check": "prettier --check .", |
| 107 | + "lint": "npm run lint -ws --if-present", |
| 108 | + "lint:fix": "npm run lint:fix -ws --if-present", |
| 109 | + "clean": "rimraf \"packages/**/*.tsbuildinfo\"", |
| 110 | + "clean:install": "rimraf \"packages/**/node_modules\" \"node_modules\" && npm install" |
| 111 | + } |
| 112 | + ``` |
| 113 | +</details> |
| 114 | +<br/> |
| 115 | + |
| 116 | +Checking the scripts: |
| 117 | + |
| 118 | +**Docker compose** |
| 119 | + |
| 120 | +```json |
| 121 | + "start:services": "docker compose up" |
| 122 | +``` |
| 123 | + |
| 124 | +This file is responsible for starting the docker services, which are Strapi CMS and Stripe API. In addition, this `docker compose` is configured to start the Azure Database for PostgreSQL database of Strapi CMS and the Stripe API, and, in the API, it is configured to start the Azure Cosmos DB database integrated with MongoDB. |
| 125 | + |
| 126 | +**API** |
| 127 | +```json |
| 128 | + "start:api": "npm run start --workspace=api" |
| 129 | +``` |
| 130 | + |
| 131 | +Responsible for running the `API` project. |
| 132 | + |
| 133 | +**Portal** |
| 134 | +```json |
| 135 | + "start:website": "npm run start:swa --workspace=portal" |
| 136 | +``` |
| 137 | + |
| 138 | +Responsible for running the `portal` project. |
| 139 | + |
| 140 | +#### Understanding the Heart of the SWA CLI: Reverse Proxy |
| 141 | + |
| 142 | +This is the heart of the SWA CLI. It intercepts and forwards HTTP requests to the right components based on purpose: |
| 143 | + |
| 144 | +- `/.auth/**` requests => forwarded to the Auth emulator server. |
| 145 | +- `/api/**` requests => forwarded to the API backend (if configured). |
| 146 | +- `/**` => all other requests forwarded to the static assets content server. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +That's why we can connect the portal to the API without having to configure anything. The SWA CLI does this for us. You can learn more about the Azure Static Web Apps CLI at [https://azure.github.io/static-web-apps-cli/docs/intro/](https://azure.github.io/static-web-apps-cli/docs/intro/?WT.mc_id=academic-101248-cyzanon). |
| 151 | + |
| 152 | +In the next exercise, you will learn how to deploy the `portal` project on Azure Static Web Apps. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +[**➡️ Exercise 3: Deploy on Azure Static Web App**](./03-exercise-portal-deploy.md) |
0 commit comments