Skip to content

Commit 17c2d08

Browse files
committed
feat: 🎨 add setup with mongodb
add setup with mongodb add setup with mongodb
0 parents  commit 17c2d08

File tree

16 files changed

+838
-0
lines changed

16 files changed

+838
-0
lines changed

.github/workflows/go.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build_and_test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Setup go-task
15+
uses: pnorton5432/setup-task@v1
16+
with:
17+
task-version: 3.29.1
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: 'stable'
24+
check-latest: true
25+
- name: Task Build
26+
run: task build
27+
- name: Task Build for mage
28+
run: task build-gg
29+
- name: Test with gg
30+
run: ./gg test

.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
bin
27+
gg
28+
mage

README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# golang-with-mongodb-sample
2+
3+
This repository is demo how to use mongodb with golang
4+
5+
## sample
6+
7+
install mongo driver on golang
8+
9+
```shell
10+
go get go.mongodb.org/mongo-driver/mongo
11+
```
12+
13+
## setup run tools on Taskfile.yml
14+
15+
```yaml
16+
version: '3'
17+
18+
tasks:
19+
default:
20+
cmds:
21+
- echo "This is task cmd"
22+
silent: true
23+
24+
build:
25+
cmds:
26+
- CGO_ENABLED=0 GOOS=linux go build -o bin/main cmd/main.go
27+
silent: true
28+
run:
29+
cmds:
30+
- ./bin/main
31+
deps:
32+
- build
33+
silent: true
34+
35+
build-mage:
36+
cmds:
37+
- CGO_ENABLED=0 GOOS=linux go build -o ./mage mage-tools/mage.go
38+
silent: true
39+
40+
build-gg:
41+
cmds:
42+
- ./mage -d mage-tools -compile ../gg
43+
deps:
44+
- build-mage
45+
silent: true
46+
47+
coverage:
48+
cmds:
49+
- go test -v -cover ./...
50+
silent: true
51+
test:
52+
cmds:
53+
- go test -v ./...
54+
silent: true
55+
```
56+
57+
## setup test script with mage lib
58+
59+
### main execute file
60+
61+
```golang
62+
//go:build ignore
63+
// +build ignore
64+
65+
package main
66+
67+
import (
68+
"os"
69+
70+
"github.com/magefile/mage/mage"
71+
)
72+
73+
func main() { os.Exit(mage.Main()) }
74+
```
75+
76+
### setup run script on magefile.go
77+
78+
```golang
79+
//go:build mage
80+
// +build mage
81+
82+
package main
83+
84+
import (
85+
"github.com/magefile/mage/mg"
86+
"github.com/magefile/mage/sh"
87+
)
88+
89+
var Default = Build
90+
91+
// clean the build binary
92+
func Clean() error {
93+
return sh.Rm("bin")
94+
}
95+
96+
// Creates the binary in the current directory.
97+
func Build() error {
98+
mg.Deps(Clean)
99+
if err := sh.Run("go", "mod", "download"); err != nil {
100+
return err
101+
}
102+
return sh.Run("go", "build", "-o", "./bin/server", "./cmd/main.go")
103+
}
104+
105+
// start the server
106+
func Launch() error {
107+
mg.Deps(Build)
108+
err := sh.RunV("./bin/server")
109+
if err != nil {
110+
return err
111+
}
112+
return nil
113+
}
114+
115+
// run the test
116+
func Test() error {
117+
err := sh.RunV("go", "test", "-v", "./...")
118+
if err != nil {
119+
return err
120+
}
121+
return nil
122+
}
123+
```

Taskfile.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
version: '3'
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- echo "This is task cmd"
7+
silent: true
8+
9+
build:
10+
cmds:
11+
- CGO_ENABLED=0 GOOS=linux go build -o bin/main cmd/main.go
12+
silent: true
13+
run:
14+
cmds:
15+
- ./bin/main
16+
deps:
17+
- build
18+
silent: true
19+
20+
build-mage:
21+
cmds:
22+
- CGO_ENABLED=0 GOOS=linux go build -o ./mage mage-tools/mage.go
23+
silent: true
24+
25+
build-gg:
26+
cmds:
27+
- ./mage -d mage-tools -compile ../gg
28+
deps:
29+
- build-mage
30+
silent: true
31+
32+
coverage:
33+
cmds:
34+
- go test -v -cover ./...
35+
silent: true
36+
test:
37+
cmds:
38+
- go test -v ./...
39+
silent: true
40+

cmd/main.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
10+
"github.com/leetcode-golang-classroom/golang-with-mongodb-sample/internal/application"
11+
"github.com/leetcode-golang-classroom/golang-with-mongodb-sample/internal/config"
12+
mlog "github.com/leetcode-golang-classroom/golang-with-mongodb-sample/internal/logger"
13+
)
14+
15+
func main() {
16+
logger := slog.New(slog.NewJSONHandler(
17+
os.Stdout, &slog.HandlerOptions{
18+
AddSource: true,
19+
},
20+
))
21+
rootContext := context.WithValue(context.Background(), mlog.CtxKey{}, logger)
22+
app, err := application.New(config.AppConfig, rootContext)
23+
if err != nil {
24+
logger.Error("failed to build app", "error", err)
25+
return
26+
}
27+
ctx, cancel := signal.NotifyContext(rootContext, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
28+
defer cancel()
29+
err = app.Start(ctx)
30+
if err != nil {
31+
logger.Error("failed to start app", "error", err)
32+
}
33+
}

docker-compose.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
mongo:
3+
image: mongo:latest
4+
container_name: mongo
5+
restart: always
6+
ports:
7+
- "${MONGO_PORT}:27017"
8+
environment:
9+
- MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
10+
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
11+
- MONGO_INITDB_DATABASE=${MONGO_DB_NAME}

go.mod

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module github.com/leetcode-golang-classroom/golang-with-mongodb-sample
2+
3+
go 1.22.4
4+
5+
require (
6+
github.com/gin-gonic/gin v1.10.0
7+
github.com/magefile/mage v1.15.0
8+
github.com/samber/slog-gin v1.13.6
9+
github.com/spf13/viper v1.19.0
10+
go.mongodb.org/mongo-driver v1.17.1
11+
)
12+
13+
require (
14+
github.com/bytedance/sonic v1.11.9 // indirect
15+
github.com/bytedance/sonic/loader v0.1.1 // indirect
16+
github.com/cloudwego/base64x v0.1.4 // indirect
17+
github.com/cloudwego/iasm v0.2.0 // indirect
18+
github.com/fsnotify/fsnotify v1.7.0 // indirect
19+
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
20+
github.com/gin-contrib/sse v0.1.0 // indirect
21+
github.com/go-playground/locales v0.14.1 // indirect
22+
github.com/go-playground/universal-translator v0.18.1 // indirect
23+
github.com/go-playground/validator/v10 v10.22.0 // indirect
24+
github.com/goccy/go-json v0.10.3 // indirect
25+
github.com/golang/snappy v0.0.4 // indirect
26+
github.com/google/uuid v1.6.0 // indirect
27+
github.com/hashicorp/hcl v1.0.0 // indirect
28+
github.com/json-iterator/go v1.1.12 // indirect
29+
github.com/klauspost/compress v1.17.2 // indirect
30+
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
31+
github.com/leodido/go-urn v1.4.0 // indirect
32+
github.com/magiconair/properties v1.8.7 // indirect
33+
github.com/mattn/go-isatty v0.0.20 // indirect
34+
github.com/mitchellh/mapstructure v1.5.0 // indirect
35+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
36+
github.com/modern-go/reflect2 v1.0.2 // indirect
37+
github.com/montanaflynn/stats v0.7.1 // indirect
38+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
39+
github.com/sagikazarmark/locafero v0.4.0 // indirect
40+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
41+
github.com/sourcegraph/conc v0.3.0 // indirect
42+
github.com/spf13/afero v1.11.0 // indirect
43+
github.com/spf13/cast v1.6.0 // indirect
44+
github.com/spf13/pflag v1.0.5 // indirect
45+
github.com/subosito/gotenv v1.6.0 // indirect
46+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
47+
github.com/ugorji/go/codec v1.2.12 // indirect
48+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
49+
github.com/xdg-go/scram v1.1.2 // indirect
50+
github.com/xdg-go/stringprep v1.0.4 // indirect
51+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
52+
go.opentelemetry.io/otel v1.29.0 // indirect
53+
go.opentelemetry.io/otel/trace v1.29.0 // indirect
54+
go.uber.org/atomic v1.9.0 // indirect
55+
go.uber.org/multierr v1.9.0 // indirect
56+
golang.org/x/arch v0.8.0 // indirect
57+
golang.org/x/crypto v0.26.0 // indirect
58+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
59+
golang.org/x/net v0.26.0 // indirect
60+
golang.org/x/sync v0.8.0 // indirect
61+
golang.org/x/sys v0.23.0 // indirect
62+
golang.org/x/text v0.17.0 // indirect
63+
google.golang.org/protobuf v1.34.2 // indirect
64+
gopkg.in/ini.v1 v1.67.0 // indirect
65+
gopkg.in/yaml.v3 v3.0.1 // indirect
66+
)

0 commit comments

Comments
 (0)