cyi1341 b48c991761 | ||
---|---|---|
src | ||
.gitignore | ||
LICENSE | ||
README.md | ||
package-lock.json | ||
package.json | ||
tsconfig.json | ||
wrangler.toml |
README.md
Cloudflare workers pastebin serverless server example (with basic login)
This project demonstrates the use of Cloudflare Workers with TypeScript. It provides a simple pastebin-like functionality with support for login, search, delete, and add-edit operations on key-value pairs stored in a KVNamespace.
Table of Contents
Introduction
Cloudflare Workers allow you to run serverless functions at the edge, close to the users, providing low-latency responses to their requests. In this project, we use TypeScript to benefit from type safety and better development experience.
Requirements
- Node.js (LTS or newer)
- NPM or Yarn
- Cloudflare account with Workers KV enabled
- Wrangler CLI installed and configured
Installation
- Clone the repository:
git clone https://git.disroot.org/cyi1341/cf-workers-pastebin.git
- Install dependencies:
npm install
or
yarn install
-
Configure the
wrangler.toml
file with your KV namespace id. -
Build the project:
npm run build
or
yarn build
- Deploy the Worker to your Cloudflare account:
wrangler publish
Usage
The following API endpoints are available:
POST /login
: Authenticate with a username and password.- Request body:
{ "username": "your_username", "password": "your_password" }
- Response:
Login successful
or an error message with the corresponding status code.
- Request body:
POST /search
: Search for key-value pairs based on key or value.- Request body:
{ "key": "search_key", "value": "search_value" }
- Response: A JSON array of matching key-value pairs.
- Request body:
POST /delete
: Delete a specific key-value pair or multiple pairs based on certain conditions.- Request body: Various options are available, such as
{ "individual": "key_to_delete" }
,{ "search": "search_term" }
,{ "allBut": "key_to_keep" }
, or{ "emptyField": true, "confirmation": true }
. - Response: A success message or an error message with the corresponding status code.
- Request body: Various options are available, such as
POST /delete-all
: Delete all key-value pairs.- Request body:
{ "confirmation": true }
- Response:
All entries deleted
or an error message with the corresponding status code if confirmation is missing.
- Request body:
POST /add-edit
: Add or edit key-value pairs.- Request body:
{ "keyValuePairs": { "key1": "value1", "key2": "value2" }, "replaceOption": "all" }
. ThereplaceOption
can be "all" or "choice". If "choice" is used, specify an additional field"replaceKeys": ["key1"]
to list the keys to replace. - Response: A success message or an error message with the corresponding status code.
- Request body:
Refer to the code for more details on the expected request format and response structure for each endpoint.
Limitations
- The project uses Cloudflare Workers KV for storage, which has certain limitations on the number of read and write operations per day, as well as the maximum size of each value.
- The login functionality is basic and does not include advanced features like token-based authentication or password hashing.
- The search functionality is limited to exact matches for key and value.