This is a simple blog project built using React for the frontend, Cloudflare Workers for the backend, and Postgres SQL with Prisma ORM for database management. The project is deployed on Cloudflare Workers, and the backend can be accessed at backend.omopyt2020.workers.dev.
- User Signup and Signin with authentication.
- Create, update, and view blog posts.
- Authentication is handled using JSON Web Tokens (JWT).
- Prisma ORM is used for database interactions with PostgreSQL.
- Frontend: React
- Backend: Cloudflare Workers
- Routing: Hono (a lightweight router for Cloudflare Workers)
- Database: PostgreSQL
- ORM: Prisma
- Deployment: Cloudflare Workers
.
├── frontend/ # React frontend files
├── backend/ # Cloudflare Workers backend code
├── prisma/ # Prisma schema and migration files
├── README.md # Documentation
└── package.json # Project dependencies
To run the backend locally and deploy it to Cloudflare Workers, follow these steps:
-
Install Dependencies:
npm install
-
Setup Prisma: Initialize Prisma and generate client:
npx prisma init npx prisma generate
-
Configure Environment Variables: Create a
.env
file for your project and add your PostgreSQL connection string:DATABASE_URL="postgresql://user:password@localhost:5432/dbname"
-
Deploy to Cloudflare Workers: Use the Cloudflare Workers CLI to deploy:
npx wrangler publish
POST /api/v1/user/signup
-
Description: Register a new user.
-
Expected Payload:
{ "username": "your_username", "password": "your_password", "name": "Your Name" }
-
Response:
- Success: 201 Created
- Failure: 400 Bad Request
POST /api/v1/user/signin
-
Description: Authenticate and log in a user.
-
Expected Payload:
{ "username": "your_username", "password": "your_password" }
-
Response:
- Success: 200 OK (with JWT)
- Failure: 401 Unauthorized
POST /api/v1/blog/
-
Description: Create a new blog post.
-
Expected Payload:
{ "title": "Blog Title", "content": "Blog content", "authorId": "JWT Token's Author ID" }
-
Response:
- Success: 201 Created
- Failure: 400 Bad Request
PUT /api/v1/blog/
-
Description: Update an existing blog post.
-
Expected Payload:
{ "where": { "id": "Blog Post ID", "authorId": "JWT Token's Author ID" }, "data": { "title": "Updated Title", "content": "Updated Content" } }
-
Response:
- Success: 200 OK
- Failure: 404 Not Found
GET /api/v1/blog/all
- Description: Retrieve all blog posts.
- Response:
- Success: 200 OK (list of blog posts)
- Failure: 404 Not Found
GET /api/v1/blog/:id
- Description: Retrieve a specific blog post by ID.
- Response:
- Success: 200 OK (blog post)
- Failure: 404 Not Found
This project uses PostgreSQL with Prisma ORM. To set up the database:
-
Configure Prisma: Update the
prisma/schema.prisma
file with your database details. -
Migrate Database: Run the following commands to create tables in the database:
npx prisma migrate dev --name init
-
Generate Prisma Client: After migrating, generate the Prisma Client:
npx prisma generate
To deploy the backend to Cloudflare Workers, use the following command:
npx wrangler publish
Ensure your database credentials are properly configured in the .env
file for deployment.
This project is licensed under the MIT License. See the LICENSE file for details.
- Be sure to secure your API by implementing additional security measures such as rate limiting and input validation to protect your endpoints.
- JWT should be securely stored and handled on the frontend.
That's it! You're now ready to run your blog project with Cloudflare Workers and PostgreSQL.