Skip to content

Commit

Permalink
Upgrade to v1.0.1
Browse files Browse the repository at this point in the history
- Better Docs.
- UI improvements.
- Add Dotenv support.
- Add more API Endpoints.
- Use "Fira Code" font.
  • Loading branch information
iAkashPattnaik committed Feb 17, 2022
1 parent cb64ed0 commit 60fb27f
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 63 deletions.
76 changes: 55 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Dustbin
<p align="center"><img src="https://avatars.githubusercontent.com/u/88306458?s=200&v=4"></p>
<p align="center">It's just another text storage service built in <b>fastify</b>.</p>

It's just another text storage service built in **fastify**.
## Table of Contents

- [Api](#api)
- [Endpoints](#endpoints)
- [Examples](#examples)
- [Developers](#developers)

## API

Expand All @@ -9,47 +15,75 @@ Let's get started with it...

### Endpoints

```diff
+ POST /api/get - Get a paste from the storage
#### The following endpoints are available -

Get a paste from the storage -

```http
POST /api/get HTTP/1.1
Host: dustbin.me
Content-Type: application/json
{ "fileId": "<string>" }
```

The request body is provided in place -

```http
POST /api/new HTTP/1.1
Host: dustbin.me
Content-Type: application/json
{ "data" : "<string>", "language": "string" }
```

Get a paste in browser -

```http
GET /paste/{fileId} HTTP/1.1
Host: dustbin.me
```

Get raw paste in browser -

PARAMETERS
[fileId]: string
```http
GET /paste/{fileId}/raw HTTP/1.1
Host: dustbin.me
```

```diff
+ POST /api/new - Create a new paste
Download a paste in browser -

PARAMETERS
[data]: string
[language]: string
```http
GET /paste/{fileId}/download HTTP/1.1
Host: dustbin.me
```

## Examples

- ### Python
- #### Python

```python
import requests

# Create A New Paste
new_req = requests.post(
'https://dustbin.me/api/new',
json={
'data': 'def main():\n\tprint("Hello World!")\n\nif __name__ == "__main__":\n\tmain()',
'language': 'Python'
})
'https://dustbin.me/api/new',
json={
'data': 'def main():\n\tprint("Hello World!")\n\nif __name__ == "__main__":\n\tmain()',
'language': 'Python',
})
print(new_req.json())
paste_id = new_req.json()['id']

# Get The Same Paste
paste_req = requests.post(
'https://dustbin.me/api/paste/',
json={'fileId': paste_id},
)
'https://dustbin.me/api/paste/',
json={'fileId': paste_id},
)
print(paste_req.json())
```

- ### Dart
- #### Dart

```dart
import 'dart:convert';
Expand Down
11 changes: 11 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 1.0.1

- Better Docs.
- UI improvements.
- Add Dotenv support.
- Add more API Endpoints.
- Use "`Fira Code`" font.

# 1.0.0

- Initial release
45 changes: 24 additions & 21 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dotenv/config';
import Fastify, { FastifyReply, FastifyRequest } from 'fastify';
import { join } from 'path';
import fs from 'fs';
Expand All @@ -8,46 +9,46 @@ interface DustbinDocument {
data?: any;
id: string;
language: string;
}
};


app.get('/tailwind.css', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'tailwind.css'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/css').send(fileBuffer);
})
});
});
app.get('/favicon.png', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'favicon.png'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('image/x-png').send(fileBuffer);
})
});
});
app.get('/script/new.js', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'script', 'new.js'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('application/javascript').send(fileBuffer);
})
});
});
app.get('/script/admin.js', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'script', 'admin.js'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('application/javascript').send(fileBuffer);
})
});
});

app.get('/', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'index.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(fileBuffer);
})
});
});

app.get('/new', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'new.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(fileBuffer);
})
});
});

app.get('/admin', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'admin.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(fileBuffer);
})
});
});

app.get('/paste/*', async (request: FastifyRequest, reply: FastifyReply) => {
Expand All @@ -56,13 +57,21 @@ app.get('/paste/*', async (request: FastifyRequest, reply: FastifyReply) => {
if (fileId == '') {
return reply.redirect('/new');
}
const result: DustbinDocument | null = await api.getDocument(fileId);
const result: DustbinDocument | null = await api.getDocument(fileId.split('/')[0]);
if (!result) return reply.send({ error: 'DOCUMENT_NOT_FOUND' });
if (fileId.split('/').length > 1) {
return fileId.split('/')[1] == 'raw' // /paste/fileId/raw
? reply.type('text/plain').send(result?.data)
: reply // /paste/fileId/[download]
.type('application/octet-stream')
.header('Content-Disposition', `attachment; filename="${fileId.split('/')[0]}.${result?.language}"`)
.send(Buffer.from(result?.data, 'utf8'));
}
fs.readFile(join(__dirname, '..', 'pages', 'paste.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(
Buffer.from(fileBuffer.toString('utf8').replace('{{ %data% }}', result.data).replace('{{ %language% }}', result.language.toLowerCase()))
Buffer.from(fileBuffer.toString('utf8').replace('{{ %data% }}', result?.data).replace('{{ %language% }}', result!.language.toLowerCase()))
);
})
});
});

// -------------------
Expand Down Expand Up @@ -109,13 +118,7 @@ app.post('/api/admin', async (request: FastifyRequest, reply: FastifyReply) => {


// Run the server !
const startServer = async () => {
try {
await app.listen(process.env.PORT || 1134, '0.0.0.0');
console.log(`[ Info ] Dustbin Init On http://localhost:${process.env.PORT || 1134}`);
} catch (err) {
console.error(err);
process.exit(1);
}
};
startServer();
(async () => {
await app.listen(process.env.PORT || 1134, '0.0.0.0');
console.log(`[ Info ] Dustbin Init On https://localhost:${process.env.PORT || 1134}`);
})();
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "dustbin",
"version": "1.0.0",
"version": "1.0.1",
"description": "Yet Another Text Storage Service.",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc -p tsconfig.json && tailwindcss build -i ./pages/tailwind.input.css -o ./pages/tailwind.css",
"build": "tsc --build && tailwindcss build -i ./pages/tailwind.input.css -o ./pages/tailwind.css",
"start": "node build",
"dev": "tsc -p tsconfig.json && node build",
"dev": "tsc --build && node build",
"watch:css": "tailwindcss -i ./pages/tailwind.input.css -o ./pages/tailwind.css --watch"
},
"repository": {
Expand All @@ -30,10 +30,11 @@
"typescript": "^4.5.5"
},
"dependencies": {
"dotenv": "^16.0.0",
"dropbox": "^10.23.0",
"fastify": "^3.27.0",
"pg": "^8.7.1",
"uniqid": "^5.4.0",
"tailwindcss": "^3.0.15"
"tailwindcss": "^3.0.15",
"uniqid": "^5.4.0"
}
}
2 changes: 1 addition & 1 deletion pages/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</a>
</div>
<div class="flex flex-row items-center justify-end pr-2">
<a href="https://github.com/DustbinServer/" class="text-white hover:text-blue-500 text-2xl">Github</a>
<a href="https://github.com/DustbinServer/" target="_blank" class="text-white font-mono hover:text-blue-500 text-xl">Github</a>
</div>
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</a>
</div>
<div class="flex flex-row items-center justify-end pr-2">
<a href="https://github.com/DustbinServer/" class="text-white hover:text-blue-500 text-2xl">Github</a>
<a href="https://github.com/DustbinServer/" target="_blank" class="text-white font-mono hover:text-blue-500 text-xl">Github</a>
</div>
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion pages/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</a>
</div>
<div class="flex flex-row items-center justify-end pr-2">
<a href="https://github.com/DustbinServer/" class="text-white hover:text-blue-500 text-2xl">Github</a>
<a href="https://github.com/DustbinServer/" target="_blank" class="text-white font-mono hover:text-blue-500 text-xl">Github</a>
</div>
</div>
</header>
Expand Down
6 changes: 4 additions & 2 deletions pages/paste.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
<span class="text-black bg-sky-600 font-semibold rounded p-0.5 text-2xl">Bin</span>
</a>
</div>
<div class="flex flex-row items-center justify-end pr-2">
<a href="https://github.com/DustbinServer/" class="text-white hover:text-blue-500 text-2xl">Github</a>
<div class="flex flex-row items-center justify-end pr-2 gap-2">
<button onclick="window.location.href += '/raw'" class="text-white font-mono hover:text-blue-500 text-xl">Raw</button>
<button onclick="window.location.href += '/download'"class="text-white font-mono hover:text-blue-500 text-xl">Download</button>
<a href="https://github.com/DustbinServer/" target="_blank" class="text-white font-mono hover:text-blue-500 text-xl">Github</a>
</div>
</div>
</header>
Expand Down
2 changes: 2 additions & 0 deletions pages/tailwind.input.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url(https://cdn.jsdelivr.net/npm/[email protected]/distr/fira_code.css);

@tailwind base;
@tailwind components;
@tailwind utilities;
6 changes: 5 additions & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module.exports = {
content: ["./pages/**/*.{html,js}"],
theme: {
extend: {},
extend: {
fontFamily: {
mono: ['Fira Code', 'monospace']
},
},
},
plugins: [],
};
14 changes: 4 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
{
"compilerOptions": {
/* Language and Environment */
"target": "es2021",
/* Modules */
"module": "commonjs",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"],
},
"outDir": "./build",
"outDir": "build/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
},
}
"skipLibCheck": true
}
}

0 comments on commit 60fb27f

Please sign in to comment.