Skip to main content

Posts

Showing posts from October, 2024

Simple Go HTTP Server Template

When working on various Go projects for clients, one recurring need is setting up a basic HTTP server. Whether it’s for health checks, APIs, or serving simple content, having a lightweight server template is essential. In this post, I’ll share a simple template I use to quickly spin up an HTTP server. This template can be extended to suit any project, and I’ll walk you through the key components. The Main Package In the main.go file, we initialize and start the server. I’ve also integrated graceful shutdown capabilities using the bsm/shutdown package to ensure that the server stops cleanly when interrupted. package main import ( "fmt" "log" "net/http" "../simple-server/internal/server" "github.com/bsm/shutdown" ) func main() { // Start the server fmt.Println("Starting server...") srv, err := server.New() if err != nil { log.Fatalln("failed to start server:", err) } defer func() { err := srv.Stop(...

Running Multiple Strategies in Parallel Using Goroutines in Go

I recently revisited a project I had worked on some time ago, where the system was required to take multiple parameters, query data from various sources, and then return a unified response. Reflecting on that project, I became curious about how I would approach building this process today. As a result, I decided to create a simple template to run multiple strategies concurrently. A Simple Version In this example, we’ll explore how to run multiple strategies in parallel using goroutines and channels. We’ll build a simple system that processes multiple strategies concurrently and returns a unified response from all of them. package main import ( "context" "fmt" "sync" ) func main() { // Create a context with a timeout ctx := context.Background() // Create a WaitGroup to synchronize goroutines var wg sync.WaitGroup // List of strategy names strategies := []Strategy{ &strA{}, &strB{}, } // start a response channel responseCh := ...

Pushing an Image to ECR from BitBucket

Recently, while updating my portfolio, I wanted to push a Docker image to AWS ECR (Elastic Container Registry) using a Bitbucket pipeline. It's something I regularly do at work, but I hadn’t set it up for a while, so I decided to document the process for easy reference. The process is straightforward, but it involves a few key steps, including setting up authentication and configuring a pipeline. Here's how you can do it: Step 1: Authenticate Bitbucket to AWS ECR To push an image to AWS ECR from Bitbucket, you'll first need to authenticate Bitbucket to AWS. This requires creating an IAM user with the necessary permissions to access and push images to ECR. Create an IAM Policy Start by creating a custom IAM policy that allows specific actions on ECR. Here's an example of the permissions you need: { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ...