A template to create API projects in GO After crating and configuring a project with this template we will have a project ready to deploy as an API The project will contain
- Initial code with a /ping GET method with tests and coverage
- Dockerfile to create registry in AWS
- Sonarcloud configuration to check tests, coverage and code quality
- CI/CD pipelines to deploy
- Terraform configuration to deploy to AWS as an API
- Datadog configuration
This template doesn't contain all the names of the created project. So this guideline is very important to ensure a proper project configuration
Golang works with modules, so this project is created with module 'github.com/trafilea/go-template' (see go.mod file) So here is the list with all the changes needed for the project to build
- Go to go.mod file and change 'go-template' for your project-name
Example:
module github.com/trafilea/go-template
->module github.com/trafilea/checkout-api
- Go to internal/routes/routes.go and change all 'go-template' to your project's name
Example:
github.com/trafilea/go-template/pkg/apperrors
->github.com/trafilea/checkout-api/pkg/apperrors
- Go to internal/routes/routes_test.go and change all 'go-template' to your project's name. Same as point 2
- Go to cmd/app/main.go and change all 'go-template' to your project's name. Same as point 2
- Run command
go mod tidy
- Run command
go build ./...
to check all changes are ok
In this case we need to change all project name references
WORKDIR /go-template
->WORKDIR /checkout-api
RUN go build -o /build/go-template.go cmd/app/main.go
->RUN go build -o /build/checkout-api.go cmd/app/main.go
CMD [ "/build/go-template.go" ]
->CMD [ "/build/checkout-api.go" ]
In this case we need to change all project name references
'sonar.projectKey=trafilea_go-template'
-> the project key assigned when setting up sonarsonar.projectName=go-template
->sonar.projectName=checkout-api
There isn't anything to change in case of CI. But there are some things to change for CD
-
In .github/workflows/continuous-deployment.yaml edit the following
- Change
APP_NAME: go-template
->APP_NAME: checkout-api
- Add
branches
instead ofbranches-ignore
to CD pipeline (we are removing this because we don't want to execute them when creating this go-template)on: push: branches: - main - develop
- Change
-
In .github/workflows/continuous-integration.yaml edit the following
- Add
branches
instead ofbranches-ignore
to CD pipeline (we are removing them because we don't want to execute them when creating this go-template)on: pull_request: branches: - main - develop
- Add
-
In .github/workflows/datadog-monitors.yaml edit the following
- Add
branches
instead ofbranches-ignore
to CD pipeline (we are removing them because we don't want to execute them when creating this go-template)on: pull_request: branches: - main paths: - '.datadog/**'
TF_VAR_APPLICATION_NAME: go-template
->TF_VAR_APPLICATION_NAME: checkout-api
- Add
-
In .github/workflows/manual-deployment.yaml edit the following
- Change
APP_NAME: go-template
->APP_NAME: checkout-api
- Change
Before starting with your project, you should change some stuff in order to get the best result.
- In terraform/globals/provider.tf you should change the
default_tags
block according to your API. For example, you could have the following config:
default_tags {
tags = {
Application = "MyAPI"
Environment = var.environment
Owner = "soft-dev"
Project = "IncredibleAPIProject"
Provisioned = "Terragrunt"
}
}
- In each environment.hcl file on the terraform/environment folders, you should change the app_name, app_name_caps and app_port according to your project. For example:
inputs = {
vpc_id = "vpc-01af99de0fc923203"
app_name = "my-api"
app_name_caps = "My API"
app_port = 80
environment = "stage"
region = "us-east-1"
account_id = 209977326717
}
Note: don't change the VPC, Region, environment or account_id inputs, since they are used to deploy the project on the corresponding environment
-
In terraform/terragrunt.hcl you should change the
config.keys
likekey = "go-template/${basename(get_terragrunt_dir())}.tfstate"
->key = "checkout-api/${basename(get_terragrunt_dir())}.tfstate"
-
Review the configuration for each module on the terraform/environment folders. There are hardcoded app names, and ports in the modules config. If you don't understand or need a review about your config, tell @jw-tera (Juan Wiggenhauser on Slack) to review it ;) . We are working to remove this and put a global configuration in the future Example for alb_sg:
inputs = merge( local.environment_vars,
{
security_group_name = "my-api-alb-sg"
security_group_desc = "My API ALB Access Control"
//Check your rules
sg_inbound = [
{
from_port = 80 //Look for the port on your app
to_port = 80 //Look for the port on your app
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}
]
//Dont touch this:
sg_outbound = [
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
]
})
Please, feel free to ask the infra team for a review of your code.
- In .datadog/datadog.backend.tf change project names
key = "go-template/datadog.tfstate"
->key = "checkout-api/datadog.tfstate"
- Create project in SonarCloud and create the secret SONAR_TOKEN in the project
- Ask Infrastructure team to create ECR repository with the project name (example checkout-api)
- Ask github organization's admin to allow the project to have AWS secrets