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

Adding the appbuilder proxy code #229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .hlxignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ LICENSE
package.json
package-lock.json
test/*
tools/appbuilder
60 changes: 60 additions & 0 deletions tools/appbuilder/.github/workflows/deploy_prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: AIO App CI

on:
release:
types: [released]
jobs:
deploy:
name: Deploy to Prod
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 1
matrix:
node-version: ['16']
os: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install
run: npm i
- name: Setup CLI
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
version: 9.x.x
- name: Auth
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: oauth_sts
CLIENTID: ${{ secrets.CLIENTID_PROD }}
CLIENTSECRET: ${{ secrets.CLIENTSECRET_PROD }}
TECHNICALACCOUNTID: ${{ secrets.TECHNICALACCID_PROD }}
TECHNICALACCOUNTEMAIL: ${{ secrets.TECHNICALACCEMAIL_PROD }}
IMSORGID: ${{ secrets.IMSORGID_PROD }}
SCOPES: ${{ secrets.SCOPES_PROD }}
- name: Build
env:
AIO_RUNTIME_NAMESPACE: ${{ secrets.AIO_RUNTIME_NAMESPACE_PROD }}
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: build
- name: Deploy
env:
AIO_RUNTIME_NAMESPACE: ${{ secrets.AIO_RUNTIME_NAMESPACE_PROD }}
AIO_RUNTIME_AUTH: ${{ secrets.AIO_RUNTIME_AUTH_PROD }}
AIO_PROJECT_ID: ${{ secrets.AIO_PROJECT_ID_PROD }}
AIO_PROJECT_NAME: ${{ secrets.AIO_PROJECT_NAME_PROD }}
AIO_PROJECT_ORG_ID: ${{ secrets.AIO_PROJECT_ORG_ID_PROD }}
AIO_PROJECT_WORKSPACE_ID: ${{ secrets.AIO_PROJECT_WORKSPACE_ID_PROD }}
AIO_PROJECT_WORKSPACE_NAME: ${{ secrets.AIO_PROJECT_WORKSPACE_NAME_PROD }}
AIO_PROJECT_WORKSPACE_DETAILS_SERVICES: ${{ secrets.AIO_PROJECT_WORKSPACE_DETAILS_SERVICES_PROD }}
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: deploy
62 changes: 62 additions & 0 deletions tools/appbuilder/.github/workflows/deploy_stage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: AIO App CI

on:
push:
branches:
- main
jobs:
deploy:
name: Deploy to Stage
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 1
matrix:
node-version: ['16']
os: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install
run: npm i
- name: Setup CLI
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
version: 9.x.x
- name: Auth
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: oauth_sts
CLIENTID: ${{ secrets.CLIENTID_STAGE }}
CLIENTSECRET: ${{ secrets.CLIENTSECRET_STAGE }}
TECHNICALACCOUNTID: ${{ secrets.TECHNICALACCID_STAGE }}
TECHNICALACCOUNTEMAIL: ${{ secrets.TECHNICALACCEMAIL_STAGE }}
IMSORGID: ${{ secrets.IMSORGID_STAGE }}
SCOPES: ${{ secrets.SCOPES_STAGE }}
- name: Build
env:
AIO_RUNTIME_NAMESPACE: ${{ secrets.AIO_RUNTIME_NAMESPACE_STAGE }}
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: build
- name: Deploy
env:
AIO_RUNTIME_NAMESPACE: ${{ secrets.AIO_RUNTIME_NAMESPACE_STAGE }}
AIO_RUNTIME_AUTH: ${{ secrets.AIO_RUNTIME_AUTH_STAGE }}
AIO_PROJECT_ID: ${{ secrets.AIO_PROJECT_ID_STAGE }}
AIO_PROJECT_NAME: ${{ secrets.AIO_PROJECT_NAME_STAGE }}
AIO_PROJECT_ORG_ID: ${{ secrets.AIO_PROJECT_ORG_ID_STAGE }}
AIO_PROJECT_WORKSPACE_ID: ${{ secrets.AIO_PROJECT_WORKSPACE_ID_STAGE }}
AIO_PROJECT_WORKSPACE_NAME: ${{ secrets.AIO_PROJECT_WORKSPACE_NAME_STAGE }}
AIO_PROJECT_WORKSPACE_DETAILS_SERVICES: ${{ secrets.AIO_PROJECT_WORKSPACE_DETAILS_SERVICES_STAGE }}
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: deploy
noPublish: true
48 changes: 48 additions & 0 deletions tools/appbuilder/.github/workflows/pr_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: AIO App CI

on: [pull_request]
jobs:
test:
name: Test PR
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: ['16']
os: [macOS-latest, ubuntu-latest, windows-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install
run: npm i
- name: Setup CLI
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
version: 9.x.x
- name: Auth
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: oauth_sts
CLIENTID: ${{ secrets.CLIENTID_STAGE }}
CLIENTSECRET: ${{ secrets.CLIENTSECRET_STAGE }}
TECHNICALACCOUNTID: ${{ secrets.TECHNICALACCID_STAGE }}
TECHNICALACCOUNTEMAIL: ${{ secrets.TECHNICALACCEMAIL_STAGE }}
IMSORGID: ${{ secrets.IMSORGID_STAGE }}
SCOPES: ${{ secrets.SCOPES_STAGE }}
- name: Build
env:
AIO_RUNTIME_NAMESPACE: ${{ secrets.AIO_RUNTIME_NAMESPACE_STAGE }}
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: build
- name: Test
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
command: test
42 changes: 42 additions & 0 deletions tools/appbuilder/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

# package directories
node_modules
jspm_packages

# build
build
dist
.manifest-dist.yml

# Config
config.json
.env*
.aio

# Adobe I/O console config
console.json

# Test output
junit.xml

# IDE & Temp
.cache
.idea
.nyc_output
.vscode
coverage
.aws.tmp.creds.json
.wskdebug.props.tmp

# Parcel
.parcel-cache

# OSX
.DS_Store

# yeoman
.yo-repository

# logs folder for aio-run-detached
logs

81 changes: 81 additions & 0 deletions tools/appbuilder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# petwatchforms

Welcome to my Adobe I/O Application!

## Setup

- Populate the `.env` file in the project root and fill it as shown [below](#env)

## Local Dev

- `aio app run` to start your local Dev server
- App will run on `localhost:9080` by default

By default the UI will be served locally but actions will be deployed and served from Adobe I/O Runtime. To start a
local serverless stack and also run your actions locally use the `aio app run --local` option.

## Test & Coverage

- Run `aio app test` to run unit tests for ui and actions
- Run `aio app test --e2e` to run e2e tests

## Deploy & Cleanup

- `aio app deploy` to build and deploy all actions on Runtime and static files to CDN
- `aio app undeploy` to undeploy the app

## Config

### `.env`

You can generate this file using the command `aio app use`.

```bash
# This file must **not** be committed to source control

## please provide your Adobe I/O Runtime credentials
# AIO_RUNTIME_AUTH=
# AIO_RUNTIME_NAMESPACE=
```

### `app.config.yaml`

- Main configuration file that defines an application's implementation.
- More information on this file, application configuration, and extension configuration
can be found [here](https://developer.adobe.com/app-builder/docs/guides/appbuilder-configuration/#appconfigyaml)

#### Action Dependencies

- You have two options to resolve your actions' dependencies:

1. **Packaged action file**: Add your action's dependencies to the root
`package.json` and install them using `npm install`. Then set the `function`
field in `app.config.yaml` to point to the **entry file** of your action
folder. We will use `webpack` to package your code and dependencies into a
single minified js file. The action will then be deployed as a single file.
Use this method if you want to reduce the size of your actions.

2. **Zipped action folder**: In the folder containing the action code add a
`package.json` with the action's dependencies. Then set the `function`
field in `app.config.yaml` to point to the **folder** of that action. We will
install the required dependencies within that directory and zip the folder
before deploying it as a zipped action. Use this method if you want to keep
your action's dependencies separated.

## Debugging in VS Code

While running your local server (`aio app run`), both UI and actions can be debugged, to do so open the vscode debugger
and select the debugging configuration called `WebAndActions`.
Alternatively, there are also debug configs for only UI and each separate action.

## Typescript support for UI

To use typescript use `.tsx` extension for react components and add a `tsconfig.json`
and make sure you have the below config added
```
{
"compilerOptions": {
"jsx": "react"
}
}
```
92 changes: 92 additions & 0 deletions tools/appbuilder/actions/cart-service/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* <license header>
*/

/**
* This is a sample action showcasing how to access an external API
*
* Note:
* You might want to disable authentication and authorization checks against Adobe Identity Management System for a generic action. In that case:

Check failure on line 9 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 145. Maximum allowed is 100
* - Remove the require-adobe-auth annotation for this action in the manifest.yml of your application

Check failure on line 10 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 103. Maximum allowed is 100
* - Remove the Authorization header from the array passed in checkMissingRequestInputs
* - The two steps above imply that every client knowing the URL to this deployed action will be able to invoke it without any authentication and authorization checks against Adobe Identity Management System

Check failure on line 12 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 209. Maximum allowed is 100
* - Make sure to validate these changes against your security requirements before deploying the action

Check failure on line 13 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 105. Maximum allowed is 100
*/

const fetch = require('node-fetch')

Check failure on line 16 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
const { Core } = require('@adobe/aio-sdk')

Check failure on line 17 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module '@adobe/aio-sdk'

Check failure on line 17 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
const { errorResponse, getBearerToken, stringParameters, checkMissingRequestInputs } = require('../utils')

Check failure on line 18 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

Expected a line break after this opening brace

Check failure on line 18 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

'getBearerToken' is assigned a value but never used

Check failure on line 18 in tools/appbuilder/actions/cart-service/authentication.js

View workflow job for this annotation

GitHub Actions / build

Expected a line break before this closing brace


const cache = {
date: 0,
token: '',
};

// main function that will be executed by Adobe I/O Runtime
async function main(params) {
// create a Logger
const logger = Core.Logger('main', { level: 'debug' })

try {
// 'info' is the default level if not set
logger.info('Calling the main action')

// log parameters, only if params.LOG_LEVEL === 'debug'
logger.error(stringParameters(params))
logger.error(stringParameters(cache))

// check for missing request input parameters and headers
const requiredParams = [ 'PETHEALTH_USERNAME' , 'PETHEALTH_API_KEY', 'PETHEALTH_BASE_DOMAIN']
const requiredHeaders = []
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
// return and log client errors
return errorResponse(400, errorMessage, logger)
}

const AUTHENTICATION_URL = `${params['PETHEALTH_BASE_DOMAIN']}/Auth/Login`;
logger.info('Will query ' + AUTHENTICATION_URL)
const token = await getToken(params.PETHEALTH_USERNAME, params.PETHEALTH_API_KEY, AUTHENTICATION_URL)

return {PETHEALTH_TOKEN: token, ...params }
//return errorResponse(500, 'server error', logger)
} catch (error) {
// log any server errors
logger.error(error)
// return with 500
return errorResponse(500, 'server error', logger)
}
}

async function getToken(user, key, authUrl) {
const now = Date.now();
if (cache.date === undefined || now - cache.date > 60 * 60 * 3) {
cache.date = now;
cache.token = await fetchToken(user, key, authUrl);
}

return cache.token;
}

async function fetchToken(user, key, authUrl) {
const data = {
username: user,
apI_KEY: key
}
const res = await fetch(authUrl, {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' }
})

if (res.status !== 200 && res.status !== 401 && res.status !== 403 && res.status !== 404) {
throw Error(`unexpected error with status '${res.status}' from '${authUrl.toString()}', please report this error`)
}

const response = await (res.json())

return response.token;
}

exports.main = main
Loading
Loading