|
| 1 | +# Write an App |
| 2 | + |
| 3 | +The folder `apps/demo` (https://github.com/BrickSchema/playground/tree/master/apps/demo) contains a demo app, which is a minimized read/write actuator with Brick and Playground |
| 4 | +interfaces. |
| 5 | + |
| 6 | +The demo app contains the following files: |
| 7 | + |
| 8 | +```text |
| 9 | +├── backend # a simple backend written by express.js |
| 10 | +│ └── index.js |
| 11 | +├── frontend # a simple frontend in pure html |
| 12 | +│ └── index.html |
| 13 | +├── build.sh # a script that packs backend.zip and frontend.zip |
| 14 | +├── Dockerfile # sbos-playground uses the Dockerfile to build the image |
| 15 | +├── package.json # define the dependencies in the backend |
| 16 | +└── yarn.lock # lock the dependencies in package.json |
| 17 | +``` |
| 18 | + |
| 19 | +You can use any language and framework to write the app backend and frontend. |
| 20 | + |
| 21 | +## Backend |
| 22 | + |
| 23 | +### Dockerfile |
| 24 | + |
| 25 | +For the backend, you must provide a `Dockerfile` and other files (if necessary) to build the docker image. |
| 26 | + |
| 27 | +For example, the `Dockerfile` of the demo app use `node 18` as base image, install the packages and run the backend file `backend/index.js`. For more information about how to write a `Dockerfile`, please refer to the official documentation of `Docker`. |
| 28 | + |
| 29 | +!!! important |
| 30 | + You should start the backend server on `localhost:5000` in the docker image so that it can be accessed by the frontend. |
| 31 | + |
| 32 | +```Dockerfile |
| 33 | +FROM node:18-alpine |
| 34 | + |
| 35 | +ENV HOME="/root" |
| 36 | +WORKDIR /root |
| 37 | + |
| 38 | +COPY ./package.json ./yarn.lock /root/ |
| 39 | +RUN --mount=type=cache,target=/usr/local/share/.config/yarn/global yarn |
| 40 | +COPY . /root |
| 41 | + |
| 42 | +CMD node backend/index.js |
| 43 | +``` |
| 44 | + |
| 45 | +### Get JWT (JSON Web Token) |
| 46 | + |
| 47 | +When running the docker image, `playground` provides an environment variable `BRICK_SERVER_API_TOKEN`, which is a JWT ( |
| 48 | +JSON Web Token) to authorize the requests to the API endpoints in `playground`. |
| 49 | + |
| 50 | +For example, the following code snippets get the JWT from the environment variable and parse the information in the JWT. |
| 51 | + |
| 52 | +=== "Python" |
| 53 | + |
| 54 | + ```python |
| 55 | + import os |
| 56 | + import jwt |
| 57 | + api_token = os.environ.get('BRICK_SERVER_API_TOKEN') |
| 58 | + decoded_token = jwt.decode(api_token, options={"verify_signature": False}) |
| 59 | + ``` |
| 60 | + |
| 61 | +=== "NodeJS" |
| 62 | + |
| 63 | + ```js |
| 64 | + const apiToken = process.env.BRICK_SERVER_API_TOKEN; |
| 65 | + const decodedToken = JSON.parse(Buffer.from(apiToken.split('.')[1], 'base64').toString()); |
| 66 | + ``` |
| 67 | + |
| 68 | +The decoded (parsed) JWT is a JSON object, which is in the format of |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + |
| 73 | + "aud": [ |
| 74 | + "brick" |
| 75 | + ], |
| 76 | + "domain": "Center_Hall", |
| 77 | + "app": "demo", |
| 78 | + "domain_user_app": "66c4ec995317281fb487ccd3", |
| 79 | + "exp": 1747595485 |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +| Key | Description | |
| 84 | +|-----------------|------------------------------------------| |
| 85 | +| sub | User using this app | |
| 86 | +| aud | JWT audience (always "brick") | |
| 87 | +| domain | The domain this app currently working in | |
| 88 | +| app | Name of this app | |
| 89 | +| domain_user_app | ObjectId of this app instance in DB | |
| 90 | +| exp | Expiration time of the JWT (Unix Epoch) | |
| 91 | + |
| 92 | +### Make API Requests to `playground` |
| 93 | + |
| 94 | +The backend should add the JWT in the header as a bearer token when sending API requests to `playground` |
| 95 | + |
| 96 | +```python |
| 97 | +headers = { |
| 98 | + 'Content-Type': 'application/json', |
| 99 | + 'Authorization': 'Bearer <JWT token>', |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +The base API endpoint of `playground` is https://brickserver.ucsd.edu/brickapi/v1/, you can also check https://brickserver.ucsd.edu/brickapi/v1/docs for a detailed documentation of the available APIs. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## Frontend |
| 108 | + |
| 109 | +For the frontend, you must provide a `index.html` and other files (such as `js`, `css` files if necessary), and the folder will be served as static files on `playground`. |
| 110 | +Though we only use a single html file as the frontend in the demo app, you can choose any frontend framework and use the build files as the frontend. |
| 111 | + |
| 112 | +### Get JWT |
| 113 | + |
| 114 | +When a user accesses the frontend through `playground`, the same JWT provided to the backend is included in the query parameter `token`. You can use `Javascript` to read the JWT. |
| 115 | + |
| 116 | +```js |
| 117 | +const baseURL = "https://brickserver.ucsd.edu/brickapi/v1/apps/api"; |
| 118 | +const params = new URL(document.location).searchParams; |
| 119 | +const token = params.get("token"); |
| 120 | +``` |
| 121 | + |
| 122 | +### Make API Requests to Backend |
| 123 | + |
| 124 | +`playground` designed special API endpoint translation rules for the frontend to access to backend APIs. |
| 125 | +The base API endpoint for such API requests is https://brickserver.ucsd.edu/brickapi/v1/apps/api. |
| 126 | + |
| 127 | +For example, if the backend has an API endpoint on `localhost:5000/example`, the frontend should send a request to https://brickserver.ucsd.edu/brickapi/v1/apps/api/example with the JWT in the header as a bearer token (same as how it is used in the backend). |
| 128 | + |
| 129 | +## Submit the App |
| 130 | + |
| 131 | +### Backend and Frontend Files |
| 132 | + |
| 133 | +The backend and frontend files should be zipped into `backend.zip` and `frontend.zip` before submission. Make sure that the files are in the root of the zip archive. |
| 134 | + |
| 135 | +For example, the structure of `backend.zip` in the demo app is |
| 136 | + |
| 137 | +```text |
| 138 | +backend.zip |
| 139 | +├── backend |
| 140 | +│ └── index.js |
| 141 | +├── Dockerfile |
| 142 | +├── package.json |
| 143 | +└── yarn.lock |
| 144 | +``` |
| 145 | + |
| 146 | +The structure of `frontend.zip` in the demo app is |
| 147 | + |
| 148 | +```text |
| 149 | +frontend.zip |
| 150 | +└── index.html |
| 151 | +``` |
| 152 | + |
| 153 | +The file `build.sh` is a helper script to generate these two zip files in the demo app. |
| 154 | + |
| 155 | +### Permission Profile |
| 156 | + |
| 157 | + |
| 158 | + |
0 commit comments