Skip to content

Commit

Permalink
Prepared entire project to build and deploy it on Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Jan 16, 2024
1 parent 220c4bc commit 7aad07f
Show file tree
Hide file tree
Showing 40 changed files with 495 additions and 228 deletions.
5 changes: 0 additions & 5 deletions backend/cmd/main.go

This file was deleted.

39 changes: 0 additions & 39 deletions backend/config/config.go

This file was deleted.

10 changes: 0 additions & 10 deletions backend/config/development-config.yml

This file was deleted.

96 changes: 0 additions & 96 deletions backend/data/database/migration/1_init.go

This file was deleted.

25 changes: 25 additions & 0 deletions backend/src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM --platform=linux/amd64 golang:1.21-bookworm as builder

WORKDIR /app

COPY go.* ./
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -v -o server ./cmd/main.go

FROM --platform=linux/amd64 debian:bookworm-slim
RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
ca-certificates && \
rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/server /app/server
COPY --from=builder /app/config/docker-config.yml /app/config/docker-config.yml

ENV APP_ENV docker
ENV PORT ${Port}

RUN chmod +x /app/server

CMD ["/app/server"]
4 changes: 2 additions & 2 deletions backend/api/api.go → backend/src/api/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package api

import (
"backend/api/router"
"backend/config"
"fmt"
"github.com/Stefan923/go-estate-market/api/router"
"github.com/Stefan923/go-estate-market/config"
"github.com/gin-gonic/gin"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package handler

import (
"backend/api/response"
error2 "backend/error"
"context"
response2 "github.com/Stefan923/go-estate-market/api/response"
error3 "github.com/Stefan923/go-estate-market/error"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
Expand All @@ -14,17 +14,17 @@ func Create[T any, R any](context *gin.Context, caller func(context context.Cont
err := context.ShouldBindJSON(&request)
if err != nil {
context.AbortWithStatusJSON(http.StatusBadRequest,
response.GenerateResponseWithValidationError(nil, false, err))
response2.GenerateResponseWithValidationError(nil, false, err))
return
}

responseObject, err := caller(context, request)
if err != nil {
context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err),
response.GenerateResponseWithError(nil, false, err))
context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}
context.JSON(http.StatusCreated, response.GenerateResponse(responseObject, true))
context.JSON(http.StatusCreated, response2.GenerateResponse(responseObject, true))
}

func Update[T any, R any](context *gin.Context, caller func(context context.Context, id int, request *T) (*R, error)) {
Expand All @@ -33,51 +33,51 @@ func Update[T any, R any](context *gin.Context, caller func(context context.Cont
err := context.ShouldBindJSON(&request)
if err != nil {
context.AbortWithStatusJSON(http.StatusBadRequest,
response.GenerateResponseWithValidationError(nil, false, err))
response2.GenerateResponseWithValidationError(nil, false, err))
return
}

responseObject, err := caller(context, id, request)
if err != nil {
context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err),
response.GenerateResponseWithError(nil, false, err))
context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}
context.JSON(http.StatusOK, response.GenerateResponse(responseObject, true))
context.JSON(http.StatusOK, response2.GenerateResponse(responseObject, true))
}

func Delete(context *gin.Context, caller func(context context.Context, id int) error) {
id, _ := strconv.Atoi(context.Params.ByName("id"))
if id == 0 {
context.AbortWithStatusJSON(http.StatusNotFound,
response.GenerateResponseWithError(nil, false, &error2.InternalError{
EndUserMessage: error2.InvalidPathParameter,
response2.GenerateResponseWithError(nil, false, &error3.InternalError{
EndUserMessage: error3.InvalidPathParameter,
}))
return
}

err := caller(context, id)
if err != nil {
context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err),
response.GenerateResponseWithError(nil, false, err))
context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}
context.JSON(http.StatusOK, response.GenerateResponse(nil, true))
context.JSON(http.StatusOK, response2.GenerateResponse(nil, true))
}

func GetById[R any](context *gin.Context, caller func(context context.Context, id int) (*R, error)) {
id, _ := strconv.Atoi(context.Params.ByName("id"))
if id == 0 {
context.AbortWithStatusJSON(http.StatusNotFound,
response.GenerateResponse(nil, false))
response2.GenerateResponse(nil, false))
return
}

responseObject, err := caller(context, id)
if err != nil {
context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err),
response.GenerateResponseWithError(nil, false, err))
context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}
context.JSON(http.StatusOK, response.GenerateResponse(responseObject, true))
context.JSON(http.StatusOK, response2.GenerateResponse(responseObject, true))
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package handler

import (
"backend/api/dto"
"backend/api/response"
"backend/config"
"backend/service"
"github.com/Stefan923/go-estate-market/api/dto"
response2 "github.com/Stefan923/go-estate-market/api/response"
"github.com/Stefan923/go-estate-market/config"
"github.com/Stefan923/go-estate-market/service"
"github.com/gin-gonic/gin"
"net/http"
)
Expand All @@ -25,19 +25,19 @@ func (handler UserAccountHandler) Login(context *gin.Context) {
if err != nil {
context.AbortWithStatusJSON(
http.StatusBadRequest,
response.GenerateResponseWithValidationError(nil, false, err))
response2.GenerateResponseWithValidationError(nil, false, err))
return
}

tokenDetail, err := handler.userAccountService.Login(request)
if err != nil {
context.AbortWithStatusJSON(
response.TranslateErrorToStatusCode(err),
response.GenerateResponseWithError(nil, false, err))
response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}

context.JSON(http.StatusCreated, response.GenerateResponse(tokenDetail, true))
context.JSON(http.StatusCreated, response2.GenerateResponse(tokenDetail, true))
}

func (handler UserAccountHandler) Register(context *gin.Context) {
Expand All @@ -46,17 +46,17 @@ func (handler UserAccountHandler) Register(context *gin.Context) {
if err != nil {
context.AbortWithStatusJSON(
http.StatusBadRequest,
response.GenerateResponseWithValidationError(nil, false, err))
response2.GenerateResponseWithValidationError(nil, false, err))
return
}

tokenDetail, err := handler.userAccountService.Register(context, request)
if err != nil {
context.AbortWithStatusJSON(
response.TranslateErrorToStatusCode(err),
response.GenerateResponseWithError(nil, false, err))
response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}

context.JSON(http.StatusCreated, response.GenerateResponse(tokenDetail, false))
context.JSON(http.StatusCreated, response2.GenerateResponse(tokenDetail, false))
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package response

import "backend/api/validator"
import (
"github.com/Stefan923/go-estate-market/api/validator"
)

type BaseHttpResponse struct {
Result any `json:"result"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package response

import (
error2 "backend/error"
error2 "github.com/Stefan923/go-estate-market/error"
"net/http"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package router

import (
"backend/api/handler"
"backend/config"
"github.com/Stefan923/go-estate-market/api/handler"
"github.com/Stefan923/go-estate-market/config"
"github.com/gin-gonic/gin"
)

Expand Down
File renamed without changes.
Loading

0 comments on commit 7aad07f

Please sign in to comment.