mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: paste views; configuration validation; hmacsha256 method; enhance paste clean up serivce; paste view clean up service;
This commit is contained in:
@@ -21,10 +21,9 @@ npm install
|
||||
|
||||
The application requires an environment variable to point to the backend API. Create a `.env` file in this directory or set the variable in your environment:
|
||||
|
||||
```env
|
||||
VITE_API_URL=http://localhost:8080
|
||||
VITE_ALLOWED_HOSTS=localhost
|
||||
```
|
||||
- `VITE_API_URL`: The URL of the backend API (e.g., `http://localhost:8080`).
|
||||
- `VITE_ALLOWED_HOSTS`: A comma separated list of allowed hosts (e.g., `localhost,sharpbin.whoswhip.dev`)
|
||||
- `VIEW_INTERNAL_API_KEY`: Enforces views to be counted/recorded only by the frontend, if set it needs to be the same for both backend and frontend.
|
||||
|
||||
### Running in Development
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import type { Paste } from '$lib/types/paste';
|
||||
import { getServerToken } from '$lib/utils/auth';
|
||||
import { env } from '$env/dynamic/private';
|
||||
|
||||
export const load: PageServerLoad = async ({ params, fetch, url }) => {
|
||||
export const load: PageServerLoad = async ({ params, fetch, url, cookies }) => {
|
||||
const { id } = params;
|
||||
|
||||
const paste = await fetch(`/api/paste/${id}`);
|
||||
@@ -11,9 +13,30 @@ export const load: PageServerLoad = async ({ params, fetch, url }) => {
|
||||
error: { message: 'Paste not found' }
|
||||
};
|
||||
}
|
||||
|
||||
const token = getServerToken(cookies);
|
||||
|
||||
const viewed = await fetch(`/api/paste/${id}/view`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Internal-API-Key': env.VIEW_INTERNAL_API_KEY ?? env.View_HMAC_Internal_API_Key ?? '',
|
||||
Authorization: token ? `Bearer ${token}` : ''
|
||||
}
|
||||
});
|
||||
|
||||
const pasteData = (await paste.json()) as Paste;
|
||||
if (viewed.status === 200) {
|
||||
const viewJson = await viewed.json();
|
||||
if (viewJson.success === true && viewJson.message !== 'View already recorded.') {
|
||||
pasteData.views += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.error(`Failed to increment view count for paste ${id}: ${await viewed.text()}`);
|
||||
}
|
||||
|
||||
const pasteContent = await fetch(`/api/paste/${id}/raw`);
|
||||
const pasteOptions = await fetch('/api/paste/info');
|
||||
const pasteOptions = await fetch('/api/paste/info');
|
||||
const options = await pasteOptions.json();
|
||||
const content = await pasteContent.text();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user