Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
update functions for ES6
fix lint
  • Loading branch information
svenanderson committed Aug 2, 2020
1 parent 63ae15d commit 9fdf1ec
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 141 deletions.
30 changes: 23 additions & 7 deletions examples/with-redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,35 @@ yarn create next-app --example with-redis roadmap-voting-app

## Configuration

Your application is running but its backend is not configured. Now we will integrate Lambda Store as data store.
Your application is running but its backend is not configured. Now we will integrate Lambda Store as data store. If you are planning to deploy to Vercel, you can leverage Vercel-Lambda Store Integration. Otherwise, you can still configure your application via adding environment variable to your Next.js application. We will explain both approaches.

### Step-1 Add Lambda Store Integration to Your Vercel Account
### Configuration (without Vercel)

If you are planning to deploy your application to somewhere other than Vercel, you can still integrate Lambda Store by setting the environment variable. First create an account and a database in [Lambda Store console](https://console.lambda.store/). To connect to Redis, you will need your Redis connection string. You can get the connection string by clicking "Connect" on the Database page within the Lambda Store dashboard as below:
<img src="https://docs.lambda.store/img/vercel/vercel5.png" width="800" />

Create a file `.env.local` in your application directory and copy your connection string as below:
`REDIS_URL="YOUR_REDIS_CONNECTION_STRING"`

Now, you can deploy your application locally or remotely as it is properly configured to access Redis database remotely.

### Configuration (within Vercel)

You can integrate Lambda Store to your Vercel account. The good thing with integration is that once you set up integration, you do not have to visit Lambda Store console anymore. Here the steps for integration and deployment:

#### Step-1 Add Lambda Store Integration to Your Vercel Account

Visit Vercel [Lambda Store Integration](https://vercel.com/integrations/lambdastore) page and click the `Add` button.

### Step-2 Configure Integration
#### Step-2 Configure Integration

Integration requires [Developer API Key](howto/developerapi.md) which can be created from the [Lambda Store console](https://console.lambda.store).

Enter the API key and your registered email address in the integration setup page as below:

<img src="https://docs.lambda.store/img/vercel/vercel1.png" width="800" />

### Step-3 Create Database
#### Step-3 Create Database

In next screen, your databases will be automatically listed.

Expand All @@ -52,7 +66,7 @@ After clicking `New Database` button then create a database as below:

<img src="https://docs.lambda.store/img/vercel/vercel3.png" width="800" />

### Step-4 Link Database to Your Project
#### Step-4 Link Database to Your Project

Select your project from the dropdown menu then click `Link To Project` for any database.

Expand All @@ -64,11 +78,13 @@ Select your project from the dropdown menu then click `Link To Project` for any

You need to re-deploy your application for the environment variable to be effective. We will do this in the next step.

### Step-5 Customize and Deploy the Application
#### Step-5 Customize and Deploy the Application

To customize the application, clone the code from your github repository (that is either created via Vercel Deploy or npx create-next-app). Then you can modify the code, for example replace `public/logo.png` with your project's own logo. Once you commit and push your changes, Vercel will deploy your application automatically. For more about deployment options see [Vercel Documentation](https://nextjs.org/docs/deployment)).

Now you can test your application, you should be able to add new features. This application has three functionality:
## Current Functionality

The current application has three functionality:

- Users can add new features. If you want to remove or edit any item, use lambda store console to connect your Redis database via Redis-cli. To find id of any feature item, click the vote button, you will see its id on the url. Using this id you edit the item via `hset <item_id> title "new title"`

Expand Down
9 changes: 4 additions & 5 deletions examples/with-redis/pages/api/addemail.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const redis = require('redis')
const { promisify } = require('util')
const { v4: uuidv4 } = require('uuid')
import redis from 'redis'
import { promisify } from 'util'

module.exports = async (req, res) => {
export default async function addEmail(req, res) {
const client = redis.createClient({
url: process.env.REDIS_URL,
})
Expand Down Expand Up @@ -30,6 +29,6 @@ module.exports = async (req, res) => {
}

function validateEmail(email) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(String(email).toLowerCase())
}
8 changes: 4 additions & 4 deletions examples/with-redis/pages/api/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const redis = require('redis')
const { promisify } = require('util')
const { v4: uuidv4 } = require('uuid')
import redis from 'redis'
import { promisify } from 'util'
import { v4 as uuidv4 } from 'uuid'

module.exports = async (req, res) => {
export default async function create(req, res) {
const client = redis.createClient({
url: process.env.REDIS_URL,
})
Expand Down
6 changes: 3 additions & 3 deletions examples/with-redis/pages/api/list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const redis = require('redis')
const { promisify } = require('util')
import redis from 'redis'
import { promisify } from 'util'

module.exports = async (req, res) => {
export default async function list(req, res) {
const client = redis.createClient({
url: process.env.REDIS_URL,
})
Expand Down
8 changes: 4 additions & 4 deletions examples/with-redis/pages/api/vote.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const redis = require('redis')
const { promisify } = require('util')
import redis from 'redis'
import { promisify } from 'util'

module.exports = async (req, res) => {
export default async function list(req, res) {
const client = redis.createClient({
url: process.env.REDIS_URL,
})
Expand All @@ -13,7 +13,7 @@ module.exports = async (req, res) => {
const id = body['id']
let ip = req.headers['x-forwarded-for']
const saddAsync = promisify(client.sadd).bind(client)
let c = await saddAsync('s:' + id, ip)
let c = await saddAsync('s:' + id, ip ? ip : '-')
if (c === 0) {
client.quit()
res.json({
Expand Down
Loading

0 comments on commit 9fdf1ec

Please sign in to comment.