From 4f5586e8f5bc1992b0ebee7f5705166e63b0dc25 Mon Sep 17 00:00:00 2001 From: Simon Headley Date: Sat, 10 Aug 2024 18:21:47 +0200 Subject: [PATCH] Refactored repo layout to Go standard and added basics of a Dockerised environment --- .editorconfig | 11 --- .gitignore | 12 +++- cmd/accounts/Dockerfile | 24 +++++++ {src/accounts => cmd/accounts/app}/account.go | 2 +- .../accounts/app}/account_types.go | 2 +- .../accounts => cmd/accounts/app}/accounts.go | 4 +- {src => cmd/accounts}/main.go | 7 +- {src/dapr => dapr}/state.accounts.yaml | 0 docker-compose.yml | 67 +++++++++++++++++++ src/go.mod => go.mod | 0 src/go.sum => go.sum | 0 test/.gitkeep | 0 12 files changed, 110 insertions(+), 19 deletions(-) delete mode 100644 .editorconfig create mode 100644 cmd/accounts/Dockerfile rename {src/accounts => cmd/accounts/app}/account.go (90%) rename {src/accounts => cmd/accounts/app}/account_types.go (83%) rename {src/accounts => cmd/accounts/app}/accounts.go (96%) rename {src => cmd/accounts}/main.go (62%) rename {src/dapr => dapr}/state.accounts.yaml (100%) create mode 100644 docker-compose.yml rename src/go.mod => go.mod (100%) rename src/go.sum => go.sum (100%) delete mode 100644 test/.gitkeep diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index ece50d4..0000000 --- a/.editorconfig +++ /dev/null @@ -1,11 +0,0 @@ -# EditorConfig is awesome: https://EditorConfig.org - -# top-most EditorConfig file -root = true - -# Unix-style newlines with a newline ending every file -[*] -indent_style = space -indent_size = 4 -end_of_line = lf -insert_final_newline = true \ No newline at end of file diff --git a/.gitignore b/.gitignore index 55a64d4..53bd35d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,10 @@ -# JetBrains directories -.idea \ No newline at end of file +# IDE directories and files +.idea +.vscode +.vs + +# OS directories and files +**/.DS_Store + +# Build artifacts +**/dist \ No newline at end of file diff --git a/cmd/accounts/Dockerfile b/cmd/accounts/Dockerfile new file mode 100644 index 0000000..ffac89a --- /dev/null +++ b/cmd/accounts/Dockerfile @@ -0,0 +1,24 @@ +FROM golang:1.22.5 + +# Set destination for COPY +WORKDIR /app + +# Download Go modules +COPY go.mod go.sum ./ +RUN go mod download + +# Copy the entire project or the necessary directories, preserving the structure +COPY . . + +# Build the Go application +RUN go build -o /dist cmd/accounts/main.go + +# Expose the application port +EXPOSE 8080 + +# Options to run in debug or release mode +ARG CONFIG=debug +ENV GIN_MODE=${CONFIG} + +# Run the compiled application +CMD ["/dist"] diff --git a/src/accounts/account.go b/cmd/accounts/app/account.go similarity index 90% rename from src/accounts/account.go rename to cmd/accounts/app/account.go index f41f7e5..4a898ce 100644 --- a/src/accounts/account.go +++ b/cmd/accounts/app/account.go @@ -1,4 +1,4 @@ -package accounts +package app type account struct { AccountID string `json:"accountId"` diff --git a/src/accounts/account_types.go b/cmd/accounts/app/account_types.go similarity index 83% rename from src/accounts/account_types.go rename to cmd/accounts/app/account_types.go index 8a9463d..4f28d0f 100644 --- a/src/accounts/account_types.go +++ b/cmd/accounts/app/account_types.go @@ -1,4 +1,4 @@ -package accounts +package app type AccountType int diff --git a/src/accounts/accounts.go b/cmd/accounts/app/accounts.go similarity index 96% rename from src/accounts/accounts.go rename to cmd/accounts/app/accounts.go index fad3c80..f97297e 100644 --- a/src/accounts/accounts.go +++ b/cmd/accounts/app/accounts.go @@ -1,4 +1,4 @@ -package accounts +package app import ( "encoding/json" @@ -10,7 +10,7 @@ import ( const state = "accounts-state" const validationFailureErrorMessage = "model validation failure while attempting to map request to type" -const notFoundErrorMessage = "reesource not found" +const notFoundErrorMessage = "resource not found" const internalServerErrorMessage = "an environmental, non-specific error has occurred" func GetAccountsForUser(c *gin.Context) { diff --git a/src/main.go b/cmd/accounts/main.go similarity index 62% rename from src/main.go rename to cmd/accounts/main.go index 4d10fd3..9912c30 100644 --- a/src/main.go +++ b/cmd/accounts/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/KrylixZA/bank-with-dapr/accounts" + accounts "github.com/KrylixZA/bank-with-dapr/cmd/accounts/app" "github.com/gin-gonic/gin" ) @@ -10,5 +10,8 @@ func main() { router.GET("/accounts/:userId", accounts.GetAccountsForUser) router.POST("/accounts/:userId", accounts.CreateAccountForUser) - router.Run("localhost:8080") + err := router.Run("localhost:8080") + if err != nil { + panic(err) + } } diff --git a/src/dapr/state.accounts.yaml b/dapr/state.accounts.yaml similarity index 100% rename from src/dapr/state.accounts.yaml rename to dapr/state.accounts.yaml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..81c7e68 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,67 @@ +services: + ################################### + # dapr placement service # + ################################### + placement: + image: daprio/placement:latest + container_name: placement + command: [ "./placement", "--port", "50006" ] + ports: + - 50006:50006 + networks: + - bank-network + + ################################### + # redis # + ################################### + redis: + image: redis:6 + container_name: redis + ports: + - 6379:6379 + networks: + - bank-network + + ################################### + # rabbitmq # + ################################### + rabbitmq: + image: rabbitmq:3-management + container_name: rabbitmq + ports: + - 5672:5672 + - 15672:15672 + networks: + - bank-network + + ################################### + # accounts service + dapr sidecar # + ################################### + accounts-service: + image: bank-with-dapr/accounts:dev + container_name: accounts-service + build: + context: . + dockerfile: cmd/accounts/Dockerfile + tags: + - bank-with-dapr/accounts:dev + ports: + - 8080:8080 + networks: + - bank-network + + accounts-service-dapr: + image: daprio/daprd:latest + command: [ "./daprd", + "--app-id", "accounts-service", + "--app-port", "8080", + "--placement-host-address", "placement:50006", + "--components-path", "/components" ] + volumes: + - ./dapr/:/components + depends_on: + - placement + network_mode: "service:accounts-service" + +networks: + bank-network: null \ No newline at end of file diff --git a/src/go.mod b/go.mod similarity index 100% rename from src/go.mod rename to go.mod diff --git a/src/go.sum b/go.sum similarity index 100% rename from src/go.sum rename to go.sum diff --git a/test/.gitkeep b/test/.gitkeep deleted file mode 100644 index e69de29..0000000