-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into fix/ssr-optimize-css
- Loading branch information
Showing
27 changed files
with
156 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,68 @@ | ||
# Redis Example | ||
# Redis Example (with Upstash) | ||
|
||
This example showcases how to use Redis as a data store in a Next.js project. [Lambda Store](https://lambda.store/) is used as managed Redis service. | ||
This example showcases how to use Redis as a data store in a Next.js project. | ||
|
||
The example is a basic roadmap voting application where users can enter and vote for feature requests. It features the following: | ||
The example is a roadmap voting application where users can enter and vote for feature requests. It features the | ||
following: | ||
|
||
- Users can add and upvote items (features in the roadmap), and enter their email addresses to be notified about the released items. | ||
- The API records the ip-addresses of the voters, so it does not allow multiple votes on the same item from the same IP address. | ||
- To find the id of any item, click the vote button, you will see its id on the url. | ||
- Users can add and upvote items (features in the roadmap), and enter their email addresses to be notified about the | ||
released items. | ||
- The API records the ip-addresses of the voters, so it does not allow multiple votes on the same item from the same IP | ||
address. | ||
|
||
## Demo | ||
|
||
[https://roadmap-voting-demo.vercel.app/](https://roadmap-voting-demo.vercel.app/) | ||
See | ||
[https://roadmap.upstash.com](https://roadmap.upstash.com) | ||
|
||
## Deploy your own | ||
## Deploy Your Own | ||
|
||
Once you have access to [the environment variables you'll need](#configuration), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): | ||
You can deploy Roadmap Voting App for your project/company | ||
using [Vercel and Upstash](https://vercel.com/integrations/upstash) clicking the below button: | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-redis&project-name=with-redis&repository-name=with-redis&env=REDIS_URL&envDescription=Required%20to%20connect%20the%20app%20to%20Redis&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-redis%23configuration) | ||
|
||
## How to use | ||
|
||
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: | ||
|
||
```bash | ||
npx create-next-app --example with-redis with-redis-app | ||
# or | ||
yarn create next-app --example with-redis with-redis-app | ||
``` | ||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fupstash%2Fserverless-tutorials%2Ftree%2Fmaster%2Froadmap-voting-app&env=LOGO&envDescription=Enter%20URL%20for%20your%20project%2Fcompany%20logo&envLink=https%3A%2F%2Fdocs.upstash.com%2Fdocs%2Ftutorials%2Froadmap_voting_app&project-name=roadmap-voting&repo-name=roadmap-voting&demo-title=Roadmap%20Voting&demo-description=Roadmap%20Voting%20Page%20for%20Your%20Project&demo-url=https%3A%2F%2Froadmap.upstash.com&integration-ids=oac_V3R1GIpkoJorr6fqyiwdhl17) | ||
|
||
## Configuration | ||
|
||
A data store with Redis is required for the app to work. In the steps below we'll integrate Lambda Store as the data store. | ||
|
||
### Without Vercel | ||
|
||
If you are planning to deploy your application to somewhere other than Vercel, you'll need to integrate Lambda Store by setting an environment variable. | ||
|
||
First, create an account and a database in the [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 on **Connect** in the Database page within the Lambda Store dashboard as below: | ||
|
||
![setup without vercel](./docs/lstr6.png) | ||
|
||
Next, create a file called `.env.local` in the root directory and copy your connection string: | ||
|
||
```bash | ||
REDIS_URL="YOUR_REDIS_CONNECTION_STRING" | ||
REDIS_PASSWORD="YOUR_REDIS_CONNECTION_PASSWORD" | ||
``` | ||
|
||
Your app is now connected to a remote Redis database! | ||
|
||
### Using Vercel | ||
|
||
You can add the Lambda Store integration to your Vercel account. Once you set up the integration you won't have to visit the Lambda Store console anymore. Follow the next steps to setup the integration: | ||
|
||
#### Step 1. Deploy Your Local Project | ||
|
||
To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example). | ||
|
||
#### Step 2. Add the Lambda Store Integration to Your Vercel Account | ||
|
||
Visit Vercel [Lambda Store Integration](https://vercel.com/integrations/lambdastore) page and click the `Add` button. | ||
The application uses [Upstash](https://upstash.com) (Serverless Redis Database) as its data storage. During deployment, | ||
you will be asked to integrate Upstash. The integration dialog will help you create an Upstash database for free and | ||
link it to your Vercel project with the following steps: | ||
|
||
#### Step 3. Configure the Integration | ||
### Deployment Steps | ||
|
||
The integration requires a [Developer API Key](howto/developerapi.md) that can be created from the [Lambda Store console](https://console.lambda.store). | ||
After clicking the deploy button, enter a name for your project. Then you will be asked to install Upstash integration. | ||
<br/> | ||
<img src="./docs/s2.png" width="300" /> | ||
<br/> | ||
|
||
Enter the API key and your registered email address in the integration setup page: | ||
You can sign up/sign in the following dialog: | ||
|
||
![setup](./docs/lstr1.png) | ||
<img src="./docs/s3.png" width="300" /> | ||
|
||
#### Step 4. Create a Database | ||
Create a free database: | ||
|
||
In the next page of the integration setup, your databases will be automatically listed. A new database can be created from the Vercel Integration page as well as in the Lambda Store Console: | ||
<img src="./docs/s4.png" width="300" /> | ||
|
||
![new db](./docs/lstr2.png) | ||
Select your database and the Vercel project: | ||
|
||
Click the **New Database**, you should be able to see the page below: | ||
<img src="./docs/s5.png" width="300" /> | ||
|
||
![new db form](./docs/lstr3.png) | ||
Click `COMPLETE ON VERCEL` button: | ||
|
||
Fill out the form and click on **Create** to have your new database. | ||
<img src="./docs/s6.png" width="300" /> | ||
|
||
#### Step 5. Link the Database to Your Project | ||
Finish you deployment by choosing a repository to host the project. In the next step, set the URL of your project's | ||
logo: | ||
|
||
Select your project from the dropdown menu then click on **Link To Project** for any database. | ||
<img src="./docs/s7.png" width="300" /> | ||
|
||
`REDIS_URL` will be automatically set as an environment variable for your application. | ||
Your Roadmap Voting Page should be ready: | ||
|
||
![link project](./docs/lstr4.png) | ||
<img src="./docs/s8.png" width="300" /> | ||
|
||
![redis url env](./docs/lstr5.png) | ||
### Maintenance | ||
|
||
**Important:** You will need to re-deploy your application for the change to be effective. | ||
The application uses a Redis database to store the feature requests and emails. The features requests are kept in a | ||
sorted set with name `roadmap`. You can connect to it via Redis-cli and manage the data using the | ||
command `zrange roadmap 0 1000 WITHSCORES`. The emails are stored in a set with name `emails`. So you can get the list | ||
by the command `smembers emails`. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
{ | ||
"name": "with-redis", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"ioredis": "^4.22.0", | ||
"next": "latest", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"react-toastify": "^6.0.8", | ||
"redis": "^3.0.2", | ||
"uuid": "^8.2.0" | ||
"react": "17.0.1", | ||
"react-dom": "17.0.1", | ||
"react-toastify": "^6.0.8" | ||
}, | ||
"license": "MIT" | ||
"devDependencies": { | ||
"prettier": "^2.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,24 @@ | ||
import redis from 'redis' | ||
import { promisify } from 'util' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
export default async function create(req, res) { | ||
const client = redis.createClient({ | ||
url: process.env.REDIS_URL, | ||
}) | ||
if (process.env.REDIS_PASSWORD) { | ||
client.auth(process.env.REDIS_PASSWORD) | ||
} | ||
const hsetAsync = promisify(client.hset).bind(client) | ||
const zaddAsync = promisify(client.zadd).bind(client) | ||
import { getRedis } from './utils' | ||
|
||
module.exports = async (req, res) => { | ||
let redis = getRedis() | ||
const body = req.body | ||
const title = body['title'] | ||
const id = uuidv4() | ||
|
||
client.on('error', function (err) { | ||
throw err | ||
}) | ||
|
||
if (title) { | ||
await zaddAsync('roadmap', 0, id) | ||
await hsetAsync(id, 'title', title) | ||
client.quit() | ||
if (!title) { | ||
redis.quit() | ||
res.json({ | ||
error: 'Feature can not be empty', | ||
}) | ||
} else if (title.length < 70) { | ||
await redis.zadd('roadmap', 'NX', 1, title) | ||
redis.quit() | ||
res.json({ | ||
body: 'success', | ||
}) | ||
} else { | ||
client.quit() | ||
redis.quit() | ||
res.json({ | ||
error: 'Feature can not be empty', | ||
error: 'Max 70 characters please.', | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Redis from 'ioredis' | ||
|
||
function fixUrl(url) { | ||
if (!url) { | ||
return '' | ||
} | ||
if (url.startsWith('redis://') && !url.startsWith('redis://:')) { | ||
return url.replace('redis://', 'redis://:') | ||
} | ||
if (url.startsWith('rediss://') && !url.startsWith('rediss://:')) { | ||
return url.replace('rediss://', 'rediss://:') | ||
} | ||
return url | ||
} | ||
|
||
export function getRedis() { | ||
return new Redis(fixUrl(process.env.REDIS_URL)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.