mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-21 11:53:10 -07:00
Add CF worker
This commit is contained in:
parent
76b69a2f13
commit
f2f239fb3b
4 changed files with 2689 additions and 0 deletions
2584
package-lock.json
generated
Normal file
2584
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
15
package.json
Normal file
15
package.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "knxbin",
|
||||
"version": "0.0.0",
|
||||
"devDependencies": {
|
||||
"wrangler": "2.1.11"
|
||||
},
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "wrangler dev",
|
||||
"deploy": "wrangler publish"
|
||||
},
|
||||
"dependencies": {
|
||||
"nanoid": "^4.0.0"
|
||||
}
|
||||
}
|
78
src/index.js
Normal file
78
src/index.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
import { nanoid } from 'nanoid'
|
||||
|
||||
export default {
|
||||
async fetch(request, env) {
|
||||
const url = new URL(request.url);
|
||||
const key = url.pathname.slice(1);
|
||||
|
||||
switch (request.method) {
|
||||
case 'OPTIONS':
|
||||
return new Response(null, { headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': '*',
|
||||
'Access-Control-Allow-Credentials': 'true'
|
||||
}})
|
||||
case 'PUT':
|
||||
if(key !== 'documents')
|
||||
return new Response('Unknown operation', { status: 404 });
|
||||
|
||||
const randomKey = nanoid()
|
||||
|
||||
// get string from request body
|
||||
const body = await request.text()
|
||||
|
||||
// if string length is over 10000000, return error
|
||||
if(body.length > 10000000)
|
||||
return new Response('Document too large', { status: 413, headers: { 'Access-Control-Allow-Origin': '*' } })
|
||||
|
||||
await env.PASTES_BUCKET.put(randomKey, body);
|
||||
return new Response('{"key":"' + randomKey + '"}', { status: 200, headers: { 'Access-Control-Allow-Origin': '*' } });
|
||||
case 'GET':
|
||||
if(key.split("/")[0] === 'raw') {
|
||||
const rawKey = url.pathname.slice(5)
|
||||
|
||||
const object = await env.PASTES_BUCKET.get(rawKey);
|
||||
|
||||
if (object === null) {
|
||||
return new Response('{"data":"Object Not Found", "key":' + rawKey + '}', { status: 404, headers: { 'Access-Control-Allow-Origin': '*' } });
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
object.writeHttpMetadata(headers);
|
||||
headers.set('etag', object.httpEtag);
|
||||
headers.set('Access-Control-Allow-Origin', '*')
|
||||
|
||||
const data = await object.text();
|
||||
|
||||
return new Response(data, {
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
const object = await env.PASTES_BUCKET.get(key);
|
||||
|
||||
if (object === null) {
|
||||
return new Response('{"data":"Object Not Found", "key":' + key + '}', { status: 404, headers: { 'Access-Control-Allow-Origin': '*' } });
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
object.writeHttpMetadata(headers);
|
||||
headers.set('etag', object.httpEtag);
|
||||
headers.set('Access-Control-Allow-Origin', '*')
|
||||
|
||||
const data = await object.text();
|
||||
|
||||
return new Response('{"data":' + JSON.stringify(data) + ',"key":"' + key + '"}', {
|
||||
headers,
|
||||
});
|
||||
default:
|
||||
return new Response('Method Not Allowed', {
|
||||
status: 405,
|
||||
headers: {
|
||||
Allow: 'PUT, GET, OPTIONS',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
12
wrangler.toml
Normal file
12
wrangler.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
name = "knxbin"
|
||||
main = "src/index.js"
|
||||
compatibility_date = "2022-11-01"
|
||||
|
||||
account_id = "025f25a08c29a51063e7a310a947a927"
|
||||
|
||||
[[r2_buckets]]
|
||||
binding = 'PASTES_BUCKET'
|
||||
bucket_name = 'pastes-bucket'
|
||||
|
||||
routes = [ { pattern = "api.knx.cool", custom_domain = true, zone_name = "knx.cool" }
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue