Skip to content

Commit

Permalink
feat: refactor scan package dependencies
Browse files Browse the repository at this point in the history
- Add `github.com/blackhorseya/ryze/app/infra/transports/httpx` to imports in `adpater/block/scan/impl.go`
- Update `NewRestful` function signature in `adpater/block/scan/impl.go`
- Add `github.com/blackhorseya/ryze/pkg/contextx` to imports in `adpater/block/scan/impl.go`
- Add `go.uber.org/zap` to imports in `adpater/block/scan/impl.go`
- Add `main_test.go` file with tests for `scan` package
- Add `github.com/blackhorseya/ryze/app/infra/transports/httpx` to imports in `adpater/block/scan/wire.go`
- Update `New` function in `adpater/block/scan/wire.go`
- Add `github.com/blackhorseya/ryze/app/infra/transports/httpx` to imports in `adpater/block/scan/wire_gen.go`
- Update `New` function in `adpater/block/scan/wire_gen.go`
- Add `github.com/blackhorseya/ryze/app/infra/transports/httpx` to imports in `adpater/block/wirex/injector.go`
- Add `app/infra/transports/httpx` to imports in `app/infra/configx/app.go`
- Add `app/infra/transports/httpx` to imports in `

Signed-off-by: Sean Zheng <[email protected]>
  • Loading branch information
blackhorseya committed Jul 27, 2024
1 parent 261856c commit c7f9489
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 10 deletions.
47 changes: 38 additions & 9 deletions adpater/block/scan/impl.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
package scan

import (
"fmt"

"github.com/blackhorseya/ryze/adpater/block/wirex"
"github.com/blackhorseya/ryze/app/infra/transports/httpx"
"github.com/blackhorseya/ryze/pkg/adapterx"
"github.com/blackhorseya/ryze/pkg/contextx"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

type restful struct {
injector *wirex.Injector
server *httpx.Server
}

// NewRestful is used to create a new adapterx.Restful
func NewRestful(injector *wirex.Injector) adapterx.Restful {
func NewRestful(injector *wirex.Injector, server *httpx.Server) adapterx.Restful {
return &restful{
injector: injector,
server: server,
}
}

func (i *restful) Start() error {
// TODO implement me
panic("implement me")
ctx := contextx.Background()

err := i.InitRouting()
if err != nil {
ctx.Error("Failed to init routing", zap.Error(err))
return err
}

err = i.server.Start(ctx)
if err != nil {
ctx.Error("Failed to start server", zap.Error(err))
return err
}

swaggerURL := fmt.Sprintf("http://localhost:%d/api/docs/index.html", i.injector.A.HTTP.Port)
ctx.Info("start restful server", zap.String("swagger_url", swaggerURL))

return nil
}

func (i *restful) AwaitSignal() error {
// TODO implement me
panic("implement me")
ctx := contextx.Background()
ctx.Info("receive signal to stop server")

if err := i.server.Stop(ctx); err != nil {
ctx.Error("Failed to stop server", zap.Error(err))
return fmt.Errorf("failed to stop server: %w", err)
}

return nil
}

func (i *restful) InitRouting() error {
// TODO implement me
panic("implement me")
// TODO: 2024/7/27|sean|implement me
return nil
}

func (i *restful) GetRouter() *gin.Engine {
// TODO implement me
panic("implement me")
return i.server.Router
}
34 changes: 34 additions & 0 deletions adpater/block/scan/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build external

package scan

import (
"os"
"os/signal"
"syscall"
"testing"

"github.com/spf13/viper"
)

func TestRun(t *testing.T) {
restful, err := New(viper.New())
if err != nil {
t.Fatalf("New() error = %v", err)
}

err = restful.Start()
if err != nil {
t.Fatalf("Start() error = %v", err)
}

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT)

<-signalChan

err = restful.AwaitSignal()
if err != nil {
t.Fatalf("AwaitSignal() error = %v", err)
}
}
11 changes: 11 additions & 0 deletions adpater/block/scan/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ package scan
import (
"github.com/blackhorseya/ryze/adpater/block/wirex"
"github.com/blackhorseya/ryze/app/infra/configx"
"github.com/blackhorseya/ryze/app/infra/transports/httpx"
"github.com/blackhorseya/ryze/pkg/adapterx"
"github.com/google/wire"
"github.com/spf13/viper"
)

func initApplication(config *configx.Configuration) (*configx.Application, error) {
return config.Services["block-scan"], nil
}

func initServer(app *configx.Application) (*httpx.Server, error) {
return httpx.NewServer(app.HTTP)
}

func New(v *viper.Viper) (adapterx.Restful, error) {
panic(wire.Build(
wire.Struct(new(wirex.Injector), "*"),
configx.NewConfiguration,
initApplication,

NewRestful,
initServer,
))
}
22 changes: 21 additions & 1 deletion adpater/block/scan/wire_gen.go

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

1 change: 1 addition & 0 deletions adpater/block/wirex/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
// Injector is the injector for wirex
type Injector struct {
C *configx.Configuration
A *configx.Application

// other fields
}
10 changes: 10 additions & 0 deletions app/infra/configx/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package configx

import (
"github.com/blackhorseya/ryze/app/infra/transports/httpx"
)

// Application is the application configuration.
type Application struct {
HTTP httpx.Options `json:"http" yaml:"http"`
}
2 changes: 2 additions & 0 deletions app/infra/configx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
type Configuration struct {
Log logging.Options `json:"log" yaml:"log"`
Networks map[string]*Network `json:"networks" yaml:"networks"`

Services map[string]*Application `json:"services" yaml:"services"`
}

// NewConfiguration creates a new configuration.
Expand Down

0 comments on commit c7f9489

Please sign in to comment.