Unverified Commit f827bf0c authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Update Redis example to use Tailwind. (#23131)

And an improved design 😄 

https://roadmap-redis.vercel.app/
parent 34e94097
......@@ -2,67 +2,31 @@
This example showcases how to use Redis as a data store in a Next.js project.
The example is a 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.
- Users can add and upvote items (features in the roadmap)
- Users can 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
See
[https://roadmap.upstash.com](https://roadmap.upstash.com)
- [https://roadmap-redis.vercel.app/](https://roadmap-redis.vercel.app/)
## Deploy Your Own
You can deploy Roadmap Voting App for your project/company
using [Vercel and Upstash](https://vercel.com/integrations/upstash) clicking the below button:
This examples uses [Upstash](https://upstash.com) (Serverless Redis Database) as its data storage. During deployment,
you will be asked to connect with Upstash. The integration will help you create a free Redis database and link it to your Vercel project automatically.
[![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)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-redis&project-name=redis-roadmap&repository-name=redis-roadmap&demo-title=Redis%20Roadmap&demo-description=Create%20and%20upvote%20features%20for%20your%20product.&demo-url=https%3A%2F%2Froadmap-redis.vercel.app%2F&integration-ids=oac_V3R1GIpkoJorr6fqyiwdhl17)
## Configuration
## How to use
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:
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:
### Deployment Steps
```bash
npx create-next-app --example with-redis roadmap
# or
yarn create next-app --example with-redis roadmap
```
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/>
You can sign up/sign in the following dialog:
<img src="./docs/s3.png" width="300" />
Create a free database:
<img src="./docs/s4.png" width="300" />
Select your database and the Vercel project:
<img src="./docs/s5.png" width="300" />
Click `COMPLETE ON VERCEL` button:
<img src="./docs/s6.png" width="300" />
Finish you deployment by choosing a repository to host the project. In the next step, set the URL of your project's
logo:
<img src="./docs/s7.png" width="300" />
Your Roadmap Voting Page should be ready:
<img src="./docs/s8.png" width="300" />
### Maintenance
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`.
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import Redis from 'ioredis'
const redis = new Redis(process.env.REDIS_URL)
export default redis
{
"name": "with-redis",
"version": "0.1.1",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"ioredis": "^4.22.0",
"autoprefixer": "^10.0.4",
"clsx": "^1.1.1",
"ioredis": "^4.23.0",
"next": "latest",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-toastify": "^6.0.8"
"postcss": "^8.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-hot-toast": "^1.0.2",
"swr": "^0.5.1",
"tailwindcss": "^2.0.2",
"uuid": "^8.3.2"
},
"devDependencies": {
"prettier": "^2.2.1"
"@tailwindcss/jit": "0.1.1"
}
}
import './index.css'
import 'react-toastify/dist/ReactToastify.css'
import '../styles/globals.css'
import { Toaster } from 'react-hot-toast'
// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<Toaster
position="bottom-right"
toastOptions={{
style: {
margin: '40px',
background: '#363636',
color: '#fff',
zIndex: 1,
},
duration: 5000,
}}
/>
</>
)
}
export default MyApp
import { getRedis } from './utils'
import { v4 as uuidv4 } from 'uuid'
import redis from '../../lib/redis'
export default async function upvote(req, res) {
const { title } = req.body
module.exports = async (req, res) => {
let redis = getRedis()
const body = req.body
const title = body['title']
if (!title) {
redis.quit()
res.json({
res.status(400).json({
error: 'Feature can not be empty',
})
} else if (title.length < 70) {
await redis.zadd('roadmap', 'NX', 1, title)
redis.quit()
res.json({
} else if (title.length < 150) {
const id = uuidv4()
const newEntry = {
id,
title,
created_at: Date.now(),
score: 1,
ip: 'NA',
}
await redis.hset('features', id, JSON.stringify(newEntry))
res.status(200).json({
body: 'success',
})
} else {
redis.quit()
res.json({
error: 'Max 70 characters please.',
res.status(400).json({
error: 'Max 150 characters please.',
})
}
}
import redis from '../../lib/redis'
export default async function upvote(req, res) {
const features = (await redis.hvals('features'))
.map((entry) => JSON.parse(entry))
.sort((a, b) => b.score - a.score)
res.status(200).json({ features })
}
import { getRedis } from './utils'
module.exports = async (req, res) => {
let redis = getRedis()
let n = await redis.zrevrange('roadmap', 0, 100, 'WITHSCORES')
let result = []
for (let i = 0; i < n.length - 1; i += 2) {
let item = {}
item['title'] = n[i]
item['score'] = n[i + 1]
result.push(item)
}
redis.quit()
res.json({
body: result,
})
}
import { getRedis } from './utils'
import redis from '../../lib/redis'
module.exports = async (req, res) => {
let redis = getRedis()
const body = req.body
const email = body['email']
redis.on('error', function (err) {
throw err
})
export default async function upvote(req, res) {
const { email } = req.body
if (email && validateEmail(email)) {
await redis.sadd('emails', email)
redis.quit()
res.json({
res.status(201).json({
body: 'success',
})
} else {
redis.quit()
res.json({
res.status(400).json({
error: 'Invalid email',
})
}
......
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))
}
import { getRedis } from './utils'
import redis from '../../lib/redis'
module.exports = async (req, res) => {
let redis = getRedis()
const body = req.body
const title = body['title']
let ip = req.headers['x-forwarded-for'] || req.headers['Remote_Addr'] || 'NA'
let c = ip === 'NA' ? 1 : await redis.sadd('s:' + title, ip)
if (c === 0) {
redis.quit()
res.json({
export default async function upvote(req, res) {
const { title, id } = req.body
const ip =
req.headers['x-forwarded-for'] || req.headers['Remote_Addr'] || 'NA'
const count = ip === 'NA' ? 1 : await redis.sadd('s:' + title, ip)
if (count === 0) {
res.status(400).json({
error: 'You can not vote an item multiple times',
})
} else {
let v = await redis.zincrby('roadmap', 1, title)
redis.quit()
res.json({
body: v,
})
const entry = JSON.parse((await redis.hget('features', id)) || 'null')
const updated = {
...entry,
score: entry.score + 1,
ip,
}
await redis.hset('features', id, JSON.stringify(updated))
return res.status(201).json(updated)
}
}
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 3rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer img {
margin-left: 0.5rem;
margin-right: 0.5rem;
height: 1.5em;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
a {
color: inherit;
text-decoration: none;
}
.title a {
color: #0070f3;
text-decoration: none;
}
.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}
.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}
.title,
.description {
text-align: center;
}
.description {
line-height: 1.5;
font-size: 1.5rem;
}
code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 1rem;
}
.card {
margin: 0.2rem;
flex-basis: 85%;
padding: 1.1rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
font-size: 1.25rem;
transition: color 0.15s ease, border-color 0.15s ease;
}
.logo {
width: 5em;
}
.upvotediv {
color: #0070f3;
float: right;
font-size: 1.9rem;
}
.blue {
color: #0070f3;
}
.upvotediv:hover,
.upvotediv:focus,
.upvotediv:active {
color: #e40d34;
}
.noborder {
border: none;
background: transparent;
font-size: 1.25rem;
width: 80%;
padding: 5px;
}
.button {
background: transparent;
font-size: 1.25rem;
float: right;
cursor: pointer;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
This diff is collapsed.
module.exports = {
plugins: {
'@tailwindcss/jit': {},
autoprefixer: {},
},
}
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 46 61"><g clip-path="url(#clip0)"><path fill="#00E9A3" d="M.098 53.118c10.045 9.946 26.33 9.946 36.375 0 10.045-9.945 10.045-26.07 0-36.017l-4.546 4.503a18.968 18.968 0 010 27.012c-7.534 7.46-19.749 7.46-27.282 0L.098 53.118z"/><path fill="#00E9A3" d="M9.192 44.114c5.022 4.973 13.165 4.973 18.188 0a12.645 12.645 0 000-18.008l-4.547 4.502a6.323 6.323 0 010 9.004c-2.512 2.487-6.583 2.487-9.094 0l-4.547 4.502zM45.567 8.097c-10.045-9.946-26.33-9.946-36.376 0-10.045 9.946-10.045 26.071 0 36.017l4.547-4.502a18.968 18.968 0 010-27.013c7.534-7.46 19.748-7.46 27.282 0l4.547-4.502z"/><path fill="#00E9A3" d="M36.473 17.101c-5.022-4.972-13.165-4.972-18.188 0a12.645 12.645 0 000 18.009l4.547-4.502a6.323 6.323 0 010-9.005c2.512-2.486 6.583-2.486 9.094 0l4.547-4.502z"/><path fill="#fff" fill-opacity=".8" d="M45.567 8.097c-10.045-9.946-26.33-9.946-36.376 0-10.045 9.946-10.045 26.071 0 36.017l4.547-4.502a18.968 18.968 0 010-27.013c7.534-7.46 19.748-7.46 27.282 0l4.547-4.502z"/><path fill="#fff" fill-opacity=".8" d="M36.473 17.101c-5.022-4.972-13.165-4.972-18.188 0a12.645 12.645 0 000 18.009l4.547-4.502a6.323 6.323 0 010-9.005c2.512-2.486 6.583-2.486 9.094 0l4.547-4.502z"/></g><defs><clipPath id="clip0"><path fill="#fff" d="M0 0h45.574v60.104H0z" transform="translate(.043 .53)"/></clipPath></defs></svg>
\ No newline at end of file
<svg width="283" height="64" viewBox="0 0 283 64" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/>
</svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 19"><path fill="#000" fill-rule="evenodd" stroke="#000" stroke-width="1.5" d="M12.04 2L2.082 18H22L12.04 2z" clip-rule="evenodd"/></svg>
\ No newline at end of file
@tailwind base;
@tailwind components;
@tailwind utilities;
body {
@apply bg-gray-50;
}
input {
/** Remove shadow on Safari iOS */
-webkit-appearance: none;
}
module.exports = {
purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment