Skip to content

Commit

Permalink
Remove dispatcher and eventsubscribers modules, remove statsd integra…
Browse files Browse the repository at this point in the history
…tion, remove event bus usage. Overall cleanup before otel integration
  • Loading branch information
erickskrauch committed Feb 7, 2024
1 parent 5d7a663 commit cecd07c
Show file tree
Hide file tree
Showing 23 changed files with 29 additions and 863 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ module ely.by/chrly

go 1.21

replace github.com/asaskevich/EventBus v0.0.0-20200330115301-33b3bc6a7ddc => github.com/erickskrauch/EventBus v0.0.0-20200330115301-33b3bc6a7ddc

// Main dependencies
require (
github.com/asaskevich/EventBus v0.0.0-20200330115301-33b3bc6a7ddc
github.com/brunomvsouza/singleflight v0.4.0
github.com/defval/di v1.12.0
github.com/etherlabsio/healthcheck/v2 v2.0.0
Expand Down Expand Up @@ -42,7 +39,6 @@ require (
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mono83/udpwriter v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/defval/di v1.12.0 h1:xXm7BMX2+Nr0Yyu55DeJl/rmfCA7CQX89f4AGE0zA6U=
github.com/defval/di v1.12.0/go.mod h1:PhVbOxQOvU7oawTOJXXTvqOJp1Dvsjs5PuzMw9gGl0I=
github.com/erickskrauch/EventBus v0.0.0-20200330115301-33b3bc6a7ddc h1:kz3f5uMA1LxfRvJjZmMYG7Zu2rddTfJy6QZofz2YoGQ=
github.com/erickskrauch/EventBus v0.0.0-20200330115301-33b3bc6a7ddc/go.mod h1:RHSo3YFV/SbOGyFR36RKWaXPy3g9nKAmn6ebNLpbco4=
github.com/etherlabsio/healthcheck/v2 v2.0.0 h1:oKq8cbpwM/yNGPXf2Sff6MIjVUjx/pGYFydWzeK2MpA=
github.com/etherlabsio/healthcheck/v2 v2.0.0/go.mod h1:huNVOjKzu6FI1eaO1CGD3ZjhrmPWf5Obu/pzpI6/wog=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down Expand Up @@ -59,8 +57,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mono83/slf v0.0.0-20170919161409-79153e9636db h1:tlz4fTklh5mttoq5M+0yEc5Lap8W/02A2HCXCJn5iz0=
github.com/mono83/slf v0.0.0-20170919161409-79153e9636db/go.mod h1:MfF+zNMZz+5IGY9h8jpFaGLyGoJ2ZPri2FmUVftBoUU=
github.com/mono83/udpwriter v1.0.2 h1:JiQ/N646oZoJA1G0FOMvn2teMt6SdL1KwNH2mszOlQs=
github.com/mono83/udpwriter v1.0.2/go.mod h1:mTDiyLtA0tXoxckkV9T4NUkJTgSQIuO8pAUKx/dSRkQ=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
Expand Down Expand Up @@ -92,7 +88,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
Expand Down Expand Up @@ -126,6 +121,5 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions internal/db/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ func storeMojangUuid(ctx context.Context, conn radix.Conn, username string, uuid
return nil
}

func (r *Redis) Ping() error {
return r.client.Do(r.context, radix.Cmd(nil, "PING"))
func (r *Redis) Ping(ctx context.Context) error {
return r.client.Do(ctx, radix.Cmd(nil, "PING"))
}

func normalizeUuid(uuid string) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/db/redis/redis_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,6 @@ func (s *redisTestSuite) TestStoreUuid() {
}

func (s *redisTestSuite) TestPing() {
err := s.Redis.Ping()
err := s.Redis.Ping(context.Background())
s.Require().Nil(err)
}
2 changes: 1 addition & 1 deletion internal/di/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/viper"
)

var config = di.Options(
var configDiOptions = di.Options(
di.Provide(newConfig),
)

Expand Down
13 changes: 5 additions & 8 deletions internal/di/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import (
"fmt"

"github.com/defval/di"
"github.com/etherlabsio/healthcheck/v2"
"github.com/spf13/viper"

db2 "ely.by/chrly/internal/db"
"ely.by/chrly/internal/db"
"ely.by/chrly/internal/db/redis"
es "ely.by/chrly/internal/eventsubscribers"
"ely.by/chrly/internal/mojang"
"ely.by/chrly/internal/profiles"
)

// v4 had the idea that it would be possible to separate backends for storing skins and capes.
// But in v5 the storage will be unified, so this is just temporary constructors before large reworking.
//
// Since there are no options for selecting target backends,
// all constants in this case point to static specific implementations.
var db = di.Options(
var dbDeOptions = di.Options(
di.Provide(newRedis,
di.As(new(profiles.ProfilesRepository)),
di.As(new(profiles.ProfilesFinder)),
Expand All @@ -34,7 +31,7 @@ func newRedis(container *di.Container, config *viper.Viper) (*redis.Redis, error

conn, err := redis.New(
context.Background(),
db2.NewZlibEncoder(&db2.JsonSerializer{}),
db.NewZlibEncoder(&db.JsonSerializer{}),
fmt.Sprintf("%s:%d", config.GetString("storage.redis.host"), config.GetInt("storage.redis.port")),
config.GetInt("storage.redis.poolSize"),
)
Expand All @@ -45,7 +42,7 @@ func newRedis(container *di.Container, config *viper.Viper) (*redis.Redis, error
if err := container.Provide(func() *namedHealthChecker {
return &namedHealthChecker{
Name: "redis",
Checker: es.DatabaseChecker(conn),
Checker: healthcheck.CheckerFunc(conn.Ping),
}
}); err != nil {
return nil, err
Expand Down
15 changes: 7 additions & 8 deletions internal/di/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import "github.com/defval/di"

func New() (*di.Container, error) {
return di.New(
config,
dispatcher,
logger,
db,
mojangTextures,
handlers,
profilesDi,
server,
configDiOptions,
loggerDiOptions,
dbDeOptions,
mojangDiOptions,
handlersDiOptions,
profilesDiOptions,
serverDiOptions,
securityDiOptions,
)
}
34 changes: 0 additions & 34 deletions internal/di/dispatcher.go

This file was deleted.

26 changes: 5 additions & 21 deletions internal/di/handlers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package di

import (
"errors"
"net/http"
"slices"
"strings"

"github.com/defval/di"
Expand All @@ -13,7 +13,7 @@ import (
. "ely.by/chrly/internal/http"
)

var handlers = di.Options(
var handlersDiOptions = di.Options(
di.Provide(newHandlerFactory, di.As(new(http.Handler))),
di.Provide(newSkinsystemHandler, di.WithName("skinsystem")),
di.Provide(newApiHandler, di.WithName("api")),
Expand All @@ -22,7 +22,6 @@ var handlers = di.Options(
func newHandlerFactory(
container *di.Container,
config *viper.Viper,
emitter Emitter,
) (*mux.Router, error) {
enabledModules := config.GetStringSlice("modules")

Expand All @@ -31,7 +30,7 @@ func newHandlerFactory(
// if you set an empty prefix. Since the main application should be mounted at the root prefix,
// we use it as the base router
var router *mux.Router
if hasValue(enabledModules, "skinsystem") {
if slices.Contains(enabledModules, "skinsystem") {
if err := container.Resolve(&router, di.Name("skinsystem")); err != nil {
return nil, err
}
Expand All @@ -40,13 +39,13 @@ func newHandlerFactory(
}

router.StrictSlash(true)
requestEventsMiddleware := CreateRequestEventsMiddleware(emitter, "skinsystem")
requestEventsMiddleware := CreateRequestEventsMiddleware()
router.Use(requestEventsMiddleware)
// NotFoundHandler doesn't call for registered middlewares, so we must wrap it manually.
// See https://github.com/gorilla/mux/issues/416#issuecomment-600079279
router.NotFoundHandler = requestEventsMiddleware(http.HandlerFunc(NotFoundHandler))

if hasValue(enabledModules, "api") {
if slices.Contains(enabledModules, "api") {
var apiRouter *mux.Router
if err := container.Resolve(&apiRouter, di.Name("api")); err != nil {
return nil, err
Expand All @@ -62,11 +61,6 @@ func newHandlerFactory(
mount(router, "/api", apiRouter)
}

err := container.Invoke(enableReporters)
if err != nil && !errors.Is(err, di.ErrTypeNotExists) {
return nil, err
}

// Resolve health checkers last, because all the services required by the application
// must first be initialized and each of them can publish its own checkers
var healthCheckers []*namedHealthChecker
Expand Down Expand Up @@ -108,16 +102,6 @@ func newApiHandler(profilesManager ProfilesManager) *mux.Router {
}).Handler()
}

func hasValue(slice []string, needle string) bool {
for _, value := range slice {
if value == needle {
return true
}
}

return false
}

func mount(router *mux.Router, path string, handler http.Handler) {
router.PathPrefix(path).Handler(
http.StripPrefix(
Expand Down
38 changes: 1 addition & 37 deletions internal/di/logger.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package di

import (
"os"

"github.com/defval/di"
"github.com/getsentry/raven-go"
"github.com/mono83/slf"
"github.com/mono83/slf/rays"
"github.com/mono83/slf/recievers/sentry"
"github.com/mono83/slf/recievers/statsd"
"github.com/mono83/slf/recievers/writer"
"github.com/mono83/slf/wd"
"github.com/spf13/viper"

"ely.by/chrly/internal/eventsubscribers"
"ely.by/chrly/internal/version"
)

var logger = di.Options(
var loggerDiOptions = di.Options(
di.Provide(newLogger),
di.Provide(newSentry),
di.Provide(newStatsReporter),
)

type loggerParams struct {
Expand Down Expand Up @@ -71,34 +66,3 @@ func newSentry(config *viper.Viper) (*raven.Client, error) {

return ravenClient, nil
}

func newStatsReporter(config *viper.Viper) (slf.StatsReporter, error) {
dispatcher := &slf.Dispatcher{}

statsdAddr := config.GetString("statsd.addr")
if statsdAddr != "" {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}

statsdReceiver, err := statsd.NewReceiver(statsd.Config{
Address: statsdAddr,
Prefix: "ely.skinsystem." + hostname + ".app.",
FlushEvery: 1,
})
if err != nil {
return nil, err
}

dispatcher.AddReceiver(statsdReceiver)
}

return wd.Custom("", "", dispatcher), nil
}

func enableReporters(reporter slf.StatsReporter, factories []eventsubscribers.Reporter) {
for _, factory := range factories {
factory.Enable(reporter)
}
}
2 changes: 1 addition & 1 deletion internal/di/mojang_textures.go → internal/di/mojang.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"ely.by/chrly/internal/profiles"
)

var mojangTextures = di.Options(
var mojangDiOptions = di.Options(
di.Provide(newMojangApi),
di.Provide(newMojangTexturesProviderFactory),
di.Provide(newMojangTexturesProvider),
Expand Down
2 changes: 1 addition & 1 deletion internal/di/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"ely.by/chrly/internal/profiles"
)

var profilesDi = di.Options(
var profilesDiOptions = di.Options(
di.Provide(newProfilesManager, di.As(new(ProfilesManager))),
di.Provide(newProfilesProvider, di.As(new(ProfilesProvider))),
)
Expand Down
2 changes: 1 addition & 1 deletion internal/di/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"ely.by/chrly/internal/security"
)

var server = di.Options(
var serverDiOptions = di.Options(
di.Provide(newAuthenticator, di.As(new(Authenticator))),
di.Provide(newServer),
)
Expand Down
34 changes: 0 additions & 34 deletions internal/dispatcher/dispatcher.go

This file was deleted.

Loading

0 comments on commit cecd07c

Please sign in to comment.