Skip to content

Commit d9a5070

Browse files
committed
wrapping up
1 parent bcc8afb commit d9a5070

File tree

4 files changed

+105
-52
lines changed

4 files changed

+105
-52
lines changed

.github/workflows/azure-static-web-apps-wonderful-bay-05e97f31e.yml renamed to .github/workflows/azure-static-web-apps.template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
id: builddeploy
2323
uses: Azure/static-web-apps-deploy@v1
2424
with:
25-
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_WONDERFUL_BAY_05E97F31E }}
25+
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
2626
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
2727
action: "upload"
2828
###### Repository/Build Configurations - These values can be configured to match your app requirements. ######

README.md

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ The implementation uses
3535
- [Azure Static WebApp](https://azure.microsoft.com/en-us/services/app-service/static/): to bind everything together in one easy package, natively integrated with GitHub CI/CD pipeline
3636
- [Vue.Js](https://vuejs.org/) as front-end client
3737
- [Azure Function](https://azure.microsoft.com/en-us/services/functions/) for providing serverless back-end infrastructure
38-
- [Prisma](https://www.prisma.io/) to interact with the Azure SQL database
39-
- [TypeScript](https://www.typescriptlang.org/) for the back-end logic
4038
- [NodeJS](https://nodejs.org/en/) for the back-end logic
39+
- [TypeScript](https://www.typescriptlang.org/) for the back-end logic
40+
- [Prisma](https://www.prisma.io/) to interact with the Azure SQL database
4141
- [Azure SQL](https://azure.microsoft.com/en-us/services/sql-database/) as database to store ToDo data
42-
- [GitHub Actions](https://github.com/features/actions) to Deploy the full-stack website (thanks to Azure Static WebApps)
42+
- [GitHub Actions](https://github.com/features/actions) to Deploy the full-stack website (thanks to Azure Static Web Apps)
4343

4444
## Implementation Details
4545

@@ -48,9 +48,41 @@ Folder structure
4848
- `/api`: the NodeJs Azure Function code used to provide the backend API, called by the Vue.Js client
4949
- `/client`: the Vue.Js client. Original source code has been taken from official Vue.js sample and adapted to call a REST or GraphQL client instead of using local storage to save and retrieve todos
5050

51-
## Local backend development with SQL Server
51+
## Install the dependencies
52+
53+
Make sure you have [Node](https://nodejs.org/en/download/) installed. Since the free tier of Azure Static Web Apps only supports Node 12, it is recommended to use Node 12 for development too.
54+
55+
Also install the [Azure Function Core Tools v3](https://www.npmjs.com/package/azure-functions-core-tools):
56+
57+
```sh
58+
npm i -g azure-functions-core-tools@3 --unsafe-perm true
59+
```
60+
61+
Make sure this dependency is not included in the `package.json` as otherwise the generated package will be too big for Azure Static Web Apps free tier (deployed .zip can max 10MB size).
62+
63+
Also install the [Azure Static Web Apps CLI](https://github.com/azure/static-web-apps-cli):
64+
65+
```sh
66+
npm install -g @azure/static-web-apps-cli`
67+
```
68+
69+
Now you can install the dependencies, enter the `./api/` folder and install the dependencies:
70+
71+
```sh
72+
npm install
73+
```
74+
75+
and the build the solution (always from in the `./api` folder):
76+
77+
```sh
78+
npm run build
79+
```
80+
81+
## Create the database
82+
83+
You have two options. If you only want to experiment locally, you can use SQL Server. If you prefer to use the cloud, you can use Azure SQL.
5284

53-
### Start the SQL Server with Docker
85+
### Option 1 - Start the SQL Server with Docker
5486

5587
If you want to develop locally without any dependency on Azure, you can run SQL Server locally using the included [docker-compose.yaml](./docker-compose.yml) file with the following command:
5688

@@ -66,21 +98,18 @@ Create a `.env` file by copying [.env.template](./api/.env.template) inside the
6698
DATABASE_URL=sqlserver://localhost:1433;database=prisma-demo;user=SA;password=Prisma1234;trustServerCertificate=true;encrypt=true
6799
```
68100
69-
## Local backend development with Azure SQL
101+
### Option 2 - Create the Azure SQL database
102+
103+
> **Beginners:** If you are completely new to Azure SQL, no worries! Here's a full playlist that will help you: [Azure SQL for beginners](https://www.youtube.com/playlist?list=PLlrxD0HtieHi5c9-i_Dnxw9vxBY-TqaeN).
70104
71-
### Create the Azure SQL database
72105
73106
Another option is to use an Azure SQL database also as a development database. If you need to create an Azure SQL database from scratch, an Azure SQL S0 database would be more than fine to run the tests.
74107
75108
```
76109
az sql db create -g <resource-group> -s <server-name> -n todo_prisma --service-objective S0
77110
```
78111
79-
Remember that if you don't have Linux environment where you can run [AZ CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) you can always use the [Cloud Shell](https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart). If you prefer to do everything via the portal, here's a tutorial: [Create an Azure SQL Database single database](https://docs.microsoft.com/en-us/azure/azure-sql/database/single-database-create-quickstart?tabs=azure-portal).
80-
81-
If you are completely new to Azure SQL, no worries! Here's a full playlist that will help you: [Azure SQL for beginners](https://www.youtube.com/playlist?list=PLlrxD0HtieHi5c9-i_Dnxw9vxBY-TqaeN).
82-
83-
### Define the connection string
112+
> **Note:**: Remember that if you don't have Linux environment where you can run [AZ CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) you can always use the [Cloud Shell](https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart). If you prefer to do everything via the portal, here's a tutorial: [Create an Azure SQL Database single database](https://docs.microsoft.com/en-us/azure/azure-sql/database/single-database-create-quickstart?tabs=azure-portal).
84113
85114
Prisma will connect to the database using the `DATABASE_URL` environment variable which can be defined in the `./api/.env` file.
86115
@@ -90,19 +119,9 @@ Create a `.env` file by copying [.env.template](./api/.env.template) inside the
90119
DATABASE_URL="sqlserver://DB_SERVER_NAME.database.windows.net:1433;database=DB_NAME;user=DB_USER;password={PASSWORD};encrypt=true;trustServerCertificate=false;loginTimeout=30"
91120
```
92121
93-
## Install the dependencies
94-
95-
Make sure you have [Node](https://nodejs.org/en/download/) and [TypeScript](https://www.typescriptlang.org/download) installed (via npm)
96-
97-
To install the dependencies, enter the `./api/` folder and install the dependencies:
98-
99-
```sh
100-
npm install
101-
```
102-
103-
### Create the database schema
122+
## Create the database schema
104123
105-
Run the migration to create the database schema using Prisma Migrate:
124+
Now that you have a database, you can create the database schema by using Prisma Migrate:
106125
107126
```sh
108127
npx prisma migrate dev
@@ -112,36 +131,41 @@ npx prisma migrate dev
112131
113132
## Start the local development server
114133

115-
Details on how to run Azure Static WebApps locally can be found here:
116-
117-
[Set up local development for Azure Static Web Apps](https://docs.microsoft.com/en-us/azure/static-web-apps/local-development)
134+
Start the Azure Static Web App server (in the root folder):
118135

119-
Long story short (make sure you have installed all the prerequisites mentioned in the link above):
136+
```sh
137+
swa start --api ./api
138+
```
120139

121-
- Install [Azure Static Web Apps CLI](https://github.com/azure/static-web-apps-cli): `npm install -g @azure/static-web-apps-cli`
122-
- Build the solution (in the `./api` folder): `npm run build`
123-
- Start the servers (in the sample root folder): `swa start --api ./api`
140+
You can now work locally.
124141

125142
## Running on Azure
126143

127144
This is the amazing part of using Azure Static WebApps. Deploying to Azure is completely automated via GitHub actions.
128145

129146
1. Fork this repository
130-
1. Open the Azure Portal
131-
1. Create a "Static Web App" resource and follow the instruction here: [Building your first static web app in the Azure portal](https://docs.microsoft.com/en-us/azure/static-web-apps/get-started-portal?tabs=vanilla-javascript), but:
132-
- When asked for GitHub repo details, point to the forked repo you just created
133-
- Select "main" as branch
134-
- Select "Custom" in the "Build Presets" dropdown
135-
- Set `client` as "App location"
136-
1. Wait for resource creation and GitHub action completion. Once the resource is ready, click on "Go to Resource".
137-
1. Be patient, now GitHub Action will kick in and deploy the full-stack website. It will take a couple of minutes.
138-
1. Relax.
139-
1. Grab some coffee or tea.
140-
1. Drink it.
141-
1. Click on "Functions" and you should be able to see the `todo` function listed.
142-
1. Go to the "Configuration" tab and add the same key and values that you have in your `.env` file you created earlier for local execution.
143-
1. Go to "Overview" and click on "Browse" to open your website. Done!
144-
145-
### Azure Static Web App
147+
1. Get a [GitHub Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)
148+
1. Run `./azure-deploy.sh`. Please note that if this is the first time you run it, it will create an `.env` file in the root folder. Fill the `.env` file. Run the `./azure-deploy.sh` again.
149+
1. Done! Well, not really, read next.
150+
151+
## Fixing generated workflow file
152+
153+
The generated workflow file will not work. Even if the CI/CD pipeline will complete successfully, the Azure Static Web App will not work. This is due to how Onyx, the tool the automate the building and the deployment for Static Web Apps, doesn't now how to properly deal with the nuances of Prisma, that generates the client right away. Fixing this issue is quite easy, just add the following enviroment variable to the workflow, in the "Build and Deploy" step:
154+
155+
```yaml
156+
env: # Add environment variables here
157+
NODE_VERSION: 12
158+
PRE_BUILD_COMMAND: "npm install -g prisma@2.30.3"
159+
CUSTOM_BUILD_COMMAND: "npm install @prisma/client && npm run build"
160+
POST_BUILD_COMMAND: "npm install @prisma/client"
161+
```
162+
163+
Take a look at the template workflow in the `./github/workflow` folder to see how your workflow file should look like.
164+
165+
## Running on Azure (yep!)
166+
167+
After you have commited the changes to the workflow file, the CI/CD pipeline will run again automatically. Once the pipeline has run, you should have a working website. Go to http://[your-swa-name].azurestaticapps.net, and enjoy!
168+
169+
## Azure Static Web App
146170

147171
Azure Static Web App supports a Free tier which is absolutely great so that you can try them for free, but of course don't expect great performances. REST API response will be in the 500 msec area. Keep this in mind if you are planning to use them for something different than testing. If you need better performance right now and cannot when for when Azure Static Web App will be out of preview, you can always deploy the REST API using plain Azure Functions where you can have amazing scalability and performance.

azure-deploy.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44
# Load values from .env file or create it if it doesn't exists
55
FILE=".env"
66
if [[ -f $FILE ]]; then
7-
echo "Loading from .env"
7+
echo "Loading from $FILE"
88
export $(egrep . $FILE | xargs -n1)
99
else
1010
cat << EOF > .env
@@ -22,6 +22,10 @@ EOF
2222
exit 1
2323
fi
2424

25+
FILE="./api/.env"
26+
echo "Loading from $FILE"
27+
export $(egrep . $FILE | xargs -n1)
28+
2529
echo "Creating Resource Group...";
2630
az group create \
2731
-n $resourceGroup \
@@ -36,7 +40,12 @@ az staticwebapp create \
3640
-b main \
3741
--api-location "./api" \
3842
--app-location "./client" \
39-
--output-location "" \
4043
--token $gitToken
4144

45+
echo "Configuring Static Web App...";
46+
az staticwebapp create \
47+
-n $appName \
48+
-g $resourceGroup \
49+
--settings-names DATABASE_URL=$DATABASE_URL
50+
4251
echo "Done."

client/index.html

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,28 @@
33

44
<head>
55
<title>TodoMVC</title>
6+
<script src="https://unpkg.com/vue"></script>
7+
<link rel="stylesheet" type="text/css" href="https://unpkg.com/todomvc-app-css@2.2.0/index.css" />
68
</head>
79

810
<body>
9-
Nothing to see here.
10-
</body>
11+
<section class="todoapp">
12+
<header class="header">
13+
<h1>todos</h1>
14+
</header>
15+
<section class="main">
16+
<ul class="todo-list">
17+
<li>Rest Sample</li>
18+
<li>GraphQL Sample</li>
19+
</ul>
20+
</section>
21+
</section>
22+
<footer class="info">
23+
<p>Original <a href="https://github.com/vuejs/vuejs.org/tree/master/src/v2/examples/vue-20-todomvc">Vue.JS Sample</a> by <a href="http://evanyou.me">Evan You</a></p>
24+
<p>Azure Function + Azure SQL Backend Sample by <a href="http://davidemauri.it">Davide Mauri</a> and <a href="https://www.prisma.io/">Prisma</a></p>
25+
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
26+
</footer>
27+
28+
</body>
29+
30+
</html>

0 commit comments

Comments
 (0)