mirror of
https://github.com/LeRoid-hub/Bookholder-API.git
synced 2025-01-31 02:34:57 +00:00
Added Dockerfile and implement http status codes
This commit is contained in:
parent
2e16304fac
commit
1eea40057f
5
.dockerignore
Normal file
5
.dockerignore
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
README.md
|
||||||
|
.gitignore
|
||||||
|
/examples
|
||||||
|
.git/
|
||||||
|
.github/
|
15
Dockerfile
Normal file
15
Dockerfile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
FROM golang:1.22
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY go.mod ./
|
||||||
|
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN go build -o /bookholder-api
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
CMD [ "/bookholder-api" ]
|
@ -21,25 +21,25 @@ func createUser(c *gin.Context) {
|
|||||||
var authInput AuthInput
|
var authInput AuthInput
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&authInput); err != nil {
|
if err := c.ShouldBindJSON(&authInput); err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var user database.User
|
var user database.User
|
||||||
user, err := database.GetUserByName(Database, authInput.Username)
|
user, err := database.GetUserByName(Database, authInput.Username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.ID != 0 {
|
if user.ID != 0 {
|
||||||
c.JSON(400, gin.H{"error": "User already exists"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "User already exists"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(authInput.Password), bcrypt.DefaultCost)
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(authInput.Password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ func createUser(c *gin.Context) {
|
|||||||
|
|
||||||
err = database.NewUser(Database, user)
|
err = database.NewUser(Database, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,25 +61,25 @@ func authenticateUser(c *gin.Context) {
|
|||||||
var authInput AuthInput
|
var authInput AuthInput
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&authInput); err != nil {
|
if err := c.ShouldBindJSON(&authInput); err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var userFound database.User
|
var userFound database.User
|
||||||
userFound, err := database.GetUserByName(Database, authInput.Username)
|
userFound, err := database.GetUserByName(Database, authInput.Username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if userFound.ID == 0 {
|
if userFound.ID == 0 {
|
||||||
c.JSON(400, gin.H{"error": "User not found"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "User not found"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(userFound.Password), []byte(authInput.Password))
|
err = bcrypt.CompareHashAndPassword([]byte(userFound.Password), []byte(authInput.Password))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": "Invalid password"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid password"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ func authenticateUser(c *gin.Context) {
|
|||||||
|
|
||||||
tokenString, err := generateToken.SignedString([]byte(Env["SECRET"]))
|
tokenString, err := generateToken.SignedString([]byte(Env["SECRET"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": "failed to generate token"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to generate token"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user