Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.yml
*.md
*.Dockerfile
Makefile
.all-contributorsrc
.env.example
infra/kubernetes
infra/terraform
.github/
scripts/
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SENTRY_DSN=**
SENTRY_TRACES_SAMPLE_RATE=1
SENTRY_ENVIRONMENT=develop
SENTRY_RELEASE=**
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: lint test imports build start

start:
./bin/AppName
./bin/app_name

dev: path
gowatch
Expand Down Expand Up @@ -29,10 +29,10 @@ clear:

## https://github.com/golang-standards/project-layout/issues/113#issuecomment-1336514449
build: clear fmt
GOARCH=amd64 go build -o ./bin/AppName ./src/main.go
GOARCH=amd64 go build -o ./bin/app_name ./src/main.go

build-arm: clear fmt
GOARCH=arm64 go build -o ./bin/AppName ./src/main.go
GOARCH=arm64 go build -o ./bin/app_name ./src/main.go

test:
go test -v ./tests
Expand All @@ -56,7 +56,7 @@ pods:
kubectl get pods -A

compose-up:
docker compose -f ./infra/docker/docker-compose.yml up -d --build
docker compose -f ./compose.yaml up -d --build

compose-down:
docker compose -f ./infra/docker/docker-compose.yml down
docker compose -f ./compose.yaml down
15 changes: 15 additions & 0 deletions app.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.23-alpine AS base
WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN GOARCH=amd64 go build -o ./app_name ./src/main.go

FROM alpine:latest AS runner

RUN addgroup -S app_user && adduser -S app_user -G app_user

WORKDIR /app
COPY --from=base /build/app_name .
USER app_user:app_user
ENTRYPOINT ["./app_name"]
3 changes: 1 addition & 2 deletions infra/docker/compose.yaml → compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ services:
app_name:
user: 1000:1000
build:
context: ../../
context: .
dockerfile: app.Dockerfile
network: host
restart: always
security_opt:
- no-new-privileges
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module jd-golang

go 1.23.0

require (
github.com/getsentry/sentry-go v0.31.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/getsentry/sentry-go v0.31.1 h1:ELVc0h7gwyhnXHDouXkhqTFSO5oslsRDk0++eyE0KJ4=
github.com/getsentry/sentry-go v0.31.1/go.mod h1:CYNcMMz73YigoHljQRG+qPF+eMq8gG72XcGN/p71BAY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
54 changes: 54 additions & 0 deletions gowatch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

# gowatch.yml configuration example

# The name of the executable file generated under the current directory execution. The default is the current directory name.
appname: "app_name"

# Specify the command to run after builds done
# run_cmd: "./run.sh"

# Specify the directory where the compiled object files are stored
output: bin/app_name

# The file name suffix that needs to be monitored. By default, there is only a '.go' file.
watch_exts:
- .go

# The directory that needs to listen for file changes. By default, only the current directory.
watch_paths:
- ./src

# Additional parameters that need to be added when running the application
cmd_args:
- GOARCH=amd64

# Additional parameters that need to be added when building the application
build_args:
- ./src/main.go

# Need to increase environment variables, the current environment variables are loaded by default
# envs:
# - env1=val1

# Whether to listen to file changes in the 'vendor' folder
vendor_watch: false

# Directory that do not need to listen for file changes
# excluded_paths:
# - path

# main package path, can also be a single file, multiple files separated by commas
build_pkg: ""

# build tags
build_tags: ""

# Commands that can be executed before build the app
# prev_build_cmds:
# - swag init

# Whether to prohibit automatic operation
disable_run: false

# log level, support debug, info, warn, error, fatal
log_level: "debug"
28 changes: 0 additions & 28 deletions infra/docker/app.Dockerfile

This file was deleted.

5 changes: 0 additions & 5 deletions infra/docker/base-x64_86.Dockerfile

This file was deleted.

36 changes: 36 additions & 0 deletions src/config/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

import (
"os"
"strconv"
"strings"
)

type SentryConfig struct {
Dsn string
TracesSampleRate float64
Release string
Environment string
}

type Config struct {
Sentry SentryConfig
}

// GetConfig retrieves the configuration for the application from environment variables.
// It returns a Config struct containing Sentry configuration details such as DSN, release,
// environment, and traces sample rate. If the environment variable for the traces sample
// rate is not set or invalid, it defaults to zero.
func GetConfig() Config {

parsedFloat, _ := strconv.ParseFloat(strings.TrimSpace(os.Getenv("SENTRY_TRACES_SAMPLE_RATE")), 64)

return Config{
Sentry: SentryConfig{
TracesSampleRate: parsedFloat,
Dsn: os.Getenv("SENTRY_DSN"),
Release: os.Getenv("SENTRY_RELEASE"),
Environment: os.Getenv("SENTRY_ENVIRONMENT"),
},
}
}
31 changes: 28 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
package main

import (
"log"

"jd-golang/src/config"

"github.com/getsentry/sentry-go"
)

func main() {
println("Hello, world!")
println("This is a template for my golang projects")
println("I hope you find it useful")

config := config.GetConfig()

if config.Sentry.Environment == "develop" {
config.Sentry.Release = "unreleased"
}

err := sentry.Init(sentry.ClientOptions{
Dsn: config.Sentry.Dsn,
TracesSampleRate: config.Sentry.TracesSampleRate,
Release: config.Sentry.Release,
Environment: config.Sentry.Environment,
})

if err != nil {
log.Fatalf("sentry.Init: %s", err)
}

log.Println("Hello, world!")
sentry.CaptureMessage("Integration is working!!")
}
53 changes: 53 additions & 0 deletions tests/config_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tests

import "jd-golang/src/config"

var config_mock = []struct {
name string
envVars map[string]string
expectedConfig config.Config
}{
{
name: "default values",
envVars: map[string]string{
"SENTRY_TRACES_SAMPLE_RATE": "0.5",
"SENTRY_DSN": "https://[email protected]/0",
"SENTRY_RELEASE": "1.0.0",
"SENTRY_ENVIRONMENT": "production",
},
expectedConfig: config.Config{
Sentry: config.SentryConfig{
TracesSampleRate: 0.5,
Dsn: "https://[email protected]/0",
Release: "1.0.0",
Environment: "production",
},
},
},
{
name: "empty env vars",
envVars: map[string]string{},
expectedConfig: config.Config{
Sentry: config.SentryConfig{
TracesSampleRate: 0,
Dsn: "",
Release: "",
Environment: "",
},
},
},
{
name: "invalid TracesSampleRate",
envVars: map[string]string{
"SENTRY_TRACES_SAMPLE_RATE": "abc",
},
expectedConfig: config.Config{
Sentry: config.SentryConfig{
TracesSampleRate: 0,
Dsn: "",
Release: "",
Environment: "",
},
},
},
}
27 changes: 23 additions & 4 deletions tests/main_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package tests

import (
"jd-golang/src/config"
"os"
"reflect"
"testing"
)

func TestMain(t *testing.T) {
t.Log("Hello, world!")
t.Log("This is a template for my golang projects")
t.Log("I hope you find it useful")
func TestGetConfig(t *testing.T) {
for _, tt := range config_mock {
t.Run(tt.name, func(t *testing.T) {
for key, value := range tt.envVars {
os.Setenv(key, value)
}

defer func() {
for key := range tt.envVars {
os.Unsetenv(key)
}
}()

config := config.GetConfig()

if !reflect.DeepEqual(config, tt.expectedConfig) {
t.Errorf("GetConfig() = %+v, want %+v", config, tt.expectedConfig)
}
})
}
}
Loading