Skip to content

Commit c2e6344

Browse files
authored
feat: [KAN-49] add vaidateClient middleware to common (#8)
1 parent 45b5084 commit c2e6344

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

.github/ci/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ services:
33
image: mysql:8.0
44
container_name: mysql-container
55
environment:
6-
MYSQL_ROOT_PASSWORD: 123456
7-
MYSQL_DATABASE: user_db
6+
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
7+
- MYSQL_DATABASE=user_db
88
# Pure memory, disappears when CI ends
99
tmpfs: /var/lib/mysql
1010
networks:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package middleware
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func ValidateClient() gin.HandlerFunc {
6+
return func(c *gin.Context) {
7+
client := c.Param("client")
8+
if client != "merchant" && client != "customer" {
9+
c.JSON(400, gin.H{"error": "Invalid client type"})
10+
c.Abort()
11+
return
12+
}
13+
c.Next()
14+
}
15+
}

server/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/server
33
go 1.24.0
44

55
require (
6-
github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common v0.0.0-20250926094305-623423bdf292
6+
github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common v0.0.0-20251001113629-170f5abf70f9
77
github.com/gin-gonic/gin v1.11.0
88
github.com/go-playground/validator/v10 v10.27.0
99
github.com/spf13/viper v1.21.0

server/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
44
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
55
github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common v0.0.0-20250926094305-623423bdf292 h1:aQ28fQMiJAkWqLyBUdQ1KuF6xaorqRKYQA9OvLrX/Pw=
66
github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common v0.0.0-20250926094305-623423bdf292/go.mod h1:37OvOYo/KVtH7LdbUnKLitzsC6qE6LCGF6sgaFHzE7E=
7+
github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common v0.0.0-20251001113629-170f5abf70f9 h1:b7OCJwxzv4hhTMZInbS8UnSYSlGRPK0fwQ4ZOFMi/2Y=
8+
github.com/NUS-ISS-Agile-Team/ceramicraft-user-mservice/common v0.0.0-20251001113629-170f5abf70f9/go.mod h1:37OvOYo/KVtH7LdbUnKLitzsC6qE6LCGF6sgaFHzE7E=
79
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
810
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
911
github.com/bytedance/sonic v1.14.1 h1:FBMC0zVz5XUmE4z9wF4Jey0An5FueFvOsTKKKtwIl7w=

server/http/router/router.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ func NewRouter() *gin.Engine {
3939
})
4040
})
4141
}
42+
parentGroup := basicGroup.Group("/:client")
43+
parentGroup.Use(middleware.ValidateClient())
4244

43-
v1UnAuthed := r.Group(serviceURIPrefix + "/:client")
45+
v1UnAuthed := parentGroup.Group("")
4446
{
45-
v1UnAuthed.Use(validateClient())
4647
v1UnAuthed.POST("/login", api.UserLogin)
4748
v1UnAuthed.POST("/users", api.Register)
4849
v1UnAuthed.PUT("/users/activate", api.Validate)
4950
}
50-
v1Authed := r.Group(serviceURIPrefix + "/:client")
51+
v1Authed := parentGroup.Group("")
5152
{
52-
v1Authed.Use(validateClient(), middleware.AuthMiddleware())
53+
v1Authed.Use(middleware.AuthMiddleware())
5354
v1Authed.POST("/logout", api.UserLogout)
5455
}
5556
return r
@@ -67,15 +68,3 @@ var passwordStrengthValidator validator.Func = func(fl validator.FieldLevel) boo
6768

6869
return hasLetter && hasDigit && isValidLength
6970
}
70-
71-
func validateClient() gin.HandlerFunc {
72-
return func(c *gin.Context) {
73-
client := c.Param("client")
74-
if client != "merchant" && client != "customer" {
75-
c.JSON(400, gin.H{"error": "Invalid client type"})
76-
c.Abort()
77-
return
78-
}
79-
c.Next()
80-
}
81-
}

0 commit comments

Comments
 (0)