Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Serverless Functions for CDN Deploys #235

Merged
merged 8 commits into from
Sep 19, 2021
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## ✨ 1.8.2 - Serverless Functions for Netlify Instances [PR #235](https://github.com/Lissy93/dashy/pull/235)
- Previously when Dashy was deployed as a static site to Netlify, it was not possible to use several features, which required server-side code
- This PR adds serverless cloud functions to provide most of this functionality

## 🩹 1.8.1 - Additional Languages, Bug Fix, and more [PR #234](https://github.com/Lissy93/dashy/pull/234)
- Merges 5 additional languages
- Adds RickyCZ's dashboard to showcase
Expand Down
34 changes: 34 additions & 0 deletions docs/development-guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,40 @@ Running `yarn upgrade` will updated all dependencies based on the ranges specifi

---

## Developing Netlify Cloud Functions

When Dashy is deployed to Netlify, it is effectively running as a static app, and therefore the server-side code for the Node.js endpoints is not available. However Netlify now supports serverless cloud lambda functions, which can be used to replace most functionality.

#### 1. Run Netlify Dev Server

First off, install the Netlify CLI: `npm install netlify-cli -g`
Then, from within the root of Dashy's directory, start the server, by running: `netlify dev`

#### 2. Create a lambda function

This should be saved it in the [`./services/serverless-functions`](https://github.com/Lissy93/dashy/tree/master/services/serverless-functions) directory

```javascript
exports.handler = async () => ({
statusCode: 200,
body: 'Return some data here...',
});
```

#### 3. Redirect the Node endpoint to the function

In the [`netlify.toml`](https://github.com/Lissy93/dashy/blob/FEATURE/serverless-functions/netlify.toml) file, add a 301 redirect, with the path to the original Node.js endpoint, and the name of your cloud function

```toml
[[redirects]]
from = "/status-check"
to = "/.netlify/functions/cloud-status-check"
status = 301
force = true
```

---

## Hiding Page Furniture on Certain Routes
For some pages (such as the login page, the minimal start page, etc) the basic page furniture, (like header, footer, nav, etc) is not needed. This section explains how you can hide furniture on a new view (step 1), or add a component that should be hidden on certain views (step 2).

Expand Down
127 changes: 77 additions & 50 deletions docs/release-workflow.md

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@
base = "/"
command = "yarn build"
publish = "dist"
functions = "services/serverless-functions"

# Site info, used for the 1-Click deploy page
[template.environment]
STATUSKIT_PAGE_TITLE = "Dashy"
STATUSKIT_COMPANY_LOGO = "https://raw.githubusercontent.com/Lissy93/dashy/master/docs/assets/logo.png"
STATUSKIT_SUPPORT_CONTACT_LINK = "https://dashy.to"
STATUSKIT_RESOURCES_LINK = "https://github.com/Lissy93/dashy/tree/master/docs"

STATUSKIT_SUPPORT_CONTACT_LINK = "https://github.com/lissy93/dashy"
STATUSKIT_RESOURCES_LINK = "https://dashy.to/docs"

# Redirect the Node endpoints to serverless functions
[[redirects]]
from = "/status-check"
to = "/.netlify/functions/cloud-status-check"
status = 301
force = true
[[redirects]]
from = "/config-manager/*"
to = "/.netlify/functions/not-supported"
status = 301
force = true

# Set any security headers here
[[headers]]
for = "/*"
[headers.values]
# Uncomment to enable Netlify user control. You must have a paid plan.
# Basic-Auth = "someuser:somepassword anotheruser:anotherpassword"
# Basic-Auth = "someuser:somepassword anotheruser:anotherpassword"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Dashy",
"version": "1.8.1",
"version": "1.8.2",
"license": "MIT",
"main": "server",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions services/serverless-functions/cloud-status-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* A cloud function that wraps the status checking method, for use on Netlify */
const statusCheck = require('../status-check');

exports.handler = (event, context, callback) => {
const paramStr = event.rawQuery;
statusCheck(paramStr, (results) => {
callback(null, {
statusCode: 200,
body: results,
});
});
};
8 changes: 8 additions & 0 deletions services/serverless-functions/not-supported.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* A Netlify cloud function to return a message endpoints that are not available */
exports.handler = async () => ({
statusCode: 200,
body: JSON.stringify({
success: false,
error: 'This action is not supported on Netlify',
}),
});