-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (94 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/google/uuid"
)
// Task represents a to-do task.
type Task struct {
ID string `json:"id"`
Title string `json:"title"`
Status string `json:"status"` // e.g., "pending" or "completed"
}
var tasks = []Task{} // In-memory storage for tasks
const Dport = ":8012"
var guide ="Welcome to task server"
func main() {
http.HandleFunc("/", returnInfo)
http.HandleFunc("/tasks", tasksHandler)
http.HandleFunc("/task/", taskHandler)
fmt.Printf("Server is starting on port: %v\n", Dport) // Added newline for better terminal output
http.ListenAndServe(Dport, nil)
}
func returnInfo(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "This is from jevica ")
fmt.Fprintln(w, "My Student Id is :500218849")
fmt.Fprintln(w, "My github is : https://github.com/jevdsouza")
fmt.Fprintln(w,"This is a tutorial on the API. It performs basic CRUD functions.")
fmt.Fprintln(w,"GET /tasks shows a list of all tasks.")
fmt.Fprintln(w,"POST /tasks creates a new task.")
fmt.Fprintln(w,"PUT /task/{id} updates an existing task.")
fmt.Fprintln(w,"DELETE /task/{id}: Deletes the task identified by {id}.")
}
// Handle requests to the /tasks endpoint
func tasksHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
json.NewEncoder(w).Encode(tasks)
case "POST":
var task Task
if err := json.NewDecoder(r.Body).Decode(&task); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
task.ID = uuid.New().String() // Generate a unique ID for the task
tasks = append(tasks, task)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(task)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
// Handle requests to the /task/{id} endpoint
func taskHandler(w http.ResponseWriter, r *http.Request) {
// Extract the task ID from the URL path
taskID := strings.TrimPrefix(r.URL.Path, "/task/")
switch r.Method {
case "PUT":
var updatedTask Task
if err := json.NewDecoder(r.Body).Decode(&updatedTask); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
found := false
for i, task := range tasks {
if task.ID == taskID {
updatedTask.ID = task.ID // Ensuring the ID remains unchanged
tasks[i] = updatedTask
found = true
break
}
}
if !found {
http.Error(w, "Task not found", http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(updatedTask)
case "DELETE":
index := -1
for i, task := range tasks {
if task.ID == taskID {
index = i
break
}
}
if index != -1 {
tasks = append(tasks[:index], tasks[index+1:]...)
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, "Task not found", http.StatusNotFound)
}
}
}