-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (177 loc) · 5.2 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"log"
"net/http"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var db *gorm.DB
var jwtKey = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh")
type User struct {
gorm.Model
Username string `json:"username"`
Password string `json:"password"`
}
type Claims struct {
Username string `json:"username"`
jwt.StandardClaims
}
type Customer struct {
gorm.Model
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
}
func main() {
// Database connection
dsn := "root:123@tcp(127.0.0.1:3307)/bontodev_lastproject_test?charset=utf8mb4&parseTime=True&loc=Local"
var err error
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to database:", err)
}
// Auto migrate
db.AutoMigrate(&User{})
db.AutoMigrate(&Customer{})
// Set up Gin
r := gin.Default()
// // CORS configuration
// r.Use(cors.Default())
// CORS configuration
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:3000"} // Replace with your desired origin
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE"}
r.Use(cors.New(config))
// Routes
r.POST("/login", login)
r.POST("/signup", signup)
// Authenticated routes
auth := r.Group("/auth")
auth.Use(authMiddleware())
{
auth.GET("/profile", profile)
auth.GET("/customers", getCustomers)
auth.GET("/customers/:id", getCustomer)
auth.POST("/customers", createCustomer)
auth.PUT("/customers/:id", updateCustomer)
auth.DELETE("/customers/:id", deleteCustomer)
}
// Start server
r.Run(":8080")
}
func signup(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := db.Create(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
}
func login(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var dbUser User
if err := db.Where("username = ? AND password = ?", user.Username, user.Password).First(&dbUser).Error; err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
expirationTime := time.Now().Add(5 * time.Minute)
claims := &Claims{
Username: user.Username,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString(jwtKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not generate token"})
return
}
c.JSON(http.StatusOK, gin.H{"token": tokenString})
}
func authMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
if tokenString == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"})
c.Abort()
return
}
// Remove "Bearer " prefix from the token string
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
claims := &Claims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return jwtKey, nil
})
if err != nil || !token.Valid {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token", "msg": err.Error()})
c.Abort()
return
}
c.Set("username", claims.Username)
c.Next()
}
}
func profile(c *gin.Context) {
username, _ := c.Get("username")
c.JSON(http.StatusOK, gin.H{"username": username})
}
func getCustomers(c *gin.Context) {
var customers []Customer
db.Find(&customers)
c.JSON(http.StatusOK, customers)
}
func createCustomer(c *gin.Context) {
var customer Customer
if err := c.ShouldBindJSON(&customer); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
db.Create(&customer)
c.JSON(http.StatusCreated, customer)
}
func getCustomer(c *gin.Context) {
id := c.Param("id")
var customer Customer
if err := db.Where("id = ?", id).First(&customer).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Customer not found"})
return
}
c.JSON(http.StatusOK, customer)
}
func updateCustomer(c *gin.Context) {
id := c.Param("id")
var customer Customer
if err := db.Where("id = ?", id).First(&customer).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Customer not found"})
return
}
if err := c.ShouldBindJSON(&customer); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
db.Save(&customer)
c.JSON(http.StatusOK, customer)
}
func deleteCustomer(c *gin.Context) {
id := c.Param("id")
var customer Customer
if err := db.Where("id = ?", id).Delete(&customer).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Customer not found"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Customer deleted"})
}