-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (99 loc) · 2.94 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
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"sync"
)
// User struct represents the structure of a user object
type User struct {
User string `json:"user"`
}
// mutex for safe concurrent access to the userCache
var mutex sync.RWMutex
// userCache stores users with their IDs as keys
var userCache = make(map[int]User)
// mainHandler responds with a simple "Hello" message
func mainHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello")
}
// createUserHandler creates a new user and adds it to the userCache
func createUserHandler(w http.ResponseWriter, r *http.Request) {
var user User
// Decode the JSON request body into a User struct
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
fmt.Println("Error here:", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return // Added return statement to exit the function on error
}
// Lock the mutex before modifying the userCache
mutex.Lock()
defer mutex.Unlock()
// Add the new user to the cache with an auto-incremented ID
userCache[len(userCache)+1] = user
fmt.Println(userCache)
w.WriteHeader(http.StatusAccepted)
}
// getUserHandler retrieves a user by ID from the userCache
func getUserHandler(w http.ResponseWriter, r *http.Request) {
// Extract the ID from the URL path
id := r.PathValue("id")
intId, err := strconv.Atoi(id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println(intId)
// Lock the mutex for reading from the userCache
mutex.RLock()
defer mutex.RUnlock()
// Retrieve the user from the cache
user, ok := userCache[intId]
if !ok {
http.Error(w, "User not found", http.StatusNotFound)
return
}
// Encode and send the user as JSON response
json.NewEncoder(w).Encode(user)
// using json marshal
// userJson, err := json.Marshal(user)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// w.Write(userJson)
}
// deleteUserHandler removes a user from the userCache by ID
func deleteUserHandler(w http.ResponseWriter, r *http.Request) {
// Extract the ID from the URL path
id := r.PathValue("id")
intId, err := strconv.Atoi(id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Lock the mutex before modifying the userCache
mutex.Lock()
defer mutex.Unlock()
// Check if the user exists before deleting
if _, ok := userCache[intId]; !ok {
http.Error(w, "User not found", http.StatusNotFound)
return
}
delete(userCache, intId)
w.WriteHeader(http.StatusAccepted)
}
func main() {
// Create a new ServeMux for routing
mux := http.NewServeMux()
// Register handlers for different routes
mux.HandleFunc("/", mainHandler)
mux.HandleFunc("POST /users", createUserHandler)
mux.HandleFunc("GET /users/{id}", getUserHandler)
mux.HandleFunc("DELETE /users/{id}", deleteUserHandler)
fmt.Println("Listening to port: 8080")
// Start the HTTP server
http.ListenAndServe(":8080", mux)
}