Skip to content

Commit

Permalink
feat: separated services
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Apr 21, 2020
0 parents commit 927511d
Show file tree
Hide file tree
Showing 49 changed files with 46,836 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages/*/node_modules
packages/*/build
packages/*/dist
54 changes: 54 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"extends": [
"airbnb",
],
"env": {
"node": true,
"jest": true
},
"rules": {
"default-case": "off",
"semi": [
"error",
"never"
],
"comma-dangle": [
"error",
"always-multiline"
],
"max-len": [
"error",
{
"code": 200
}
],
"no-use-before-define": "off",
"import/prefer-default-export": "off",
"no-param-reassign": "off",
"radix": "off",
"no-console": [
"error",
{
"allow": [
"log",
"info",
"warn",
"error"
]
}
],
"no-plusplus": [
"error",
{
"allowForLoopAfterthoughts": true
}
],
"quotes": [
"error",
"single"
],
"import/no-extraneous-dependencies": "off",
"global-require": "off",
"import/no-dynamic-require": "off",
}
}
13 changes: 13 additions & 0 deletions .github/workflows/auto-approve-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Auto approve PRs from dependabot

on:
pull_request

jobs:
auto-approve:
runs-on: ubuntu-latest
steps:
- uses: hmarr/[email protected]
if: github.actor == 'dependabot[bot]' || github.actor == 'dependabot-preview[bot]'
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
42 changes: 42 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Deploy on release published

on:
release:
types:
- published

jobs:
deploy_prod:
runs-on: ubuntu-latest
strategy:
matrix:
package: [db, api, ui]
steps:
- name: Download release
uses: Legion2/[email protected]
with:
repository: ${{ github.repository }}
token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
tag: latest
file: ${{ matrix.package }}.zip

- name: Unzip release package
run: unzip ${{ matrix.package }}.zip

- name: Setup AWS Credentials
run: |
mkdir ~/.aws
echo "[myapp_prod]
aws_access_key_id = ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key = ${{ secrets.AWS_SECRET_ACCESS_KEY }}" > ~/.aws/credentials
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: Install package
run: npm i

- name: Deploy package
run: npm run deploy-package
84 changes: 84 additions & 0 deletions .github/workflows/test-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Test and release

on:
push:
branches: [ master ]
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
package: [db, api, ui]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: Install root
run: npm ci

- name: Check for vulnerable dependencies in root
run: npm audit --audit-level high

- name: Install package
working-directory: "./packages/${{ matrix.package }}"
run: npm ci

- name: Check for vulnerable dependencies in package
working-directory: "./packages/${{ matrix.package }}"
run: npm audit --audit-level high

- name: Test
working-directory: "./packages/${{ matrix.package }}"
run: npm test

# - name: Deploy staging
# working-directory: "./packages/${{ matrix.package }}"
# if: github.event_name == 'push' && github.ref == 'refs/heads/master' && matrix.package === 'api'
# run: npm run deploy-staging -- --runId=${{ github.run_id }}

- name: Package
working-directory: "./packages/${{ matrix.package }}"
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
run: npm run package

- name: Upload package artifact
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
uses: actions/upload-artifact@v1
with:
name: release
path: "./packages/${{ matrix.package }}/${{ matrix.package }}.zip"

release:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
needs: [test]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: Install root dependencies
run: npm ci

- name: Download package artifact
uses: actions/download-artifact@v1
with:
name: release
path: release

- name: Release
run: npx semantic-release
env:
# NOTE: GitHub Actions won't trigger on release when using the default GITHUB_TOKEN
GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
dist
.DS_Store
stats.json
.serverless
.env
.cache
coverage
.webpack
Empty file added CHANGELOG.md
Empty file.
Empty file added README.md
Empty file.
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "myapp",
"version": "1.0.5",
"private": true,
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/commit-analyzer": "^8.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.0.5",
"@semantic-release/npm": "^7.0.5",
"@semantic-release/release-notes-generator": "^9.0.1",
"eslint": "^5.16.0 || ^6.1.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^1.7.0",
"semantic-release": "^17.0.4"
},
"dependencies": {}
}
18 changes: 18 additions & 0 deletions packages/api/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"presets": [
[
"@babel/preset-env",
{
// "modules": false
}
]
],
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs",
"@babel/plugin-transform-async-to-generator"
]
}
}
}
Empty file added packages/api/README.md
Empty file.
18 changes: 18 additions & 0 deletions packages/api/__mocks__/node-fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { Response, Headers } = jest.requireActual('node-fetch')

const fetch = jest.fn(async () => {
const meta = {
'Content-Type': 'application/json',
Accept: '*/*',
}
const headers = new Headers(meta)
const responseInit = {
status: 200,
statusText: '{ message: \'200\' }',
headers,
}
const response = new Response(JSON.stringify({}), responseInit)
return response
})

module.exports = fetch
20 changes: 20 additions & 0 deletions packages/api/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import DynamoDB from 'aws-sdk/clients/dynamodb'
import dynamo from 'dynamodb'

const baseConfig = {
region: 'us-east-1',
convertEmptyValues: true,
}
const dynamodbOfflineOptions = {
...baseConfig,
region: 'localhost',
endpoint: 'http://localhost:889',
}

const dynamoDb = process.env.IS_OFFLINE ? new DynamoDB.DocumentClient(dynamodbOfflineOptions) : new DynamoDB.DocumentClient(baseConfig)
dynamo.documentClient(dynamoDb)

// dynamo.AWS.config.loadFromPath('credentials.json')
dynamo.AWS.config.update({
region: 'us-east-1',
})
39 changes: 39 additions & 0 deletions packages/api/functions/express/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import userRouter from './routes/user'

const IS_PRODUCTION = process.env.NODE_ENV === 'production'

const app = express()
const router = express.Router()
// router.use(cookieParser())
router.use(cors())
router.use(bodyParser.json())

app.use('/', router)
app.use('/user', userRouter)

app.use((req, res, next) => {
const err = new Error('Not Found')
err.statusCode = 404
next(err)
})

// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
const { statusCode = 500 } = err
const response = {
message: err.message,
}

if (!IS_PRODUCTION) {
response.trace = err.stack
}

res
.status(statusCode)
.json(response)
})

export default app
17 changes: 17 additions & 0 deletions packages/api/functions/express/controllers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import shortId from 'shortid'
import User from '../../../models/User'

export function createUser({
name,
}) {
return User.create({
id: shortId.generate(),
name,
})
}

export function scanUsers() {
return new Promise((resolve) => {
return User.scan().exec((error, data) => resolve(data))
})
}
10 changes: 10 additions & 0 deletions packages/api/functions/express/lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'source-map-support/register'
import serverless from 'serverless-http'
import app from './app'
import '../../aws'

export const handler = serverless(app, {
request(request, event) {
request.requestContext = event.requestContext
},
})
17 changes: 17 additions & 0 deletions packages/api/functions/express/routes/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import express from 'express'
import wrapAsync from '../wrap-async'
import { createUser, scanUsers } from '../controllers/user'

const userRouter = express.Router()
userRouter.get('/', wrapAsync(async (req, res) => {
const users = await scanUsers()
console.log(users)
res.json(users)
}))
userRouter.post('/', wrapAsync(async (req, res) => {
const { name } = req.body
const user = await createUser({ name })
res.json(user)
}))

export default userRouter
3 changes: 3 additions & 0 deletions packages/api/functions/express/wrap-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const wrapAsync = fn => (req, res, next) => fn(req, res, next).catch(next)

export default wrapAsync
Loading

0 comments on commit 927511d

Please sign in to comment.