-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (130 loc) · 3.61 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
type User struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
Role string `json:"role"`
}
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println("What would you like to do?")
fmt.Println("1. Create user")
fmt.Println("2. Query user")
fmt.Println("3. Quit")
fmt.Print("Choice: ")
choice, _ := reader.ReadString('\n')
choice = strings.TrimSpace(choice)
switch choice {
case "1":
createUser(reader)
case "2":
getUser(reader)
case "3":
fmt.Println("Application is quitting.")
return
default:
fmt.Println("Invalid choice.")
}
}
}
func createUser(reader *bufio.Reader) {
// Authorization check: Only users with admin privileges can create new users
if !isAdmin() {
fmt.Println("Only users with admin privileges can create new users.")
return
}
fmt.Println("Enter user data:")
fmt.Print("Username: ")
username, _ := reader.ReadString('\n')
username = strings.TrimSpace(username)
fmt.Print("Email: ")
email, _ := reader.ReadString('\n')
email = strings.TrimSpace(email)
fmt.Print("Password: ")
password, _ := reader.ReadString('\n')
password = strings.TrimSpace(password)
fmt.Print("Role: ")
role, _ := reader.ReadString('\n')
role = strings.TrimSpace(role)
user := User{
Username: username,
Email: email,
Password: password,
Role: role,
}
// Encode the user object to JSON
userJSON, err := json.Marshal(user)
if err != nil {
fmt.Println("Error encoding user:", err)
return
}
// Send an HTTP request to the server to create the user
resp, err := http.Post("http://localhost:8080/add-user", "application/json", strings.NewReader(string(userJSON)))
if err != nil {
fmt.Println("Error creating user:", err)
return
}
defer resp.Body.Close()
// Check if the response is successful
if resp.StatusCode != http.StatusCreated {
// Check if rate limit exceeded
if resp.StatusCode == http.StatusTooManyRequests {
fmt.Println("Rate limit exceeded. Please try again later.")
} else {
fmt.Println("Error creating user. Status code:", resp.StatusCode)
}
return
}
fmt.Println("User created successfully.")
}
func getUser(reader *bufio.Reader) {
// Authorization check: Only users with admin privileges can query users
if !isAdmin() {
fmt.Println("Only users with admin privileges can query users.")
return
}
fmt.Print("Enter username to query: ")
username, _ := reader.ReadString('\n')
username = strings.TrimSpace(username)
// Send an HTTP request to the server to get the user details
resp, err := http.Get("http://localhost:8080/get-user?username=" + username)
if err != nil {
fmt.Println("Error querying user:", err)
return
}
defer resp.Body.Close()
// Check if the status code is OK
if resp.StatusCode != http.StatusOK {
// Check if rate limit exceeded
if resp.StatusCode == http.StatusTooManyRequests {
fmt.Println("Rate limit exceeded. Please try again later.")
} else {
fmt.Println("Error retrieving user. Status code:", resp.StatusCode)
}
return
}
// Decode the JSON response object
var user User
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
fmt.Println("Error decoding response:", err)
return
}
// Print the user details
fmt.Println("User data:", user)
}
func isAdmin() bool {
// Here you can implement the authorization check
// For example, you could check the role of the current user
// and return true if the user is an admin, otherwise return false.
return true // Temporarily return true here to demonstrate functionality
}