#!/usr/bin/env node "use strict"; var _chunkXH5LGNWHcjs = require('../chunk-XH5LGNWH.cjs'); require('../chunk-7UDY4VFQ.cjs'); // src/examples/custom-routes.ts var _zod = require('zod'); var users = /* @__PURE__ */ new Map([ ["1", { email: "alice@example.com", id: "1", name: "Alice" }], ["2", { email: "bob@example.com", id: "2", name: "Bob" }] ]); var requestCount = 0; var server = new (0, _chunkXH5LGNWHcjs.FastMCP)({ // Simple authentication - in production, use proper tokens/JWTs authenticate: async (req) => { const authHeader = req.headers.authorization; if (authHeader === "Bearer admin-token") { return { role: "admin", userId: "admin" }; } else if (authHeader === "Bearer user-token") { return { role: "user", userId: "user1" }; } throw new Error("Invalid or missing authentication"); }, name: "custom-routes-example", version: "1.0.0" }); var app = server.getApp(); var getAuth = async (c) => { const req = c.env.incoming; const authHeader = req.headers.authorization; if (authHeader === "Bearer admin-token") { return { role: "admin", userId: "admin" }; } else if (authHeader === "Bearer user-token") { return { role: "user", userId: "user1" }; } return null; }; app.get("/.well-known/openid-configuration", async (c) => { return c.json({ authorization_endpoint: "https://example.com/oauth/authorize", issuer: "https://example.com", jwks_uri: "https://example.com/.well-known/jwks.json", response_types_supported: ["code"], scopes_supported: ["openid", "profile", "email"], subject_types_supported: ["public"], token_endpoint: "https://example.com/oauth/token" }); }); app.get("/.well-known/oauth-protected-resource", async (c) => { return c.json({ authorizationServers: ["https://example.com"], resource: "https://example.com/api", scopesSupported: ["read", "write"] }); }); app.get("/status", async (c) => { return c.json({ message: "Server is running", status: "healthy", timestamp: (/* @__PURE__ */ new Date()).toISOString(), version: "1.0.0" }); }); app.get("/docs", async (c) => { const html = `
/status
/.well-known/openid-configuration
/api/users
/admin
Use one of these tokens in the Authorization header:
Bearer admin-token - Admin accessBearer user-token - User access
# Public endpoint (no auth needed)
curl http://localhost:8080/status
# Private endpoint (auth required)
curl -H "Authorization: Bearer user-token" http://localhost:8080/api/users
`;
return c.html(html);
});
app.get("/public/*", async (c) => {
return c.json({
file: c.req.url,
message: "This would serve static files",
public: true
});
});
app.get("/api/users", async (c) => {
const auth = await getAuth(c);
if (!auth) {
return c.json({ error: "Authentication required" }, 401);
}
const userList = Array.from(users.values());
return c.json({
authenticated_as: auth.userId,
count: userList.length,
role: auth.role,
users: userList
});
});
app.get("/api/users/:id", async (c) => {
const auth = await getAuth(c);
if (!auth) {
return c.json({ error: "Authentication required" }, 401);
}
const id = c.req.param("id");
const user = users.get(id);
if (!user) {
return c.json({ error: "User not found" }, 404);
}
return c.json(user);
});
app.post("/api/users", async (c) => {
const auth = await getAuth(c);
if (!auth) {
return c.json({ error: "Authentication required" }, 401);
}
const body = await c.req.json();
if (!body.name || !body.email) {
return c.json({ error: "Name and email are required" }, 400);
}
const id = String(users.size + 1);
const newUser = {
email: body.email,
id,
name: body.name
};
users.set(id, newUser);
return c.json(newUser, 201);
});
app.put("/api/users/:id", async (c) => {
const auth = await getAuth(c);
if (!auth) {
return c.json({ error: "Authentication required" }, 401);
}
const id = c.req.param("id");
const user = users.get(id);
if (!user) {
return c.json({ error: "User not found" }, 404);
}
const body = await c.req.json();
const updatedUser = { ...user, ...body, id: user.id };
users.set(user.id, updatedUser);
return c.json(updatedUser);
});
app.delete("/api/users/:id", async (c) => {
const auth = await getAuth(c);
if (!auth) {
return c.json({ error: "Authentication required" }, 401);
}
const id = c.req.param("id");
if (!users.has(id)) {
return c.json({ error: "User not found" }, 404);
}
users.delete(id);
return c.body(null, 204);
});
app.get("/admin", async (c) => {
const auth = await getAuth(c);
if (!auth) {
return c.json({ error: "Authentication required" }, 401);
}
if (auth.role !== "admin") {
return c.json({ error: "Admin access required" }, 403);
}
const html = `