Skip to content

Commit

Permalink
Remove the util package
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 14, 2024
1 parent e084699 commit 169ebf8
Show file tree
Hide file tree
Showing 98 changed files with 238 additions and 338 deletions.
4 changes: 2 additions & 2 deletions frontend/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (

"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/leighmacdonald/gbans/pkg/fs"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
)

func AddRoutes(engine *gin.Engine, root string) error {
if root == "" {
root = "frontend/dist"
}

if !util.Exists(filepath.Join(root, "index.html")) {
if !fs.Exists(filepath.Join(root, "index.html")) {
return ErrContentRoot
}

Expand Down
4 changes: 2 additions & 2 deletions internal/auth/auth_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/gbans/pkg/stringutil"
"github.com/leighmacdonald/steamid/v4/steamid"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ func (u *auth) MakeToken(ctx *gin.Context, cookieKey string, sid steamid.SteamID
return domain.UserTokens{}, domain.ErrCookieKeyMissing
}

fingerprint := util.SecureRandomString(40)
fingerprint := stringutil.SecureRandomString(40)

accessToken, errAccess := u.NewUserToken(sid, cookieKey, fingerprint, domain.AuthTokenDuration)
if errAccess != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/ban/ban_asn_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/httphelper"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
)

type banASNHandler struct {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (h banASNHandler) onAPIPostBansASNCreate() gin.HandlerFunc {
sid = httphelper.CurrentUserProfile(ctx).SteamID
)

duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil)
duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil)
if errDuration != nil {
httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)

Expand Down
4 changes: 2 additions & 2 deletions internal/ban/ban_net_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/httphelper"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
)

Expand Down Expand Up @@ -82,7 +82,7 @@ func (h banNetHandler) onAPIPostBansCIDRCreate() gin.HandlerFunc {
sid = httphelper.CurrentUserProfile(ctx).SteamID
)

duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil)
duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil)
if errDuration != nil {
httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)

Expand Down
4 changes: 2 additions & 2 deletions internal/ban/ban_steam_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/httphelper"
"github.com/leighmacdonald/gbans/internal/thirdparty"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -146,7 +146,7 @@ func (h banHandler) onAPIPostBanSteamCreate() gin.HandlerFunc {
sid = sourceID
}

duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil)
duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil)
if errDuration != nil {
httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)

Expand Down
4 changes: 2 additions & 2 deletions internal/ban/ban_steam_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/leighmacdonald/gbans/internal/discord"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/leighmacdonald/steamweb/v2"
)
Expand Down Expand Up @@ -223,7 +223,7 @@ func (s banSteamUsecase) CheckEvadeStatus(ctx context.Context, curUser domain.Pe
return false, errMatch
}

duration, errDuration := util.ParseUserStringDuration("10y")
duration, errDuration := datetime.ParseUserStringDuration("10y")
if errDuration != nil {
return false, errDuration
}
Expand Down
4 changes: 2 additions & 2 deletions internal/blocklist/blocklist_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"time"

"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/httphelper"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
)

Expand Down Expand Up @@ -125,7 +125,7 @@ func (b blocklistUsecase) updateSource(ctx context.Context, list domain.CIDRBloc
return errors.Join(errReq, domain.ErrRequestCreate)
}

client := util.NewHTTPClient()
client := httphelper.NewHTTPClient()

resp, errResp := client.Do(req)
if errResp != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/chat/chat_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/gofrs/uuid/v5"
"github.com/leighmacdonald/gbans/internal/database"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/fp"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/logparse"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
)

Expand Down Expand Up @@ -238,7 +238,7 @@ func (r chatRepository) QueryChatHistory(ctx context.Context, filters domain.Cha
if !filters.Unrestricted {
unrTime := now.AddDate(0, 0, -30)
if filters.DateStart != nil && filters.DateStart.Before(unrTime) {
return nil, util.ErrInvalidDuration
return nil, datetime.ErrInvalidDuration
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/chat/chat_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/leighmacdonald/gbans/internal/discord"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/state"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
)

Expand Down Expand Up @@ -65,7 +65,7 @@ func (u chatUsecase) onWarningExceeded(ctx context.Context, newWarning domain.Ne
)

if newWarning.MatchedFilter.Action == domain.Ban || newWarning.MatchedFilter.Action == domain.Mute {
duration, errDuration := util.ParseDuration(newWarning.MatchedFilter.Duration)
duration, errDuration := datetime.ParseDuration(newWarning.MatchedFilter.Duration)
if errDuration != nil {
return fmt.Errorf("invalid duration: %w", errDuration)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/httphelper"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/leighmacdonald/steamweb/v2"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -207,7 +207,7 @@ func getGithubReleases(ctx context.Context) ([]GithubRelease, error) {
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")

client := util.NewHTTPClient()
client := httphelper.NewHTTPClient()

resp, errResp := client.Do(req)
if errResp != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/config/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"strings"

"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/stringutil"
"github.com/mitchellh/go-homedir"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
Expand All @@ -25,7 +26,7 @@ func decodeDuration() mapstructure.DecodeHookFuncType {
return data, nil
}

duration, errDuration := util.ParseUserStringDuration(data.(string))
duration, errDuration := datetime.ParseUserStringDuration(data.(string))
if errDuration != nil {
return nil, errors.Join(errDuration, fmt.Errorf("%w: %s", domain.ErrDecodeDuration, target.String()))
}
Expand All @@ -52,7 +53,7 @@ func setDefaultConfigValues() {
"http_host": "127.0.0.1",
"http_port": 6006,
"http_static_path": "frontend/dist",
"http_cookie_key": util.SecureRandomString(32),
"http_cookie_key": stringutil.SecureRandomString(32),
"http_client_timeout": "10s",
"http_cors_origins": []string{"http://gbans.localhost"},
"database_dsn": "postgresql://gbans:gbans@localhost/gbans",
Expand Down
4 changes: 2 additions & 2 deletions internal/database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/golang-migrate/migrate/v4"
pgxMigrate "github.com/golang-migrate/migrate/v4/database/pgx"
"github.com/golang-migrate/migrate/v4/source/httpfs"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/gbans/pkg/log"
)

// MigrationAction is the type of migration to perform.
Expand Down Expand Up @@ -58,7 +58,7 @@ func (db *postgresStore) migrate(action MigrationAction, dsn string) error {
return errors.Join(errMigrate, ErrMigrationDriver)
}

defer util.LogCloser(driver)
defer log.Closer(driver)

source, errHTTPFS := httpfs.New(http.FS(migrations), "migrations")
if errHTTPFS != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/discord/discord_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
)

type nullDiscordRepository struct{}
Expand Down Expand Up @@ -82,7 +81,7 @@ func (bot *discordRepository) RegisterHandler(cmd domain.Cmd, handler domain.Sla

func (bot *discordRepository) Shutdown(guildID string) {
if bot.session != nil {
defer util.LogCloser(bot.session)
defer log.Closer(bot.session)
bot.botUnregisterSlashCommands(guildID)
}
}
Expand Down
16 changes: 8 additions & 8 deletions internal/discord/discord_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/gofrs/uuid/v5"
"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/thirdparty"
"github.com/leighmacdonald/gbans/pkg/datetime"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/steamid/v4/steamid"
)

Expand Down Expand Up @@ -764,9 +764,9 @@ func (h discordService) makeOnMute() func(context.Context, *discordgo.Session, *

reason = domain.Reason(reasonValueOpt.IntValue())

duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue())
duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue())
if errDuration != nil {
return nil, util.ErrInvalidDuration
return nil, datetime.ErrInvalidDuration
}

modNote := opts[domain.OptNote].StringValue()
Expand Down Expand Up @@ -805,9 +805,9 @@ func (h discordService) onBanASN(ctx context.Context, _ *discordgo.Session,
return nil, domain.ErrInvalidSID
}

duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue())
duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue())
if errDuration != nil {
return nil, util.ErrInvalidDuration
return nil, datetime.ErrInvalidDuration
}

author, errGetPersonByDiscordID := h.persons.GetPersonByDiscordID(ctx, interaction.Interaction.Member.User.ID)
Expand Down Expand Up @@ -858,7 +858,7 @@ func (h discordService) onBanIP(ctx context.Context, _ *discordgo.Session,
return nil, errors.Join(errParseCIDR, domain.ErrNetworkInvalidIP)
}

duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue())
duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue())
if errDuration != nil {
return nil, errDuration
}
Expand Down Expand Up @@ -914,9 +914,9 @@ func (h discordService) onBanSteam(ctx context.Context, _ *discordgo.Session,
return nil, domain.ErrInvalidSID
}

duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue())
duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue())
if errDuration != nil {
return nil, util.ErrInvalidDuration
return nil, datetime.ErrInvalidDuration
}

author, errAuthor := h.getDiscordAuthor(ctx, interaction)
Expand Down
13 changes: 7 additions & 6 deletions internal/discord/discord_usecase_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ import (
"time"

"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/internal/httphelper"
"github.com/leighmacdonald/gbans/pkg/log"
"github.com/leighmacdonald/gbans/pkg/util"
"github.com/leighmacdonald/gbans/pkg/oauth"
"github.com/leighmacdonald/steamid/v4/steamid"
)

type discordOAuthUsecase struct {
config domain.ConfigUsecase
state *util.LoginStateTracker
state *oauth.LoginStateTracker
repository domain.DiscordOAuthRepository
}

func NewDiscordOAuthUsecase(repository domain.DiscordOAuthRepository, config domain.ConfigUsecase) domain.DiscordOAuthUsecase {
return &discordOAuthUsecase{
repository: repository,
config: config,
state: util.NewLoginStateTracker(),
state: oauth.NewLoginStateTracker(),
}
}

Expand Down Expand Up @@ -95,7 +96,7 @@ func (d discordOAuthUsecase) fetchRefresh(ctx context.Context, credentials domai

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, errResp := util.NewHTTPClient().Do(req)
resp, errResp := httphelper.NewHTTPClient().Do(req)
if errResp != nil {
return domain.DiscordCredential{}, errors.Join(errResp, domain.ErrRequestPerform)
}
Expand Down Expand Up @@ -160,7 +161,7 @@ func (d discordOAuthUsecase) Logout(ctx context.Context, steamID steamid.SteamID

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, errResp := util.NewHTTPClient().Do(req)
resp, errResp := httphelper.NewHTTPClient().Do(req)
if errResp != nil {
return errors.Join(errResp, domain.ErrRequestPerform)
}
Expand Down Expand Up @@ -193,7 +194,7 @@ func (d discordOAuthUsecase) CreateStatefulLoginURL(steamID steamid.SteamID) (st
}

func (d discordOAuthUsecase) HandleOAuthCode(ctx context.Context, code string, state string) error {
client := util.NewHTTPClient()
client := httphelper.NewHTTPClient()

steamID, found := d.state.Get(state)
if !found {
Expand Down
Loading

0 comments on commit 169ebf8

Please sign in to comment.