Skip to content

Commit fc664d9

Browse files
Merge pull request #3 from jd-apprentice/develop
Update MASTER
2 parents eccc148 + ff98a99 commit fc664d9

File tree

14 files changed

+244
-47
lines changed

14 files changed

+244
-47
lines changed

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.yml
2+
*.md
3+
*.Dockerfile
4+
Makefile
5+
.all-contributorsrc
6+
.env.example
7+
infra/kubernetes
8+
infra/terraform
9+
.github/
10+
scripts/

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SENTRY_DSN=**
2+
SENTRY_TRACES_SAMPLE_RATE=1
3+
SENTRY_ENVIRONMENT=develop
4+
SENTRY_RELEASE=**

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
all: lint test imports build start
22

33
start:
4-
./bin/AppName
4+
./bin/app_name
55

66
dev: path
77
gowatch
@@ -29,10 +29,10 @@ clear:
2929

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

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

3737
test:
3838
go test -v ./tests
@@ -56,7 +56,7 @@ pods:
5656
kubectl get pods -A
5757

5858
compose-up:
59-
docker compose -f ./infra/docker/docker-compose.yml up -d --build
59+
docker compose -f ./compose.yaml up -d --build
6060

6161
compose-down:
62-
docker compose -f ./infra/docker/docker-compose.yml down
62+
docker compose -f ./compose.yaml down

app.Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.23-alpine AS base
2+
WORKDIR /build
3+
COPY go.mod go.sum ./
4+
RUN go mod download
5+
COPY . .
6+
RUN GOARCH=amd64 go build -o ./app_name ./src/main.go
7+
8+
FROM alpine:latest AS runner
9+
10+
RUN addgroup -S app_user && adduser -S app_user -G app_user
11+
12+
WORKDIR /app
13+
COPY --from=base /build/app_name .
14+
USER app_user:app_user
15+
ENTRYPOINT ["./app_name"]

infra/docker/compose.yaml renamed to compose.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ services:
22
app_name:
33
user: 1000:1000
44
build:
5-
context: ../../
5+
context: .
66
dockerfile: app.Dockerfile
7-
network: host
87
restart: always
98
security_opt:
109
- no-new-privileges

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
module jd-golang
22

33
go 1.23.0
4+
5+
require (
6+
github.com/getsentry/sentry-go v0.31.1 // indirect
7+
github.com/joho/godotenv v1.5.1 // indirect
8+
golang.org/x/sys v0.18.0 // indirect
9+
golang.org/x/text v0.14.0 // indirect
10+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/getsentry/sentry-go v0.31.1 h1:ELVc0h7gwyhnXHDouXkhqTFSO5oslsRDk0++eyE0KJ4=
2+
github.com/getsentry/sentry-go v0.31.1/go.mod h1:CYNcMMz73YigoHljQRG+qPF+eMq8gG72XcGN/p71BAY=
3+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
4+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
5+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
6+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
8+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

gowatch.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
# gowatch.yml configuration example
3+
4+
# The name of the executable file generated under the current directory execution. The default is the current directory name.
5+
appname: "app_name"
6+
7+
# Specify the command to run after builds done
8+
# run_cmd: "./run.sh"
9+
10+
# Specify the directory where the compiled object files are stored
11+
output: bin/app_name
12+
13+
# The file name suffix that needs to be monitored. By default, there is only a '.go' file.
14+
watch_exts:
15+
- .go
16+
17+
# The directory that needs to listen for file changes. By default, only the current directory.
18+
watch_paths:
19+
- ./src
20+
21+
# Additional parameters that need to be added when running the application
22+
cmd_args:
23+
- GOARCH=amd64
24+
25+
# Additional parameters that need to be added when building the application
26+
build_args:
27+
- ./src/main.go
28+
29+
# Need to increase environment variables, the current environment variables are loaded by default
30+
# envs:
31+
# - env1=val1
32+
33+
# Whether to listen to file changes in the 'vendor' folder
34+
vendor_watch: false
35+
36+
# Directory that do not need to listen for file changes
37+
# excluded_paths:
38+
# - path
39+
40+
# main package path, can also be a single file, multiple files separated by commas
41+
build_pkg: ""
42+
43+
# build tags
44+
build_tags: ""
45+
46+
# Commands that can be executed before build the app
47+
# prev_build_cmds:
48+
# - swag init
49+
50+
# Whether to prohibit automatic operation
51+
disable_run: false
52+
53+
# log level, support debug, info, warn, error, fatal
54+
log_level: "debug"

infra/docker/app.Dockerfile

Lines changed: 0 additions & 28 deletions
This file was deleted.

infra/docker/base-x64_86.Dockerfile

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/config/environment.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type SentryConfig struct {
10+
Dsn string
11+
TracesSampleRate float64
12+
Release string
13+
Environment string
14+
}
15+
16+
type Config struct {
17+
Sentry SentryConfig
18+
}
19+
20+
// GetConfig retrieves the configuration for the application from environment variables.
21+
// It returns a Config struct containing Sentry configuration details such as DSN, release,
22+
// environment, and traces sample rate. If the environment variable for the traces sample
23+
// rate is not set or invalid, it defaults to zero.
24+
func GetConfig() Config {
25+
26+
parsedFloat, _ := strconv.ParseFloat(strings.TrimSpace(os.Getenv("SENTRY_TRACES_SAMPLE_RATE")), 64)
27+
28+
return Config{
29+
Sentry: SentryConfig{
30+
TracesSampleRate: parsedFloat,
31+
Dsn: os.Getenv("SENTRY_DSN"),
32+
Release: os.Getenv("SENTRY_RELEASE"),
33+
Environment: os.Getenv("SENTRY_ENVIRONMENT"),
34+
},
35+
}
36+
}

src/main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
package main
22

3+
import (
4+
"log"
5+
6+
"jd-golang/src/config"
7+
8+
"github.com/getsentry/sentry-go"
9+
)
10+
311
func main() {
4-
println("Hello, world!")
5-
println("This is a template for my golang projects")
6-
println("I hope you find it useful")
12+
13+
config := config.GetConfig()
14+
15+
if config.Sentry.Environment == "develop" {
16+
config.Sentry.Release = "unreleased"
17+
}
18+
19+
err := sentry.Init(sentry.ClientOptions{
20+
Dsn: config.Sentry.Dsn,
21+
TracesSampleRate: config.Sentry.TracesSampleRate,
22+
Release: config.Sentry.Release,
23+
Environment: config.Sentry.Environment,
24+
})
25+
26+
if err != nil {
27+
log.Fatalf("sentry.Init: %s", err)
28+
}
29+
30+
log.Println("Hello, world!")
31+
sentry.CaptureMessage("Integration is working!!")
732
}

tests/config_mock.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tests
2+
3+
import "jd-golang/src/config"
4+
5+
var config_mock = []struct {
6+
name string
7+
envVars map[string]string
8+
expectedConfig config.Config
9+
}{
10+
{
11+
name: "default values",
12+
envVars: map[string]string{
13+
"SENTRY_TRACES_SAMPLE_RATE": "0.5",
14+
"SENTRY_DSN": "https://[email protected]/0",
15+
"SENTRY_RELEASE": "1.0.0",
16+
"SENTRY_ENVIRONMENT": "production",
17+
},
18+
expectedConfig: config.Config{
19+
Sentry: config.SentryConfig{
20+
TracesSampleRate: 0.5,
21+
Dsn: "https://[email protected]/0",
22+
Release: "1.0.0",
23+
Environment: "production",
24+
},
25+
},
26+
},
27+
{
28+
name: "empty env vars",
29+
envVars: map[string]string{},
30+
expectedConfig: config.Config{
31+
Sentry: config.SentryConfig{
32+
TracesSampleRate: 0,
33+
Dsn: "",
34+
Release: "",
35+
Environment: "",
36+
},
37+
},
38+
},
39+
{
40+
name: "invalid TracesSampleRate",
41+
envVars: map[string]string{
42+
"SENTRY_TRACES_SAMPLE_RATE": "abc",
43+
},
44+
expectedConfig: config.Config{
45+
Sentry: config.SentryConfig{
46+
TracesSampleRate: 0,
47+
Dsn: "",
48+
Release: "",
49+
Environment: "",
50+
},
51+
},
52+
},
53+
}

tests/main_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
package tests
22

33
import (
4+
"jd-golang/src/config"
5+
"os"
6+
"reflect"
47
"testing"
58
)
69

7-
func TestMain(t *testing.T) {
8-
t.Log("Hello, world!")
9-
t.Log("This is a template for my golang projects")
10-
t.Log("I hope you find it useful")
10+
func TestGetConfig(t *testing.T) {
11+
for _, tt := range config_mock {
12+
t.Run(tt.name, func(t *testing.T) {
13+
for key, value := range tt.envVars {
14+
os.Setenv(key, value)
15+
}
16+
17+
defer func() {
18+
for key := range tt.envVars {
19+
os.Unsetenv(key)
20+
}
21+
}()
22+
23+
config := config.GetConfig()
24+
25+
if !reflect.DeepEqual(config, tt.expectedConfig) {
26+
t.Errorf("GetConfig() = %+v, want %+v", config, tt.expectedConfig)
27+
}
28+
})
29+
}
1130
}

0 commit comments

Comments
 (0)