Skip to content

Latest commit

 

History

History
149 lines (114 loc) · 6.49 KB

File metadata and controls

149 lines (114 loc) · 6.49 KB

CircleCI Coverage Status Docs Greenkeeper badge

Google Cloud Platform (Datastore + Cloud Functions) serverless codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld API Spec.

This codebase was created to demonstrate a fully fledged fullstack application built with Google Cloud Platform (Datastore + Cloud Functions) including CRUD operations, authentication, routing, pagination, and more.

We've gone to great lengths to adhere to community style guides & best practices.

For more information on how to this works with other frontends/backends, head over to the RealWorld repo.

How it works

Background

This codebase is meant to be deployed to Google Cloud Functions, a serverless environment which allows you to run NodeJS code in reponse to events like HTTP triggers scaling up and down elastically without the need for spinning up or maintaining servers.

Life of a Request

For every API call made, a new invocation of the top level function index.js occurs. It calls Router.js which parses the HTTP route and calls the appropriate handler in User.js or Article.js. The handler applies business logic and returns a response which is marshalled back to the caller by Cloud Functions.

For data persistence, Google Cloud Datastore is used which is a fully managed NoSQL database as a service. Cloud Datastore multitenancy is supported and can be leveraged by specifying a DATASTORE_NAMESPACE environment variable at runtime.

Datastore Schema

User

{ username: 'Julie_Stracke',
  password: '$2a$05$Unbla43FRv5Zb...',
  email: 'Julie_Stracke@foomail.com',
  image: '',
  bio: '',
  followers: [],
  following: [],
  [Symbol(KEY)]:
   Key {
     namespace: 'test-unit',
     name: 'Julie_Stracke',
     kind: 'User',
     path: [Getter] } }

Article

{ slug: 'In-enim-nur2bx',
  title: 'In enim',
  description: 'Suscipit voluptas minima...',
  body: 'Voluptates doloremque unde...',
  tagList: [ 'temporibus', 'quae', 'omnis', 'aut' ],
  createdAt: 1509036552780,
  updatedAt: 1509036552780,
  author: 'author_Paul_Heaney',
  favoritedBy: [],
  [Symbol(KEY)]:
   Key {
     namespace: 'test-unit',
     name: 'In-enim-nur2bx',
     kind: 'Article',
     path: [Getter] } }

Comment

{ body: 'Voluptatem dolorem repellat...',
  author: 'Wiegand_Hattie',
  createdAt: 1509036740596,
  updatedAt: 1509036740596,
  [Symbol(KEY)]:
   Key {
     namespace: 'test-unit',
     id: '5629499534213120',
     kind: 'Comment',
     parent:
      Key {
        namespace: 'test-unit',
        name: 'In-enim-nur2bx',
        kind: 'Article',
        path: [Getter] },
     path: [Getter] } }

Testing

Unit Tests

Unit tests live adjacent to source code as src/*.test.js and can be run by executing npm run test:unit. They use mocha as a test runner and istanbul/nyc for coverage.

API Tests

You can also run Postman based API tests by executing npm run test:api. These are run using the newman command line runner. The code is deployed locally to a Cloud Functions Local Emulator environment and tested. See run-api-tests.sh for details.

Linting

Code linting is enforced using eslint configured by .eslintrc.js. You can run the linter by executing npm run lint.

CI

You can see recent build and test runs in CircleCI.

Getting started

Setup Google Cloud Platform (GCP)

  • Signup for a Google Cloud Platform (GCP) Free Account here and create a new project
  • Setup Cloud Datastore for this project by following steps here
  • Install and setup gcloud CLI tool by following help here

Setup local development environment

  • Clone this repo
  • Create GCP Datastore indexes
gcloud datastore create-indexes index.yaml
  • Create a service account and store the credentials as service-account-key.json in the repo root folder - DO NOT SHARE THIS FILE!
  • Ensure Datastore indexes are created before proceeding by checking here
  • Specify details about your GCP project,
export GOOGLE_APPLICATION_CREDENTIALS=service-account-key.json
export GCP_PROJECT_ID=<your-GCP-project-id>
  • Install dependencies
npm install
npx functions config set projectId $GCP_PROJECT_ID
  • Test
npm test

Deploy to Cloud Functions

Note: The Cloud Functions Runtime follows the Node LTS schedule and is currently at Node v6.11.1. This codebase needs Node v8 due its use of async/await and as such currently will not run until Cloud Functions Runtime upgrades to Node v8 sometime after October 31, 2017. You can still deploy it though.

To deploy the code to Cloud Functions environment, execute:

gcloud beta functions deploy api --trigger-http --stage-bucket gs://$GCP_PROJECT_ID.appspot.com