Skip to content

Commit

Permalink
API route working
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutlope committed Jan 7, 2023
1 parent c299419 commit d043dc0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 102 deletions.
1 change: 1 addition & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REPLICATE_API_KEY=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts
.env
31 changes: 7 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
# Next.js + Tailwind CSS Example
# RestorePhotos.io

This example shows how to use [Tailwind CSS](https://tailwindcss.com/) [(v3.2)](https://tailwindcss.com/blog/tailwindcss-v3-2) with Next.js. It follows the steps outlined in the official [Tailwind docs](https://tailwindcss.com/docs/guides/nextjs).
A service that will restore old face photos.

## Deploy your own
## Todos

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-tailwindcss)

[![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-tailwindcss&project-name=with-tailwindcss&repository-name=with-tailwindcss)

## 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), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:

```bash
npx create-next-app --example with-tailwindcss with-tailwindcss-app
```

```bash
yarn create next-app --example with-tailwindcss with-tailwindcss-app
```

```bash
pnpm create next-app --example with-tailwindcss with-tailwindcss-app
```

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)).
- [ ] Get initial API route to work
- [ ] Figure how to feed API an image from the UI
- [ ] Create the UI for uploading an image
- [ ] Get an OG card
50 changes: 50 additions & 0 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { NextApiRequest, NextApiResponse } from "next";

type Data = string;
interface ExtendedNextApiRequest extends NextApiRequest {
body: {
imageUrl: string;
};
}

export default async function handler(req: ExtendedNextApiRequest, res: NextApiResponse<Data>) {
const { imageUrl } = req.query;
// POST request to Replicate to start the image restoration generation process
let startResponse = await fetch("https://api.replicate.com/v1/predictions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Token " + process.env.REPLICATE_API_KEY,
},
body: JSON.stringify({
version: "9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
input: { img: imageUrl, version: "v1.4", scale: 2 },
}),
});

let jsonStartResponse = await startResponse.json();
let endpointUrl = jsonStartResponse.urls.get;

// GET request to get the status of the image restoration process & return the result when it's ready
let restoredImage: string | null = null;
while (!restoredImage) {
// Loop in 1s intervals until the alt text is ready
let finalResponse = await fetch(endpointUrl, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Token " + process.env.REPLICATE_API_KEY,
},
});
let jsonFinalResponse = await finalResponse.json();

if (jsonFinalResponse.status === "succeeded") {
restoredImage = jsonFinalResponse.output;
} else if (jsonFinalResponse.status === "failed") {
break;
} else {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
res.status(200).json(restoredImage ? restoredImage : "Failed to restore image");
}
13 changes: 0 additions & 13 deletions pages/api/hello.ts

This file was deleted.

77 changes: 12 additions & 65 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import type { NextPage } from "next";
import Head from "next/head";

const Home: NextPage = () => {
return (
Expand All @@ -12,75 +11,23 @@ const Home: NextPage = () => {

<main className="flex w-full flex-1 flex-col items-center justify-center px-20 text-center">
<h1 className="text-6xl font-bold">
Welcome to{' '}
Welcome to{" "}
<a className="text-blue-600" href="https://nextjs.org">
Next.js!
RestorePhotos.io!
</a>
</h1>

<p className="mt-3 text-2xl">
Get started by editing{' '}
<code className="rounded-md bg-gray-100 p-3 font-mono text-lg">
pages/index.tsx
</code>
</p>

<div className="mt-6 flex max-w-4xl flex-wrap items-center justify-around sm:w-full">
<a
href="https://nextjs.org/docs"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Documentation &rarr;</h3>
<p className="mt-4 text-xl">
Find in-depth information about Next.js features and its API.
</p>
</a>

<a
href="https://nextjs.org/learn"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Learn &rarr;</h3>
<p className="mt-4 text-xl">
Learn about Next.js in an interactive course with quizzes!
</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Examples &rarr;</h3>
<p className="mt-4 text-xl">
Discover and deploy boilerplate example Next.js projects.
</p>
</a>

<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Deploy &rarr;</h3>
<p className="mt-4 text-xl">
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
{/* Input for a photo upload */}
<input type="file" name="photo" id="photo" />
</main>

<footer className="flex h-24 w-full items-center justify-center border-t">
<a
className="flex items-center justify-center gap-2"
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
<a className="flex items-center justify-center gap-2" href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" target="_blank" rel="noopener noreferrer">
Powered by Next.js, Vercel, and Replicate.
</a>
{/* Also include GitHub Repo */}
</footer>
</div>
)
}
);
};

export default Home
export default Home;

0 comments on commit d043dc0

Please sign in to comment.