# README
Todo API Project
This project is a full-stack application for managing a todo list, built with Go and PostgreSQL for the backend, and React for the frontend.
Table of Contents
- Project Structure
- Prerequisites
- Setup
- Running the Server
- API Endpoints
- Frontend Application
- Testing the API
- Future Improvements
Project Structure
The project consists of the following main components:
Backend:
main.go
: Contains the main server logic, API handlers, and CORS configurationdb.go
: Handles database connection and initializationtodo.go
: Defines the Todo struct and related functions
Frontend:
todo-frontend/
: React application directorytodo-frontend/src/App.js
: Main React component for the Todo application
Prerequisites
Before you begin, ensure you have the following installed:
- Go (1.16 or later)
- PostgreSQL
- Node.js and npm (for React frontend)
- Git (optional, for version control)
Setup
-
Clone the repository (if using Git):
git clone <repository-url> cd todo-api
-
Install PostgreSQL (if not already installed): For Ubuntu or Debian-based systems:
sudo apt-get update sudo apt-get install postgresql postgresql-contrib
For other operating systems, please refer to the official PostgreSQL documentation.
-
Start PostgreSQL service:
sudo systemctl start postgresql sudo systemctl enable postgresql
-
Install the required Go packages:
go get github.com/lib/pq go get github.com/rs/cors
-
Set up the PostgreSQL database:
- Open a terminal and start the PostgreSQL command-line interface:
sudo -u postgres psql
- Create a new database:
CREATE DATABASE todo_db;
- Create a new user and grant privileges (replace 'your_username' and 'your_password'):
CREATE USER your_username WITH PASSWORD 'your_password'; GRANT ALL PRIVILEGES ON DATABASE todo_db TO your_username;
- Exit the PostgreSQL CLI:
\q
- Open a terminal and start the PostgreSQL command-line interface:
-
Update the database connection string:
- Open
db.go
- Locate the
initDB
function - Update the
connStr
variable with your PostgreSQL username and password:connStr := "user=your_username password=your_password dbname=todo_db sslmode=disable"
- Open
-
Set up the frontend:
cd todo-frontend npm install
After completing these steps, you should be ready to run the server and start using the application.
Running the Server
To run the backend server, use the following command in the project root directory:
go run *.go
If successful, you should see the following output:
Successfully connected to database
Server is running on port 8080
To run the frontend development server:
cd todo-frontend
npm start
The React application will be available at http://localhost:3000
.
API Endpoints
The API provides the following endpoints:
GET /todos
: Retrieve all todo itemsPOST /todos
: Create a new todo itemPUT /todos?id=<id>
: Update an existing todo itemDELETE /todos?id=<id>
: Delete a todo item
Frontend Application
The frontend is a React application that provides a user interface for interacting with the Todo API. It includes the following features:
- Display a list of todo items
- Add new todo items
- Mark todo items as complete/incomplete
- Edit existing todo items
- Delete todo items
The main component (App.js
) uses React hooks for state management and effect handling.
Testing the API
You can use curl commands to test the API. Here are some examples:
-
Create a new todo item:
curl -X POST -H "Content-Type: application/json" -d '{"title":"Buy groceries","completed":false}' 'http://localhost:8080/todos'
-
Retrieve all todo items:
curl 'http://localhost:8080/todos'
-
Update a todo item (replace
<id>
with the actual item id):curl -X PUT -H "Content-Type: application/json" -d '{"title":"Buy groceries","completed":true}' 'http://localhost:8080/todos?id=<id>'
-
Delete a todo item (replace
<id>
with the actual item id):curl -X DELETE 'http://localhost:8080/todos?id=<id>'
Note: When using zsh, make sure to wrap the URL in single quotes to prevent issues with the '?' character.
CORS Configuration
The backend server is configured to handle Cross-Origin Resource Sharing (CORS), allowing the frontend application to make requests to the API. This is implemented using the github.com/rs/cors
package in the main.go
file:
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"}, // React app's address
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"Content-Type"},
})
handler := c.Handler(mux)
This configuration allows requests from the React application running on http://localhost:3000
and specifies the allowed HTTP methods and headers.
Future Improvements
Here are some potential improvements for the project:
- Implement user authentication and authorization
- Add input validation and error handling
- Implement pagination for the GET /todos endpoint
- Add unit and integration tests
- Use environment variables for configuration
- Implement logging for better debugging and monitoring
- Enhance the frontend with more advanced features (e.g., filtering, sorting)
- Dockerize the application for easier deployment
- Implement real-time updates using WebSockets
- Add offline support and data synchronization for the frontend
Feel free to contribute to the project by implementing these improvements or suggesting new features!