Initial commit from template
Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (push) Failing after 15s

This commit is contained in:
purvarao
2025-11-21 16:27:27 +05:30
commit 07a983d24f
21 changed files with 1742 additions and 0 deletions

141
src/index.ts Normal file
View File

@@ -0,0 +1,141 @@
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { getCookie } from 'hono/cookie';
import { decryptAuthCookie } from './services/decrypt-service';
import { handleSwaggerRequest } from './swagger-ui';
import { authMiddleware } from './middleware/auth';
import { saveUserData, getUserByUid } from './services/db-service';
// Create a new Hono app
const app = new Hono();
// Add middleware
app.use('*', async (c, next) => {
console.log(`[${c.req.method}] ${c.req.url}`);
await next();
});
// Add CORS middleware
app.use('*', cors());
// Create a group for protected routes with auth middleware
// Exclude Swagger docs routes from auth middleware
app.use('/api/cf-template/*', async (c, next) => {
const path = new URL(c.req.url).pathname;
// Skip auth for Swagger docs routes
if (path.includes('/docs') || path.includes('/swagger.json') || path.includes('/openapi.json')) {
return next();
}
// Apply auth middleware for all other routes
return authMiddleware(c, next);
});
// Add auth validation endpoint (GET method)
app.get('/api/cf-template/auth/validate', async (c) => {
try {
// Get auth from query parameter
const authToken = c.req.query('auth');
if (!authToken) {
return c.json({ error: 'No auth parameter found' }, 401);
}
// Decrypt the auth token
const decryptedData = await decryptAuthCookie(authToken, c.env);
// Check if the decrypted data contains the expected fields
if (!decryptedData || !decryptedData.firstname || !decryptedData.lastname) {
console.error('Invalid decrypted data format:', decryptedData);
return c.json({ error: 'Invalid auth parameter' }, 401);
}
return c.json(decryptedData, 200);
} catch (error) {
console.error('Authentication error:', error);
return c.json({ error: 'Authentication failed' }, 500);
}
});
// Add auth validation endpoint (POST method)
app.post('/api/cf-template/auth/validate', async (c) => {
try {
// Get auth from request body
const body = await c.req.json();
const authToken = body.auth;
if (!authToken) {
return c.json({ error: 'No auth parameter found in request body' }, 401);
}
// Decrypt the auth token
const decryptedData = await decryptAuthCookie(authToken, c.env);
// Check if the decrypted data contains the expected fields
if (!decryptedData || !decryptedData.firstname || !decryptedData.lastname) {
console.error('Invalid decrypted data format:', decryptedData);
return c.json({ error: 'Invalid auth parameter' }, 401);
}
return c.json(decryptedData, 200);
} catch (error) {
console.error('Authentication error:', error);
return c.json({ error: 'Authentication failed' }, 500);
}
});
// Add endpoint to decrypt and save cookie data to D1 database
app.post('/api/cf-template/auth/decrypt-and-save', async (c) => {
try {
// Get auth token from request body
const body = await c.req.json();
const authToken = body.auth;
if (!authToken) {
return c.json({ error: 'No auth parameter found in request body' }, 401);
}
// Decrypt the auth token
const decryptedData = await decryptAuthCookie(authToken, c.env);
// Check if the decrypted data contains the expected fields
if (!decryptedData || !decryptedData.uid || !decryptedData.email) {
console.error('Invalid decrypted data format:', decryptedData);
return c.json({ error: 'Invalid auth parameter' }, 401);
}
// Save the decrypted data to the D1 database
const result = await saveUserData(decryptedData, c.env);
if (!result.success) {
return c.json({ error: result.error || 'Failed to save user data' }, 500);
}
// Return success response with the saved data
return c.json({
success: true,
message: result.message,
updated: result.updated,
uid: result.uid,
userData: decryptedData
}, 200);
} catch (error) {
console.error('Error processing request:', error);
return c.json({ error: 'Failed to process request', details: error.message }, 500);
}
});
// Add health check endpoint
app.get('/api/cf-template/health', (c) => {
return c.json({ status: 'ok' });
});
// Add Swagger UI routes
app.get('/api/cf-template/docs', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
app.get('/api/cf-template/docs/', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
app.get('/api/cf-template/swagger.json', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
app.get('/api/cf-template/openapi.json', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
// Export the app
export default {
fetch: app.fetch,
};