From 1eea40057f44035cca863bb6f77b030145b942c5 Mon Sep 17 00:00:00 2001 From: Jan Barfuss Date: Fri, 20 Dec 2024 15:47:43 +0100 Subject: [PATCH] Added Dockerfile and implement http status codes --- .dockerignore | 5 +++++ Dockerfile | 15 +++++++++++++++ server/auth.go | 20 ++++++++++---------- 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c71b59a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +README.md +.gitignore +/examples +.git/ +.github/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c3864b4 --- /dev/null +++ b/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/server/auth.go b/server/auth.go index 761a907..b7d8e66 100644 --- a/server/auth.go +++ b/server/auth.go @@ -21,25 +21,25 @@ func createUser(c *gin.Context) { var authInput AuthInput 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 } var user database.User user, err := database.GetUserByName(Database, authInput.Username) if err != nil { - c.JSON(400, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if user.ID != 0 { - c.JSON(400, gin.H{"error": "User already exists"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "User already exists"}) return } passwordHash, err := bcrypt.GenerateFromPassword([]byte(authInput.Password), bcrypt.DefaultCost) if err != nil { - c.JSON(400, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } @@ -50,7 +50,7 @@ func createUser(c *gin.Context) { err = database.NewUser(Database, user) if err != nil { - c.JSON(400, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } @@ -61,25 +61,25 @@ func authenticateUser(c *gin.Context) { var authInput AuthInput 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 } var userFound database.User userFound, err := database.GetUserByName(Database, authInput.Username) if err != nil { - c.JSON(400, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if userFound.ID == 0 { - c.JSON(400, gin.H{"error": "User not found"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "User not found"}) return } err = bcrypt.CompareHashAndPassword([]byte(userFound.Password), []byte(authInput.Password)) if err != nil { - c.JSON(400, gin.H{"error": "Invalid password"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid password"}) return } @@ -90,7 +90,7 @@ func authenticateUser(c *gin.Context) { tokenString, err := generateToken.SignedString([]byte(Env["SECRET"])) 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 }