Skip to content

Commit 6f153c1

Browse files
committed
Add configuration for Redis
1 parent 7c42215 commit 6f153c1

File tree

13 files changed

+243
-11
lines changed

13 files changed

+243
-11
lines changed

app/watch/cmd/main.go

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
11
package main
22

3-
func main() {}
3+
import (
4+
"flag"
5+
"os"
6+
7+
"github.com/go-kratos/kratos/v2"
8+
"github.com/go-kratos/kratos/v2/config"
9+
"github.com/go-kratos/kratos/v2/config/file"
10+
"github.com/go-kratos/kratos/v2/log"
11+
"github.com/go-kratos/kratos/v2/transport/grpc"
12+
"github.com/uneva/dd-watch/app/watch/internal/conf"
13+
)
14+
15+
var (
16+
// Name is the name of the compiled software.
17+
Name string
18+
// Version is the version of the compiled software.
19+
Version string
20+
// flagconf is the config flag.
21+
flagconf string
22+
)
23+
24+
func init() {
25+
flag.StringVar(&flagconf, "conf", "../configs", "config path, eg: -conf config.yaml")
26+
}
27+
28+
func newApp(logger log.Logger, gs *grpc.Server) *kratos.App {
29+
return kratos.New(
30+
kratos.Name(Name),
31+
kratos.Version(Version),
32+
kratos.Metadata(map[string]string{}),
33+
kratos.Logger(logger),
34+
kratos.Server(gs),
35+
)
36+
}
37+
38+
func main() {
39+
flag.Parse()
40+
logger := log.NewStdLogger(os.Stdout)
41+
42+
cfg := config.New(
43+
config.WithSource(
44+
file.NewSource(flagconf),
45+
),
46+
)
47+
if err := cfg.Load(); err != nil {
48+
panic(err)
49+
}
50+
51+
var bc conf.Bootstrap
52+
if err := cfg.Scan(&bc); err != nil {
53+
panic(err)
54+
}
55+
56+
app, cleanup, err := wireApp(bc.Server, bc.Data, logger)
57+
if err != nil {
58+
panic(err)
59+
}
60+
defer cleanup()
61+
62+
// start and wait for stop signal
63+
if err := app.Run(); err != nil {
64+
panic(err)
65+
}
66+
}

app/watch/cmd/wire.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
//go:build wireinject
2+
// +build wireinject
3+
14
package main
25

3-
func wireApp() {}
6+
import (
7+
"github.com/go-kratos/kratos/v2"
8+
"github.com/go-kratos/kratos/v2/log"
9+
"github.com/google/wire"
10+
"github.com/uneva/dd-watch/app/watch/internal/biz"
11+
"github.com/uneva/dd-watch/app/watch/internal/conf"
12+
"github.com/uneva/dd-watch/app/watch/internal/data"
13+
"github.com/uneva/dd-watch/app/watch/internal/server"
14+
"github.com/uneva/dd-watch/app/watch/internal/service"
15+
)
16+
17+
func wireApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) {
18+
panic(wire.Build(
19+
server.ProviderSet,
20+
data.ProviderSet,
21+
biz.ProviderSet,
22+
service.ProviderSet,
23+
newApp))
24+
}

app/watch/configs/config.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
trace:
2+
endpoint: http://127.0.0.1:14268/api/traces
13
server:
24
http:
35
addr: 0.0.0.0:0
@@ -8,4 +10,9 @@ server:
810
data:
911
database:
1012
driver: mysql
11-
source: root:dangerous@tcp(127.0.0.1:3306)/test?parseTime=true
13+
source: root:dangerous@tcp(127.0.0.1:3306)/test?parseTime=true
14+
redis:
15+
addr: 127.0.0.1:6379
16+
dial_timeout: 1s
17+
read_timeout: 0.4s
18+
write_timeout: 0.6s

app/watch/internal/biz/biz.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package biz
22

33
import "github.com/google/wire"
44

5-
var ProviderSet = wire.NewSet()
5+
var ProviderSet = wire.NewSet(NewWatchUsecase)

app/watch/internal/biz/watch.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package biz
2+
3+
import (
4+
"github.com/go-kratos/kratos/v2/log"
5+
)
6+
7+
type WatchRepo interface{}
8+
9+
type WatchUsecase struct {
10+
repo WatchRepo
11+
}
12+
13+
func NewWatchUsecase(repo WatchRepo, logger log.Logger) *WatchUsecase {
14+
return &WatchUsecase{
15+
repo: repo,
16+
}
17+
}

app/watch/internal/conf/conf.proto

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import "google/protobuf/duration.proto";
77
option go_package = "dd/watch/conf;conf";
88

99
message Bootstrap {
10-
Server server = 1;
11-
Data data = 2;
10+
Trace trace = 1;
11+
Server server = 2;
12+
Data data = 3;
13+
}
14+
15+
message Trace {
16+
string endpoint = 1;
1217
}
1318

1419
message Server {
@@ -31,5 +36,15 @@ message Data {
3136
string driver = 1;
3237
string source = 2;
3338
}
39+
message Redis {
40+
string network = 1;
41+
string addr = 2;
42+
string password = 3;
43+
int32 db = 4;
44+
google.protobuf.Duration dial_timeout = 5;
45+
google.protobuf.Duration read_timeout = 6;
46+
google.protobuf.Duration write_timeout = 7;
47+
}
3448
Database database = 1;
49+
Redis redis = 2;
3550
}

app/watch/internal/data/data.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package data
22

3-
import "github.com/google/wire"
3+
import (
4+
"github.com/go-kratos/kratos/v2/log"
5+
"github.com/google/wire"
6+
"github.com/uneva/dd-watch/app/watch/internal/conf"
7+
)
48

5-
var ProviderSet = wire.NewSet()
9+
var ProviderSet = wire.NewSet(NewData, NewWatchRepo)
10+
11+
type Data struct{}
12+
13+
func NewData(conf *conf.Data, logger log.Logger) (*Data, func(), error) {
14+
// log := log.NewHelper(logger)
15+
return &Data{}, nil, nil
16+
}

app/watch/internal/data/watch.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package data
2+
3+
import (
4+
"github.com/go-kratos/kratos/v2/log"
5+
"github.com/uneva/dd-watch/app/watch/internal/biz"
6+
)
7+
8+
type watchRepo struct {
9+
data *Data
10+
log *log.Helper
11+
}
12+
13+
func NewWatchRepo(data *Data, logger log.Logger) biz.WatchRepo {
14+
return &watchRepo{
15+
data: data,
16+
log: log.NewHelper(logger),
17+
}
18+
}

app/watch/internal/server/grpc.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package server
2+
3+
import (
4+
"github.com/go-kratos/kratos/v2/log"
5+
"github.com/go-kratos/kratos/v2/middleware/logging"
6+
"github.com/go-kratos/kratos/v2/middleware/recovery"
7+
"github.com/go-kratos/kratos/v2/middleware/tracing"
8+
"github.com/go-kratos/kratos/v2/middleware/validate"
9+
"github.com/go-kratos/kratos/v2/transport/grpc"
10+
v1 "github.com/uneva/dd-watch/api/watch/v1"
11+
"github.com/uneva/dd-watch/app/watch/internal/conf"
12+
"github.com/uneva/dd-watch/app/watch/internal/service"
13+
)
14+
15+
func NewGRPCServer(c *conf.Server, logger log.Logger, watch *service.WatchService) *grpc.Server {
16+
opts := []grpc.ServerOption{
17+
grpc.Middleware(
18+
recovery.Recovery(),
19+
tracing.Server(),
20+
logging.Server(logger),
21+
validate.Validator(),
22+
),
23+
}
24+
if c.Grpc.Network != "" {
25+
opts = append(opts, grpc.Network(c.Grpc.Network))
26+
}
27+
if c.Grpc.Addr != "" {
28+
opts = append(opts, grpc.Address(c.Grpc.Addr))
29+
}
30+
if c.Grpc.Timeout != nil {
31+
opts = append(opts, grpc.Timeout(c.Grpc.Timeout.AsDuration()))
32+
}
33+
srv := grpc.NewServer(opts...)
34+
35+
v1.RegisterWatchServiceServer(srv, watch)
36+
37+
return srv
38+
}

app/watch/internal/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package server
22

33
import "github.com/google/wire"
44

5-
var ProviderSet = wire.NewSet()
5+
var ProviderSet = wire.NewSet(NewGRPCServer)

app/watch/internal/service/service.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package service
22

3-
import "github.com/google/wire"
3+
import (
4+
"github.com/go-kratos/kratos/v2/log"
5+
"github.com/google/wire"
6+
v1 "github.com/uneva/dd-watch/api/watch/v1"
7+
"github.com/uneva/dd-watch/app/watch/internal/biz"
8+
)
49

5-
var ProviderSet = wire.NewSet()
10+
var ProviderSet = wire.NewSet(NewWatchService)
11+
12+
type WatchService struct {
13+
v1.UnimplementedWatchServiceServer
14+
15+
log *log.Helper
16+
17+
watch *biz.WatchUsecase
18+
}

app/watch/internal/service/watch.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package service
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-kratos/kratos/v2/log"
7+
v1 "github.com/uneva/dd-watch/api/watch/v1"
8+
"github.com/uneva/dd-watch/app/watch/internal/biz"
9+
)
10+
11+
func NewWatchService(watch *biz.WatchUsecase, logger log.Logger) *WatchService {
12+
return &WatchService{
13+
watch: watch,
14+
log: log.NewHelper(logger),
15+
}
16+
}
17+
18+
func (s *WatchService) ToLink(ctx context.Context, req *v1.ToLinkRequest) (*v1.ToLinkResponse, error) {
19+
s.log.Infof("input data %v", req)
20+
return &v1.ToLinkResponse{}, nil
21+
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ require (
1313
)
1414

1515
require (
16+
github.com/fsnotify/fsnotify v1.6.0 // indirect
1617
github.com/go-kratos/aegis v0.2.0 // indirect
18+
github.com/go-logr/logr v1.2.4 // indirect
19+
github.com/go-logr/stdr v1.2.2 // indirect
1720
github.com/go-playground/form/v4 v4.2.0 // indirect
1821
github.com/golang/protobuf v1.5.3 // indirect
1922
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
2023
github.com/google/uuid v1.4.0 // indirect
2124
github.com/gorilla/mux v1.8.0 // indirect
25+
github.com/imdario/mergo v0.3.16 // indirect
26+
go.opentelemetry.io/otel v1.16.0 // indirect
27+
go.opentelemetry.io/otel/metric v1.16.0 // indirect
28+
go.opentelemetry.io/otel/trace v1.16.0 // indirect
2229
golang.org/x/net v0.20.0 // indirect
30+
golang.org/x/sync v0.5.0 // indirect
2331
golang.org/x/sys v0.16.0 // indirect
2432
golang.org/x/text v0.14.0 // indirect
2533
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect

0 commit comments

Comments
 (0)