forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-runtimes#57 from eigengravy/generate-short-url
generateShortUrl Function using Deno
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 🌐 Shorten URL | ||
|
||
A Deno Cloud Function for shortening URL from the [Bitly API](https://dev.bitly.com/) and [TinyURL API](https://tinyurl.com/app/dev). | ||
|
||
_Example input:_ | ||
|
||
```json | ||
{ | ||
"provider": "bitly", | ||
"url": "https://google.com/" | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"provider": "tinyurl", | ||
"url": "https://google.com/" | ||
} | ||
``` | ||
|
||
_Example output:_ | ||
|
||
```json | ||
{ | ||
"success": true, | ||
"url": "https://bit.ly/3y3HDkC" | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"success": false, | ||
"message": "provider is required" | ||
} | ||
``` | ||
|
||
## 📝 Variables | ||
|
||
List of variables used by this cloud function: | ||
|
||
**BITLY_TOKEN** - Your Bitly API token. | ||
**TINYURL_TOKEN** - Your TInyURL API token. | ||
|
||
## 🚀 Deployment | ||
|
||
1. Clone this repository, and enter this function folder: | ||
|
||
``` | ||
$ git clone https://github.com/open-runtimes/examples.git && cd examples | ||
$ cd deno/generate_short_url | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
|
||
``` | ||
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=src/mod.ts --rm --interactive --tty --volume $PWD:/usr/code openruntimes/deno:v2-1.24 sh /usr/local/src/build.sh | ||
``` | ||
|
||
As a result, a `code.tar.gz` file will be generated. | ||
|
||
3. Start the Open Runtime: | ||
|
||
``` | ||
docker run -p 3000:3000 -e INTERNAL_RUNTIME_ENTRYPOINT=src/mod.ts -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/deno:v2-1.24 sh /usr/local/src/start.sh | ||
``` | ||
|
||
Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Deno runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/deno-1.24). | ||
|
||
4. Execute function: | ||
|
||
``` | ||
curl http:https://localhost:3000/ -d '{"variables":{"BITLY_TOKEN":"[YOUR_API_KEY]"},"payload": "{\"url\":\"https://appwrite.io/\",\"provider\":\"bitly\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" | ||
``` | ||
|
||
## 📝 Notes | ||
|
||
- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). | ||
- This example is compatible with Deno 1.21 and 1.24. Other versions may work but are not guarenteed to work as they haven't been tested. |
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,67 @@ | ||
export default async function (req: any, res: any) { | ||
const { provider, url } = JSON.parse(req.payload); | ||
|
||
if (!provider) { | ||
res.json({ success: false, message: "provider is required" }); | ||
return; | ||
} | ||
|
||
if (!url) { | ||
res.json({ success: false, message: "url is required" }); | ||
return; | ||
} | ||
|
||
if (provider === "bitly") { | ||
const BITLY_TOKEN = req.variables["BITLY_TOKEN"]; | ||
|
||
if (!BITLY_TOKEN) { | ||
res.json({ success: false, message: "BITLY_TOKEN is required" }); | ||
return; | ||
} | ||
|
||
const response = await fetch(`https://api-ssl.bitly.com/v4/shorten`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${BITLY_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
long_url: url, | ||
}), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
res.json({ success: false, code: response.status, message: `${await response.text()}` }); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
res.json({ success: true, url: data["link"] }); | ||
} else if (provider === "tinyurl") { | ||
const TINYURL_TOKEN = req.variables["TINYURL_TOKEN"]; | ||
|
||
if (!TINYURL_TOKEN) { | ||
res.json({ success: false, message: "TINYURL_TOKEN is required" }); | ||
return; | ||
} | ||
|
||
const response = await fetch(`https://api.tinyurl.com/create`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${TINYURL_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
url: url, | ||
}), | ||
}); | ||
|
||
if (response.status !== 200) { | ||
res.json({ success: false, message: `${await response.text()}` }); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
res.json({ success: true, url: data.data.tiny_url }); | ||
} | ||
} |