A sample project about implementing Domain-Driven Design and Hexagonal Architecture (Ports&Adapters) with Go!
This code is the basis for a series of blog posts:
Implementing Domain-Driven Design and Hexagonal Architecture with Go - Part 1
Implementing Domain-Driven Design and Hexagonal Architecture with Go - Part 2
Part 3 (about Hexagonal Architecture) is ongoing
The code those blog posts are about is frozen in this branch: freeze_blog-posts_1-3
Run docker-compose up -d
in the project root.
Create local.env file in the project root (.env files is gitignored there) with following contents and replace
POSTGRES_DSN=postgresql://goiddd:password123@localhost:15432/goiddd_local?sslmode=disable
POSTGRES_MIGRATIONS_PATH_CUSTOMER=$PathToProjectRoot$/go-iddd/service/customeraccounts/infrastructure/postgres/database/migrations
GRPC_HOST_AND_PORT=localhost:5566
REST_HOST_AND_PORT=localhost:8085
REST_GRPC_DIAL_TIMEOUT=3
SWAGGER_FILE_PATH_CUSTOMER=$PathToProjectRoot$/go-iddd/src/customeraccounts/infrastructure/adapter/rest
Create test.env file in the project root (.env files is gitignored there) with following contents and replace
POSTGRES_DSN=postgresql://goiddd:password123@localhost:15432/goiddd_test?sslmode=disable
POSTGRES_MIGRATIONS_PATH_CUSTOMER=$PathToProjectRoot$/go-iddd/src/customeraccounts/infrastructure/adapter/postgres/database/migrations
GRPC_HOST_AND_PORT=localhost:5566
REST_HOST_AND_PORT=localhost:8085
REST_GRPC_DIAL_TIMEOUT=3
SWAGGER_FILE_PATH_CUSTOMER=$PathToProjectRoot$/go-iddd/src/customeraccounts/infrastructure/adapter/rest
Create a customer.http file in the project root (.http files are gitignored there) with following contents.
### Register a Customer
POST http://localhost:8085/v1/customer
Accept: */*
Cache-Control: no-cache
Content-Type: application/json
{
"emailAddress": "[email protected]",
"familyName": "Doe",
"givenName": "John"
}
> {% client.global.set("id", response.body.id); %}
### Confirm a Customer's email address
PUT http://localhost:8085/v1/customer/{{id}}/emailaddress/confirm
Accept: */*
Cache-Control: no-cache
Content-Type: application/json
{
"confirmationHash": "0acf14bbeaf0b9c6ef8e39d7f9254336"
}
### Change a Customer's email address
PUT http://localhost:8085/v1/customer/{{id}}/emailaddress
Accept: */*
Cache-Control: no-cache
Content-Type: application/json
{
"emailAddress": "[email protected]"
}
### Change a Customer's name
PUT http://localhost:8085/v1/customer/{{id}}/name
Accept: application/json
Cache-Control: no-cache
Content-Type: application/json
{
"givenName": "Joana",
"familyName": "Doe"
}
### Delete a Customer
DELETE http://localhost:8085/v1/customer/{{id}}
Accept: application/json
Cache-Control: no-cache
Content-Type: application/json
### Retrieve a Customer View
GET http://localhost:8085/v1/customer/{{id}}
Accept: application/json
Cache-Control: no-cache
Content-Type: application/json
### Get the Swagger documentation
GET http://localhost:8085/v1/customer/swagger.json
###
Attention
The ConfirmEmailAddress request does not work without changes - the confirmationHash needs to be adapted. You can find it in the CustomerRegistered event in the eventstore DB table. For security reasons the response of the Register request does not return the hash (it must only be sent to the Customer via email ;-)
- Source the local.env file in your terminal, e.g.
source dev/local.env
or set the env vars in a different way - In the project root run
go run service/cmd/grpc/main.go
- Create a build configuration for
service/cmd/grpc/main.go
- I suggest using the EnvFile GoLand plugin and add the local.env file in the build configuration