Skip to content

Commit

Permalink
feat: migrate api endpoints to use use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeii committed Dec 21, 2023
1 parent fae63c0 commit f3b66ec
Show file tree
Hide file tree
Showing 80 changed files with 2,813 additions and 1,796 deletions.
6 changes: 4 additions & 2 deletions cmd/swat4master/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package application
import (
"go.uber.org/fx"

"github.com/sergeii/swat4master/cmd/swat4master/clock"
"github.com/sergeii/swat4master/cmd/swat4master/container"
"github.com/sergeii/swat4master/cmd/swat4master/logging"
"github.com/sergeii/swat4master/cmd/swat4master/persistence"
"github.com/sergeii/swat4master/cmd/swat4master/timing"
"github.com/sergeii/swat4master/internal/services/discovery/finding"
"github.com/sergeii/swat4master/internal/services/monitoring"
"github.com/sergeii/swat4master/internal/services/probe"
Expand All @@ -15,12 +16,13 @@ import (

var Module = fx.Module("application",
fx.Provide(logging.Provide),
fx.Provide(timing.Provide),
fx.Provide(clock.Provide),
fx.Invoke(logging.NoGlobal),
fx.Provide(validation.New),
fx.Provide(persistence.Provide),
fx.Provide(monitoring.NewMetricService),
fx.Provide(finding.NewService),
fx.Provide(server.NewService),
fx.Provide(probe.NewService),
container.Module,
)
9 changes: 9 additions & 0 deletions cmd/swat4master/clock/clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package clock

import (
"github.com/jonboulle/clockwork"
)

func Provide() clockwork.Clock {
return clockwork.NewRealClock()
}
3 changes: 2 additions & 1 deletion cmd/swat4master/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Config struct {

ExporterListenAddr string

DiscoveryRefreshInterval time.Duration
DiscoveryRefreshInterval time.Duration

DiscoveryRevivalInterval time.Duration
DiscoveryRevivalScope time.Duration
DiscoveryRevivalCountdown time.Duration
Expand Down
34 changes: 34 additions & 0 deletions cmd/swat4master/container/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package container

import (
"go.uber.org/fx"

"github.com/sergeii/swat4master/internal/core/usecases/addserver"
"github.com/sergeii/swat4master/internal/core/usecases/getserver"
"github.com/sergeii/swat4master/internal/core/usecases/listservers"
)

type Container struct {
GetServer getserver.UseCase
AddServer addserver.UseCase
ListServers listservers.UseCase
}

func New(
getServerUseCase getserver.UseCase,
addServerUseCase addserver.UseCase,
listServersUseCase listservers.UseCase,
) Container {
return Container{
GetServer: getServerUseCase,
AddServer: addServerUseCase,
ListServers: listServersUseCase,
}
}

var Module = fx.Module("container",
fx.Provide(getserver.New),
fx.Provide(addserver.New),
fx.Provide(listservers.New),
fx.Provide(New),
)
9 changes: 6 additions & 3 deletions cmd/swat4master/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
"github.com/sergeii/swat4master/cmd/swat4master/modules/cleaner"
"github.com/sergeii/swat4master/cmd/swat4master/modules/collector"
"github.com/sergeii/swat4master/cmd/swat4master/modules/exporter"
"github.com/sergeii/swat4master/cmd/swat4master/modules/finder"
"github.com/sergeii/swat4master/cmd/swat4master/modules/prober"
"github.com/sergeii/swat4master/cmd/swat4master/modules/refresher"
"github.com/sergeii/swat4master/cmd/swat4master/modules/reporter"
"github.com/sergeii/swat4master/cmd/swat4master/modules/reviver"

"github.com/sergeii/swat4master/cmd/swat4master/application"
)
Expand All @@ -26,7 +27,8 @@ func main() {
app := fx.New(
fx.Provide(config.Provide),
application.Module,
finder.Module,
refresher.Module,
reviver.Module,
prober.Module,
browser.Module,
reporter.Module,
Expand Down Expand Up @@ -56,7 +58,8 @@ func main() {
}),
fx.Invoke(func(
logger *zerolog.Logger,
_ *finder.Finder,
_ *refresher.Refresher,
_ *reviver.Reviver,
_ *prober.Prober,
_ *browser.Browser,
_ *reporter.Reporter,
Expand Down
54 changes: 0 additions & 54 deletions cmd/swat4master/modules/api/api_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions cmd/swat4master/modules/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"

_ "github.com/sergeii/swat4master/api/docs" // nolint: revive
h "github.com/sergeii/swat4master/cmd/swat4master/modules/api/handlers"
"github.com/sergeii/swat4master/internal/rest/api"
)

func NewRouter(a *api.API) *gin.Engine {
router := gin.Default()
router.GET("/status", h.Status)
router.GET("/status", a.Status)
router.GET("/api/servers", a.ListServers)
router.GET("/api/servers/:address", a.ViewServer)
router.POST("/api/servers", a.AddServer)
Expand Down
5 changes: 1 addition & 4 deletions cmd/swat4master/modules/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net"

"github.com/benbjohnson/clock"
"github.com/rs/zerolog"
"go.uber.org/fx"

Expand All @@ -19,17 +18,15 @@ type Browser struct{}
type Handler struct {
service *browsing.Service
metrics *monitoring.MetricService
clock clock.Clock
logger *zerolog.Logger
}

func newHandler(
service *browsing.Service,
metrics *monitoring.MetricService,
clock clock.Clock,
logger *zerolog.Logger,
) *Handler {
return &Handler{service, metrics, clock, logger}
return &Handler{service, metrics, logger}
}

func NewBrowser(
Expand Down
5 changes: 3 additions & 2 deletions cmd/swat4master/modules/browser/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"fmt"
"net"
"time"
)

func (h *Handler) Handle(ctx context.Context, conn *net.TCPConn) {
defer conn.Close()
reqStarted := h.clock.Now()
reqStarted := time.Now()

buf := make([]byte, 2048)
n, err := conn.Read(buf)
Expand Down Expand Up @@ -52,5 +53,5 @@ func (h *Handler) Handle(ctx context.Context, conn *net.TCPConn) {
}
}
h.metrics.BrowserRequests.Inc()
h.metrics.BrowserDurations.Observe(h.clock.Since(reqStarted).Seconds())
h.metrics.BrowserDurations.Observe(time.Since(reqStarted).Seconds())
}
11 changes: 6 additions & 5 deletions cmd/swat4master/modules/cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cleaner
import (
"context"

"github.com/benbjohnson/clock"
"github.com/jonboulle/clockwork"
"github.com/rs/zerolog"
"go.uber.org/fx"

Expand All @@ -16,12 +16,13 @@ type Cleaner struct{}
func Run(
stop chan struct{},
stopped chan struct{},
clock clock.Clock,
clock clockwork.Clock,
logger *zerolog.Logger,
service *cleaning.Service,
cfg config.Config,
) {
ticker := clock.Ticker(cfg.CleanInterval)
ticker := clock.NewTicker(cfg.CleanInterval)
tickerCh := ticker.Chan()
defer ticker.Stop()

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -37,7 +38,7 @@ func Run(
logger.Info().Msg("Stopping cleaner")
close(stopped)
return
case <-ticker.C:
case <-tickerCh:
if err := service.Clean(ctx, clock.Now().Add(-cfg.CleanRetention)); err != nil {
logger.Error().
Err(err).
Expand All @@ -50,7 +51,7 @@ func Run(
func NewCleaner(
lc fx.Lifecycle,
cfg config.Config,
clock clock.Clock,
clock clockwork.Clock,
service *cleaning.Service,
logger *zerolog.Logger,
) *Cleaner {
Expand Down
11 changes: 6 additions & 5 deletions cmd/swat4master/modules/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package collector
import (
"context"

"github.com/benbjohnson/clock"
"github.com/jonboulle/clockwork"
"github.com/rs/zerolog"
"go.uber.org/fx"

Expand All @@ -16,12 +16,13 @@ type Collector struct{}
func Run(
stop chan struct{},
stopped chan struct{},
clock clock.Clock,
clock clockwork.Clock,
logger *zerolog.Logger,
metrics *monitoring.MetricService,
cfg config.Config,
) {
ticker := clock.Ticker(cfg.CollectorInterval)
ticker := clock.NewTicker(cfg.CollectorInterval)
tickerCh := ticker.Chan()
defer ticker.Stop()

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -37,7 +38,7 @@ func Run(
logger.Info().Msg("Stopping collector")
close(stopped)
return
case <-ticker.C:
case <-tickerCh:
metrics.Observe(ctx, monitoring.ObserverConfig{
ServerLiveness: cfg.BrowserServerLiveness,
})
Expand All @@ -48,8 +49,8 @@ func Run(
func NewCollector(
lc fx.Lifecycle,
cfg config.Config,
clock clockwork.Clock,
metrics *monitoring.MetricService,
clock clock.Clock,
logger *zerolog.Logger,
) *Collector {
stopped := make(chan struct{})
Expand Down
Loading

0 comments on commit f3b66ec

Please sign in to comment.