Read issue #1 of Daily Digest, by Mailbrew Team.
Friday May, 2024
How do I manage entity relationships in Go?

I come from a TypeScript background where tools like TypeORM and Equalize handle entity relationships efficiently. If I’m building an API using Go's standard library, what options do I have to manage relationships such as one-to-many and many-to-many?

Interface, is this a bad practice?

We are having an internal discussion within the company on best practices of interfaces. Project layout context: We have grpc backend that does database crud operations following MVC-REPO. This is traditionally for front-end apps, but brief overview of the pattern as follows: M (Model) - represents the data, a struct Repo (Repository) - abstraction layer between the model and controller that does the database operations for the given model. C (Controller) - handles the input, grpc requests in this case, interacts with the repository for the db operations and returns back the response. V (View) - omitted, but would take the response from the controller and send it back to the client that invoked the grpc request. Code context package controller type MyController struct { repo repository.MyRepositoryInterface } func NewMyController(repo repository.MyRepositoryInterface) MyController { return &MyController { repo: repo } } func (c MyController) MyDatabaseOperation(myModel models.SampleModel) error { return c.repo.MyDatabaseOperation(myModel) } package repository type MyRepositoryInterface interface { MyDatabaseOperation(myModel models.SampleModel) } type MyRepository struct { db sqlx.DB } func NewMyRepository(db sqlx.DB) MyRepository { return &MyRepository{ db: db } } func (r MyRepository) MyDatabaseOperation(myModel *models.SampleModel) { // database and query magic } package main func main() { c := controllers.NewMyController(repository.MyRepositoryInterface) sampleModel := &models.SampleModel err := c.MyDatabaseOperation(sampleModel) if err != nil { // error } // sampleModel now has data populated } Question / topic of the hour, if I repeat this pattern for every model. Team is against this due to following reasons: 1. interface pollution 2. un-necessary abstraction 3. use the concrete value directly 4. Golang is not an object-oriented In my defense, the PROs would be: 1. interface segregation principle, no client should be forced to depend on interfaces they do not use. 2. Each controller would be dependent on a specific repository interface, which is small and forcused on a specific database operations related to a given model. 3. Used as a contract to define the behavior of each model. 4. Unit testing is easier. Is my defense a bad practice of using interfaces, and disregard the controllers and repository code sample above?

How to Implement Two-Factor Authentication (2FA) with TOTP in Golang

Hi folks, I'm one of the maintainers of the GO OSS project Permify, an open source authorization service that ease building scalable and fine-grained access controls. I've written a brief tutorial guide to demonstrate how to implement Two-Factor Authentication (2FA) using TOTP by implementing to your in a Golang app. Looking forward for your feedback!!

Debugging binaries, hard to debug panic

I have two binaries one built from source which passes some integration test, and another build with goreleaser. I've verified \&gravego version -m <binary>\&grave that it matches both, they are both built from the same source tree, however the goreleaser binary causes a panic. The panic doesn't seem possible from a compilation perspective, for which I've filed the issue upstream on the opentelemetry-go github repo with the panic. The panic occurs with go 1.22, but not 1.21 builds. But as the build from source passes the test, I'm coming up a bit short in diagnosing the panic. I'm already looking at elf_diff to see if i can spot any difference in the binaries being built, so far nothing I've tried had any effect, other than if the test suite runs the app in a delve debugger session, then the panic does not occur. Feels like I've exhausted reasonable options, also by trying flags like \&grave-cflags "-N -l"\&grave to disable optimisations and inlining (no effect), git --safe-directory to include vcs info in local builds, goreleaser -buildvss=false to exclude vcs info, and running go build with -race (no races reported). Could anyone suggest any build flags or settings that may have some effect, or how to approach this other than ripping out goreleaser from the build workflow? Goreleaser build with 1.21 is passing the tests, so things are pointing to some sort of go runtime/build issue. I'd appreciate any hints at this point, before i loose all my hair. Edit 1: as requested the panic: &grave&grave&grave tyk-1 | panic: runtime error: hash of unhashable type [2]string tyk-1 | tyk-1 | goroutine 54 [running]: tyk-1 |{0xc00020af08, 0x4, 0xc000e6a080?}) tyk-1 | +0x2d9 tyk-1 |, {0x404dc18, 0xc0002dc0e0}, {0xc00020af08?, 0xc00008eef2?, 0xc0002936c0?}) tyk-1 | +0x34 tyk-1 |, {0x404dba8, 0xc00017c6e0}) tyk-1 | +0x238 tyk-1 |*batchSpanProcessor).processQueue(0xc00031c140) tyk-1 | +0x36e tyk-1 | tyk-1 | +0x54 tyk-1 | created by in goroutine 1 tyk-1 | +0x2e5 tyk-1 exited with code 2 &grave&grave&grave The panic itself as mentioned in the filed issue should be impossible. - span.go source on L41 - the key is of a &gravekey&grave type, struct{}, and the value is a pointer - no idea where [2]string may be coming from, given the rest of the panic

What libraries do you folks use for user registration and authentication?

I'm writing a golang json api server and was looking at libraries that handle user registration and authentication. Do you folks have any recommendations? I don't need any html view components as that will be taken care of by a separate front-end component. I have worked on rails before and for those who can relate, I'm looking for something like devise. Thanks!

Go Error Propagation and API Contracts

Go Error Propagation and API Contracts


so this is the usecase for a project am working on , so I have an RSTP stream being generated by a camera and have another device reading the streams for this on my web browser. So I need to establish  RSTP connection between browser and the device. So when there are disconnections I can pass video or audio (which can implement later),right now am focussing on video. Have implemented this  but am getting intermittent connections . Need help to spot the problems or any idea I could adjust  will be appreciated. This is exact picture of what happens with the disconnections

Issues with Golang lambda

Hi everyone, I seem to keep getting issues with my Lambda in Golang. For a project I'm working on I'm using Cognito for authentication, I want to add my custom scopes that are retrieved from my database into the token. This is achievable as is documented here: The code for my lambda looks somewhat like this (left out some names for privacy reasons) package main import ( "context" "encoding/json" "errors" "log" "GITHUBREPONAME/internal/db" "" "" ) var store db.PostgresStore func init() { var err error store, err = db.NewPostgresStore() if err != nil { log.Fatalf("Error creating db store: %s", err) } err = store.Init() if err != nil { log.Fatalf("Error initializing db: %s", err) } } func main() { defer func() { if closeErr := store.Close(); closeErr != nil { log.Fatalf("Error closing db store: %s", closeErr) } }() lambda.Start(handleRequest) } func handleRequest(ctx context.Context, event events.CognitoEventUserPoolsPreTokenGenV2) (events.CognitoEventUserPoolsPreTokenGenV2Response, error) { if event.Request.UserAttributes == nil { return nil, errors.New("user attributes were nil") } userName, exists := event.Request.UserAttributes["cognito:username"] if !exists { return nil, errors.New("username not found in user attributes") } permissions, err := store.GetUserPermissions(userName) if err != nil { return nil, err } permissionsJson, err := json.Marshal(permissions) if err != nil { return nil, err } var scopes []string for , permission := range permissions { scope := permission.PracticeId + ":" + permission.RoleName + ":" + string(permissionsJson) scopes = append(scopes, scope) } scopesJson, err := json.Marshal(scopes) if err != nil { return nil, err } claimsToAddOrOverride := map[string]string{ "scope": string(scopesJson), } return &events.CognitoEventUserPoolsPreTokenGenV2Response{ ClaimsAndScopeOverrideDetails: events.ClaimsAndScopeOverrideDetails{ AccessTokenGeneration: events.AccessTokenGeneration{ ClaimsToAddOrOverride: claimsToAddOrOverride, }, }, }, nil } My API is running fine with the package as it uses the same package. My makefile looks like this: build-pre-gen-lambda: @echo "Building the application..." GOOS=linux GOARCH=arm64 go build -o bin/pre-token-gen-lambda/bootstrap cmd/pre-token-gen-lambda/main.go chmod 755 bin/pre-token-gen-lambda/bootstrap publish-pre-gen-lambda-stag: build-pre-gen-lambda zip ./bin/pre-token-gen-lambda/ ./bin/pre-token-gen-lambda/bootstrap aws s3 cp ./bin/pre-token-gen-lambda/ s3://BUCKETNAME/FUNCTIONNAME/ deploy-pre-gen-lambda-stag: publish-pre-gen-lambda-stag aws lambda update-function-code --function-name pregentokenlambda --s3-bucket BUCKETNAME --s3-key FUNCTIONNAME/ --architectures arm64 I keep getting this logged message when trying to log in (and thus invoking the lambda): || || |2024-05-23T18:06:35.815Z|INIT_REPORT Init Duration: 0.51 ms Phase: init Status: error Error Type: Runtime.InvalidEntrypoint| |2024-05-23T18:06:35.835Z|INIT_REPORT Init Duration: 0.50 ms Phase: invoke Status: error Error Type: Runtime.InvalidEntrypoint| |2024-05-23T18:06:35.835Z|START RequestId: 48f1a4af-1712-4b36-a43c-ad5661f11aa5 Version: $LATEST| |2024-05-23T18:06:35.836Z|RequestId: 48f1a4af-1712-4b36-a43c-ad5661f11aa5 Error: fork/exec /var/task/bootstrap: exec format error Runtime.InvalidEntrypoint| |2024-05-23T18:06:35.836Z|END RequestId: 48f1a4af-1712-4b36-a43c-ad5661f11aa5| |2024-05-23T18:06:35.836Z|REPORT RequestId: 48f1a4af-1712-4b36-a43c-ad5661f11aa5 Duration: 1.40 ms Billed Duration: 2 ms Memory Size: 1024 MB Max Memory Used: 3 MB| And my terraform code as well: resource "awslambdafunction" "pregentokenlambda" { functionname = "pregentokenlambda" s3bucket = awss3bucket.lambdabuildsbucket.bucket s3key = "pregentokenlambda/" role = awsiamrole.pregentokenlambdaexecrole.arn handler = "bootstrap" runtime = "provided.al2" timeout = 10 architectures = ["arm64"] memorysize = 1024 ephemeral_storage { size = 1024 } environment { variables = { -- DATABASE VARIABLES -- } } }

Standard format to store and process HTTP requests in golang (via config)

Hi! i am new to golang and I currently face this issue, how to implement a function in golang, which allows me to parse a config file or similar to then process the information how a certain request to an API should be made. Example how I think it could work (if I code it by myself) config: myrequest: headers: X-Forwarded-For: abc Authorization: Bearer $env_token endpoint: https://... method: POST ... I then want to parse this in golang (using yaml UnMarshall & structs) and execute the request according to the specification of the user in the config file (and do some post processing with the response later on). But is there a standardized format which I can use / import directly into golang? In my research I found this: Parse HTTP requests and responses from text file in Go. But this seems cumbersome in terms of a "config" file / complex to do for a user. Also some tools can export API requests as "curl" statements. This is - however - cumbersome to use as "format" for a request specification. Is there a standardized format to store request information and a library to parse it in golang (I couldn't find something yet, but maybe I searched in the wrong way)? Just found stuff for "Responses" not for Requests. I want to be as flexible as possible to allow users to adapt to any (REST) APIs they want to consume with my tool. Did someone else here faced a similiar issue? Any opinions / ideas how I should proceed? If the question is too broad / unclear please let me know - I will update it accordingly. Thank you!

Vault Kubernetes databases security for credentials

Hello everyone ! Here is my first OSS contribution and it’s a pleasure to share it with you ! I’m a devops trying to build tools around Kubernetes and its ecosystem. I’ve built a Kubernetes Injector that use mutating webhook to generate database credentials over vault database engine and inject them inside pod with specific annotations. It’s a real pleasure for me to share it with our amazing community ! I hope you could review it and share with me your feeling about it For me information i let you check the GitHub repository Thx

Twitter, RSS, YouTube, Newsletters, Weather, Calendar, ...

You can customize this digest to include all the sites and sources you want.