Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY go.mod go.sum ./

RUN go mod download

COPY . .
COPY . .git


RUN go build -o /usr/local/bin/todo-app ./...
Expand All @@ -22,6 +22,6 @@ WORKDIR /root/
COPY --from=builder /usr/local/bin/todo-app .


EXPOSE 8080
EXPOSE 8081

CMD ["./todo-app"]
85 changes: 41 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
This project is a simple REST API built using Go.
It allows you to manage **Users** and their **To-Do Tasks** with basic CRUD operations.

---



📘 Go User & Task Management API

A lightweight, concurrency-safe REST API built using pure Go, designed for managing Users and their associated To-Do Tasks.
This project demonstrates clean API structuring, mutex-based synchronization, JSON handling, routing logic, and in-memory data storage.

⭐ Features
👤 User Management

Create a new user

Retrieve a user by ID

Delete a user

📝 Task (To-Do) Management

Create a task for a specific user

Fetch all tasks of a user

Tasks stored user-wise

Auto-increment task IDs

🔒 Thread-Safe Operations

Uses sync.Mutex and sync.RWMutex

Prevents race conditions under concurrent requests

⚡ Lightweight + No Dependencies

Standard library only (net/http, encoding/json)

No database — uses in-memory maps

Fast for demos, learning, and prototypes
This project is a simple REST API built using Go.
It allows you to manage **Users** and their **To-Do Tasks** with basic CRUD operations.

---



📘 Go User & Task Management API

A lightweight, concurrency-safe REST API built using pure Go, designed for managing Users and their associated To-Do Tasks.
This project demonstrates clean API structuring, mutex-based synchronization, JSON handling, routing logic, and in-memory data storage.

⭐ Features
👤 User Management

Create a new user

Retrieve a user by ID

Delete a user

📝 Task (To-Do) Management

Create a task for a specific user

Fetch all tasks of a user

Tasks stored user-wise

Auto-increment task IDs

🔒 Thread-Safe Operations

Uses sync.Mutex and sync.RWMutex

Prevents race conditions under concurrent requests

⚡ Lightweight + No Dependencies

Standard library only (net/http, encoding/json)

12 changes: 6 additions & 6 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func createUsers(w http.ResponseWriter, r *http.Request) {

}

func getUsers(w http.ResponseWriter, r *http.Request) {
func getUsers(w http.ResponseWriter, r *http.Request) { // to get users created

id, err := strconv.Atoi(r.PathValue("id"))
if err != nil {
Expand All @@ -49,7 +49,7 @@ func getUsers(w http.ResponseWriter, r *http.Request) {

}

func deleteUsers(w http.ResponseWriter, r *http.Request) {
func deleteUsers(w http.ResponseWriter, r *http.Request) { // to delete users based on id
id, err := strconv.Atoi(r.PathValue("id"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -70,7 +70,7 @@ func deleteUsers(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusNoContent)
}
func getAllUsers(w http.ResponseWriter, r *http.Request) {
func getAllUsers(w http.ResponseWriter, r *http.Request) { // to get all users
UserMutex.RLock()
defer UserMutex.RUnlock()

Expand All @@ -83,7 +83,7 @@ func getAllUsers(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(users)
}

func createToDoForUser(w http.ResponseWriter, r *http.Request) {
func createToDoForUser(w http.ResponseWriter, r *http.Request) { // to create tasks to users created
userID, err := strconv.Atoi(r.PathValue("userID"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -103,7 +103,7 @@ func createToDoForUser(w http.ResponseWriter, r *http.Request) {

err = json.NewDecoder(r.Body).Decode(&todo)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if todo.Task == "" {
Expand All @@ -128,7 +128,7 @@ func createToDoForUser(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(todo)
}
func getTasksForUser(w http.ResponseWriter, r *http.Request) {
func getTasksForUser(w http.ResponseWriter, r *http.Request) { // to get tasks for the users
userID, err := strconv.Atoi(r.PathValue("userID"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
fmt.Println(" GET /users/{id}/tasks -> List tasks for user")
fmt.Println("=====================================")

log.Println("⚡ Starting server on http://localhost:8081")
log.Println("⚡ Starting server on http://localhost:8081\n")

if err := server.ListenAndServe(); err != nil {
log.Fatalf("Could not listen on :8081: %v\n", err)
Expand Down
5 changes: 1 addition & 4 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type User struct {

type Tasks struct {
ID int `json:"id"`
UserID int `json:"userId"`
UserID int `json:"usersId"`
Task string `json:"task"`
Status bool `json:"status"`
}
Expand All @@ -21,9 +21,6 @@ var UserList = make(map[int]User)
var TaskList = make(map[int][]Tasks)

var NextUser = 1

var NextTask = 1

var UserMutex = sync.RWMutex{}

var TaskMutex = sync.RWMutex{}