Skip to content

Commit

Permalink
refactor(helloworld): improve app initialization and config management
Browse files Browse the repository at this point in the history
- Rename wireApp to buildApp for clarity
- Add NewApp function for creating a new kratos App instance
- Move config file to TOML format for better readability and structure
- Remove unused pagination import in greeter.biz.go
- Add absolute path resolution for config file path
- Print config path and bootstrap config for debugging purposes
  • Loading branch information
godcong committed Oct 11, 2024
1 parent 818bd30 commit da847b6
Show file tree
Hide file tree
Showing 13 changed files with 962 additions and 309 deletions.
11 changes: 8 additions & 3 deletions cmd/service/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/config"
Expand Down Expand Up @@ -30,10 +32,10 @@ var (
)

func init() {
flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml")
flag.StringVar(&flagconf, "conf", "configs", "config path, eg: -conf config.yaml")
}

func buildApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {
func NewApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {
return kratos.New(
kratos.ID(id),
kratos.Name(Name),
Expand All @@ -58,6 +60,8 @@ func main() {
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)
flagconf, _ = filepath.Abs(flagconf)
fmt.Println("load config at:", flagconf)
c := config.New(
config.WithSource(
file.NewSource(flagconf),
Expand All @@ -73,8 +77,9 @@ func main() {
if err := c.Scan(&bc); err != nil {
panic(err)
}
fmt.Println("show bootstrap config:", bc)

app, cleanup, err := wireApp(bc.Server, bc.Data, logger)
app, cleanup, err := buildApp(bc.Server, bc.Data, logger)
if err != nil {
panic(err)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/service/helloworld/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"github.com/origadmin/basic-layout/internal/mods/helloworld/biz"
"github.com/origadmin/basic-layout/internal/mods/helloworld/conf"
"github.com/origadmin/basic-layout/internal/mods/helloworld/dal"
"github.com/origadmin/basic-layout/internal/mods/helloworld/server"
"github.com/origadmin/basic-layout/internal/mods/helloworld/service"

Expand All @@ -15,9 +16,9 @@ import (
"github.com/google/wire"
)

//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:generate go run -mod=mod --tags wireinject github.com/google/wire/cmd/wire

// wireApp init kratos application.
func wireApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) {
panic(wire.Build(server.ProviderSet, dal.ProviderSet, biz.ProviderSet, service.ProviderSet, buildApp))
// buildApp init kratos application.
func buildApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) {
panic(wire.Build(server.ProviderSet, dal.ProviderSet, biz.ProviderSet, service.ProviderSet, NewApp))
}
10 changes: 5 additions & 5 deletions cmd/service/helloworld/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions configs/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[server.http]
addr = "0.0.0.0:8000"
timeout = "1s"

[server.grpc]
addr = "0.0.0.0:9000"
timeout = "1s"

[data.database]
driver = "mysql"
source = "root:root@tcp(127.0.0.1:3306)/test?parseTime=True&loc=Local"

[data.redis]
addr = "127.0.0.1:6379"
read_timeout = "0.2s"
write_timeout = "0.2s"
15 changes: 0 additions & 15 deletions configs/config.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions internal/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package internal

//generate config proto file
//go:generate protoc -I. -I../third_party --go_out=../ --go-http_out=../ --go-grpc_out=../ ./mods/helloworld/conf/*.proto
5 changes: 2 additions & 3 deletions internal/mods/helloworld/biz/greeter.biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/go-kratos/kratos/v2/log"
"github.com/origadmin/toolkits/third_party/pagination"

"github.com/origadmin/basic-layout/api/v1/services/helloworld"
"github.com/origadmin/basic-layout/internal/mods/helloworld/dto"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (uc *GreeterBiz) CreateGreeter(ctx context.Context, g *dto.Greeter) (*dto.G
return uc.repo.Save(ctx, g)
}

func (uc *GreeterBiz) ListGreeter(ctx context.Context, g *dto.Greeter, params *pagination.PagingRequest) ([]*dto.Greeter, error) {
func (uc *GreeterBiz) ListGreeter(ctx context.Context, g *dto.Greeter) ([]*dto.Greeter, error) {
uc.log.WithContext(ctx).Infof("ListGreeter: %v", g.Hello)
return uc.repo.ListByHello(ctx, g.Hello, &dto.GreeterQueryParam{PagingRequest: *params})
return uc.repo.ListByHello(ctx, g.Hello, &dto.GreeterQueryParam{})
}
Loading

0 comments on commit da847b6

Please sign in to comment.