Unverified Commit 4627da2f authored by Julian's avatar Julian
Browse files

add CSS support

parent ddbed5d0
# Space API Server
# Hapi - The Hasi SpaceAPI Server
This server provides a [SpaceAPI 0.13](https://spaceapi.io) compatible API. And a status page.
......@@ -10,6 +10,7 @@ This server provides a [SpaceAPI 0.13](https://spaceapi.io) compatible API. And
* Automatic updates through Server Sent Events
* Dynamic Favicon depending on space state
* Configuration using the SpaceAPI
* A CSS file which you can use to show the current space status on your website
* Zero dependencies!
......@@ -33,8 +34,8 @@ Execute a GET request at / with `'accept': 'text/html'` to get an overview of th
If you want to update things, your request needs an `authorization` header containing the API key specified in the
`SPACE_API_KEY` variable.
Execute a PUT or POST request to any valid sub-path described in the [SpaceAPI 0.13 Docs](https://spaceapi.io/docs/) to
update it. For example, to set the address of the space, execute a POST or PUT request to `/location/address` with a HTML body
containing the new address. You can also POST to non-leaf nodes by providing valid JSON in the request body.
update it. For example, to set the address of the space, execute a POST or PUT request to `/location/address` with an HTML
body containing the new address. You can also POST to non-leaf nodes by providing valid JSON in the request body.
For example, you can POST (or PUT) the JSON object `{"m4": "http://hacker.space/stream.m4"}` to `/stream/`.
This does work for every path besides `/`. If you want to update or create a new entry in an array, please use indexes.
For example: `/sensors/temperature/0/value` with an HTTP body of `5`.
......@@ -48,7 +49,7 @@ You can execute an HTTP DELETE at any path besides `/` to delete the specified n
The response body of each valid deletion contains the complete space status.
## Hiding Sensors on the Status Page
The sensors can be hidden on the status page by giving them the additional attribute: `"ext_hide_on_page": true`.
Sensors can be hidden on the status page by giving them the additional attribute: `"ext_hide_on_page": true`.
Example:
```json
{
......@@ -59,4 +60,10 @@ Example:
}
```
## Using `status.css`
Hapi offers a CSS file which changes according to the status (open/closed) of your hackspace at `/status.css`. You
can set the content of `status.css` by POSTing CSS to `/ext_css_open` and `/ext_css_closed` respectively.
If there is no content for the current state, an empty CSS file is returned.
......@@ -252,12 +252,28 @@ const server = http.createServer((req, res) => {
case '/favicon_closed.ico':
handleFavIcon(req, res, false);
break;
case '/status.css':
handleCSS(req, res);
break;
default:
handleUrl(req, res);
}
});
function handleCSS(req, res) {
res.writeHead(200, {
'Content-Type': 'text/css; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
});
if(outputJSON.state.open){
res.end(outputJSON.ext_css_open || "");
} else {
res.end(outputJSON.ext_css_closed || "");
}
}
function handleFavIcon(req, res, isOpen) {
res.writeHead(200, {
......@@ -344,7 +360,7 @@ function handleUrl(req, res) {
body += chunk.toString(); // convert Buffer to string
});
req.on('end', () => {
if (infos) {
if (infos || req.url.startsWith('/ext_')) {
const success = updateAtPath(pathArray, body, infos.type);
if (success) {
res.setHeader('Content-Type', 'application/json');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment