Skip to content

Commit

Permalink
Merge pull request #180 from elliottsj/vercel
Browse files Browse the repository at this point in the history
Support Vercel
  • Loading branch information
electerious authored Nov 3, 2020
2 parents 2c74cf0 + 2b3c601 commit 6cf0c2c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 43 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ node_modules

# Dist folder
dist/*
!dist/.gitkeep
!dist/.gitkeep

# Vercel
.vercel
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ env:
- YARN_GPG=no
node_js:
- 'node'
- '12'
- '14'
os:
- windows
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Get Ackee up and running…
- […with Helm](docs/Get%20started.md#with-helm)
- […without Docker](docs/Get%20started.md#without-docker)
- […with Netlify](docs/Get%20started.md#with-netlify)
- […with Vercel](docs/Get%20started.md#with-vercel)
- […with Heroku](docs/Get%20started.md#with-heroku)
- […with Render](docs/Get%20started.md#with-render)

Expand Down
12 changes: 12 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

/**
* A serverless function handler for the '/api' route, for use with Vercel.
* This handler follows the AWS Lambda API; Vercel deployments are opted-in
* using the "NODEJS_AWS_HANDLER_NAME" environment variable defined in vercel.json.
*
* See:
* - https://vercel.com/docs/serverless-functions/supported-languages#node.js
* - https://vercel.com/docs/runtimes#advanced-usage/advanced-node-js-usage/aws-lambda-api
*/
exports.handler = require('../src/serverless').handler
18 changes: 18 additions & 0 deletions docs/Get started.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ Ackee now runs on port `3000` and is only accessible from you local network. It'

Netlify adds a forked version of Ackee to your GitHub repositories. You can always [pull the newest code](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/syncing-a-fork) from the original project. Pushing those changes to your repository will automatically deploy the new version of Ackee on Netlify.

## With Vercel

### 1. Deploy to Vercel

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Felecterious%2FAckee&env=ACKEE_USERNAME,ACKEE_PASSWORD,ACKEE_MONGODB,ACKEE_ALLOW_ORIGIN&envDescription=Environment%20variables%20needed%20to%20configure%20the%20user%20and%20database%20connection&envLink=https%3A%2F%2Fgithub.com%2Felecterious%2FAckee%2Fblob%2Fmaster%2Fdocs%2FOptions.md)

When prompted to select a directory, select the root directory.

### 2. Configure Ackee

* Set the build command: `yarn build`
* Set the output directory: `dist`
* Set environment variables `ACKEE_USERNAME`, `ACKEE_PASSWORD`, `ACKEE_MONGODB`, and `ACKEE_ALLOW_ORIGIN`.

### 3. Updating Ackee

Vercel adds a forked version of Ackee to your GitHub repositories. You can always [pull the newest code](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/syncing-a-fork) from the original project. Pushing those changes to your repository will automatically deploy the new version of Ackee on Vercel.

## With Heroku

### 1. Deploy to Heroku
Expand Down
48 changes: 7 additions & 41 deletions functions/api.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
'use strict'

const { ApolloServer } = require('apollo-server-lambda')
const { UnsignedIntResolver, UnsignedIntTypeDefinition, DateTimeResolver, DateTimeTypeDefinition } = require('graphql-scalars')

const connect = require('../src/utils/connect')
const isDemoMode = require('../src/utils/isDemoMode')
const isDevelopmentMode = require('../src/utils/isDevelopmentMode')
const { createServerlessContext } = require('../src/utils/createContext')

const allowOrigin = process.env.ACKEE_ALLOW_ORIGIN || ''
const dbUrl = process.env.ACKEE_MONGODB || process.env.MONGODB_URI

if (dbUrl == null) {
throw new Error('MongoDB connection URI missing in environment')
}

connect(dbUrl)

const apolloServer = new ApolloServer({
introspection: isDemoMode === true || isDevelopmentMode === true,
playground: isDemoMode === true || isDevelopmentMode === true,
debug: isDevelopmentMode === true,
typeDefs: [
UnsignedIntTypeDefinition,
DateTimeTypeDefinition,
require('../src/types')
],
resolvers: {
UnsignedInt: UnsignedIntResolver,
DateTime: DateTimeResolver,
...require('../src/resolvers')
},
context: createServerlessContext
})

exports.handler = apolloServer.createHandler({
cors: {
origin: allowOrigin === '*' ? true : allowOrigin.split(','),
methods: 'GET,POST,PATCH,OPTIONS',
allowedHeaders: 'Content-Type'
}
})
/**
* A serverless function handler for the '/api' route, for use with Netlify.
*
* See:
* - https://docs.netlify.com/functions/overview/
*/
exports.handler = require('../src/serverless').handler
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@
}
},
"engines": {
"node": ">= 14"
"node": ">= 12"
}
}
43 changes: 43 additions & 0 deletions src/serverless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const { ApolloServer } = require('apollo-server-lambda')
const { UnsignedIntResolver, UnsignedIntTypeDefinition, DateTimeResolver, DateTimeTypeDefinition } = require('graphql-scalars')

const connect = require('./utils/connect')
const isDemoMode = require('./utils/isDemoMode')
const isDevelopmentMode = require('./utils/isDevelopmentMode')
const { createServerlessContext } = require('./utils/createContext')

const allowOrigin = process.env.ACKEE_ALLOW_ORIGIN || ''
const dbUrl = process.env.ACKEE_MONGODB || process.env.MONGODB_URI

if (dbUrl == null) {
throw new Error('MongoDB connection URI missing in environment')
}

connect(dbUrl)

const apolloServer = new ApolloServer({
introspection: isDemoMode === true || isDevelopmentMode === true,
playground: isDemoMode === true || isDevelopmentMode === true,
debug: isDevelopmentMode === true,
typeDefs: [
UnsignedIntTypeDefinition,
DateTimeTypeDefinition,
require('./types')
],
resolvers: {
UnsignedInt: UnsignedIntResolver,
DateTime: DateTimeResolver,
...require('./resolvers')
},
context: createServerlessContext
})

exports.handler = apolloServer.createHandler({
cors: {
origin: allowOrigin === '*' ? true : allowOrigin.split(','),
methods: 'GET,POST,PATCH,OPTIONS',
allowedHeaders: 'Content-Type'
}
})
7 changes: 7 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"build": {
"env": {
"NODEJS_AWS_HANDLER_NAME": "handler"
}
}
}

0 comments on commit 6cf0c2c

Please sign in to comment.