mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
.
This commit is contained in:
36
packages/worker/src/index.ts
Normal file
36
packages/worker/src/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Hono } from "hono"
|
||||
import { cors } from "hono/cors"
|
||||
|
||||
// Create a new Hono app
|
||||
const app = new Hono()
|
||||
|
||||
// Enable CORS for all routes
|
||||
app.use("/*", cors())
|
||||
|
||||
// Health check endpoint
|
||||
app.get("/health", (c) => {
|
||||
return c.json({ status: "ok", message: "Worker is running!" })
|
||||
})
|
||||
|
||||
// Root endpoint
|
||||
app.get("/", (c) => {
|
||||
return c.json({
|
||||
message: "Welcome to the Cloudflare Worker API",
|
||||
endpoints: {
|
||||
health: "/health",
|
||||
api: "/api/v1",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
// Example API endpoint
|
||||
app.get("/api/v1/hello", (c) => {
|
||||
const name = c.req.query("name") || "World"
|
||||
return c.json({ message: `Hello, ${name}!` })
|
||||
})
|
||||
|
||||
// Export the Hono app as default (handles HTTP requests)
|
||||
export default app
|
||||
|
||||
// Export the RPC worker for RPC calls via service bindings
|
||||
export { WorkerRpc } from "./rpc"
|
||||
75
packages/worker/src/rpc.ts
Normal file
75
packages/worker/src/rpc.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { WorkerEntrypoint } from "cloudflare:workers"
|
||||
|
||||
/**
|
||||
* RPC Worker Entrypoint
|
||||
* This class enables RPC-style calls to the worker from service bindings.
|
||||
* Methods defined here can be called directly from other workers or systems
|
||||
* that have a service binding to this worker.
|
||||
*/
|
||||
export class WorkerRpc extends WorkerEntrypoint {
|
||||
/**
|
||||
* Example RPC method - returns a greeting message
|
||||
*/
|
||||
async sayHello(
|
||||
name: string,
|
||||
): Promise<{ message: string; timestamp: number }> {
|
||||
return {
|
||||
message: `Hello, ${name}!`,
|
||||
timestamp: Date.now(),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example RPC method - performs a calculation
|
||||
*/
|
||||
async calculate(
|
||||
operation: "add" | "subtract" | "multiply" | "divide",
|
||||
a: number,
|
||||
b: number,
|
||||
): Promise<number> {
|
||||
switch (operation) {
|
||||
case "add":
|
||||
return a + b
|
||||
case "subtract":
|
||||
return a - b
|
||||
case "multiply":
|
||||
return a * b
|
||||
case "divide":
|
||||
if (b === 0) throw new Error("Division by zero")
|
||||
return a / b
|
||||
default:
|
||||
throw new Error(`Unknown operation: ${operation}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example RPC method - fetches data (could interact with bindings)
|
||||
*/
|
||||
async getData(
|
||||
key: string,
|
||||
): Promise<{ key: string; found: boolean; value?: string }> {
|
||||
// Example: You could interact with KV, D1, R2, etc. here
|
||||
// For now, just return a mock response
|
||||
return {
|
||||
key,
|
||||
found: false,
|
||||
value: undefined,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example RPC method - processes batch operations
|
||||
*/
|
||||
async processBatch(
|
||||
items: string[],
|
||||
): Promise<{ processed: number; items: string[] }> {
|
||||
const processed = items.map((item) => item.toUpperCase())
|
||||
return {
|
||||
processed: items.length,
|
||||
items: processed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export the RPC worker as the default export
|
||||
export default WorkerRpc
|
||||
Reference in New Issue
Block a user