Sample Project
Last updated
Last updated
Along with the pages, we've also provided a which can help you get up and running. This project is designed as as Google Cloud Function meaning that it'll only be running for the times that it needs to be used. You can .
Within the project you'll find the following folders :
src
- this is the main folder for the project. All of your application logic should go in here.
src/config
- here we've provided a way of allowing you to swap configuration variables between environments.
src/const
- Any constants that might be used in your code. As an example we provide const
variables for environment.
src/handlers
- This contains the logic for your function and will be the entry point for the function.
src/model
- this provides a utility function to help manage the configuration between environments.
src/util
- a few other utility functions that you might find useful.
scripts
- add any build scripts you need in here. We've provided one script in this folder to help get your environment variables into you project.
package.json
- As a NodeJS project, this file determines the tasks that you can run, as well as any dependancies you might need for your application.
tsconfig.json
- This is the configuration for the TypeScript
files.
webpack.config.js
- This is the configuration for Webpack
To get the project running you will first need a NodeJS environment on your computer. We'd recommend using Node Version Manager
or nvm
for short to help you manage the various different versions of NodeJS to install.
The page above provides instructions for installing nvm
. Once installed, we recommend a version 12.x
of NodeJS. You can install this on your computer by running nvm install 12
.
The file contains all the various scripts and commands you will need to build the source code into an executable format.
To start with you'll need to run npm install
from within the project folder to install all the external dependancies listed in the package.json
. These dependancies will include TypeScript
.
The project source code is written in TypeScript
however as part of the build process we compile the typescript into normal JavaScript. This is done using the tsc
command. As defined in the package.json
file, we have provided a watch
command which will run this command with the -w
flag so that it automatically builds any modified files in the source directory. Depending on how you are working you can either run tsc
once, or npm run watch
to build the files as they change.
The result of the tsc
command will be a series of files in the .build
folder which mirror that of the src
folder, but are .js
files rather than .ts
files. It is these files that are used to run the cloud function.
To start the cloud function, use npm start
. This will use the functions-framework
tool that emulates what it will be like running the function via Google Cloud. This process will start the webserver and expose the application on typically port 8080
. This means that once running you should be able to send network requests to your application via http://localhost:8080
Also provided within the package.json
file is a build
task that will help you package the files in an appropriate format for a live environment. This process will combine all the files into one, and "minimise" them so that they can be transferred and loaded as quick as possible. To run this process use npm run build
. The output of this task will be in the .webpack
folder and will be essentially an index.js
file, along with a package.json
file.
As each environment is different we have not provided samples to help you deploy your application to something like Google Cloud, however if you have followed the steps above then you should have everything you need. For reference, the documentation for Google Cloud is below.
In this context, we would use the following command to deploy this project, however please adapt this to suit your needs.
If you have used environment variables with your application then we'd recommend the use of a env.yaml file to provide the variables to the Cloud environment. More information is available below.
To make use of this file, simply add --env-vars-file env.yaml
to the end of the deployment command line options.
As mentioned in the documentation, to test this when running locally you will need something like ngrok
to make your local application available to the internet. . Once you have done this, or if the application is deployed to a public URL then you can modify the settings of your digital assistant to point to it.