Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
f51ad2e1
Commit
f51ad2e1
authored
Dec 25, 2025
by
Forest
Browse files
refactor: 删除 ports 目录
parent
f57f12c6
Changes
66
Show whitespace changes
Inline
Side-by-side
backend/internal/service/ports/api_key_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
)
// ApiKeyCache defines cache operations for API key service
type
ApiKeyCache
interface
{
GetCreateAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
(
int
,
error
)
IncrementCreateAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
error
DeleteCreateAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
error
IncrementDailyUsage
(
ctx
context
.
Context
,
apiKey
string
)
error
SetDailyUsageExpiry
(
ctx
context
.
Context
,
apiKey
string
,
ttl
time
.
Duration
)
error
}
backend/internal/service/ports/billing_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
)
// SubscriptionCacheData represents cached subscription data
type
SubscriptionCacheData
struct
{
Status
string
ExpiresAt
time
.
Time
DailyUsage
float64
WeeklyUsage
float64
MonthlyUsage
float64
Version
int64
}
// BillingCache defines cache operations for billing service
type
BillingCache
interface
{
// Balance operations
GetUserBalance
(
ctx
context
.
Context
,
userID
int64
)
(
float64
,
error
)
SetUserBalance
(
ctx
context
.
Context
,
userID
int64
,
balance
float64
)
error
DeductUserBalance
(
ctx
context
.
Context
,
userID
int64
,
amount
float64
)
error
InvalidateUserBalance
(
ctx
context
.
Context
,
userID
int64
)
error
// Subscription operations
GetSubscriptionCache
(
ctx
context
.
Context
,
userID
,
groupID
int64
)
(
*
SubscriptionCacheData
,
error
)
SetSubscriptionCache
(
ctx
context
.
Context
,
userID
,
groupID
int64
,
data
*
SubscriptionCacheData
)
error
UpdateSubscriptionUsage
(
ctx
context
.
Context
,
userID
,
groupID
int64
,
cost
float64
)
error
InvalidateSubscriptionCache
(
ctx
context
.
Context
,
userID
,
groupID
int64
)
error
}
backend/internal/service/ports/concurrency_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
"context"
// ConcurrencyCache defines cache operations for concurrency service
// Uses independent keys per request slot with native Redis TTL for automatic cleanup
type
ConcurrencyCache
interface
{
// Account slot management - each slot is a separate key with independent TTL
// Key format: concurrency:account:{accountID}:{requestID}
AcquireAccountSlot
(
ctx
context
.
Context
,
accountID
int64
,
maxConcurrency
int
,
requestID
string
)
(
bool
,
error
)
ReleaseAccountSlot
(
ctx
context
.
Context
,
accountID
int64
,
requestID
string
)
error
GetAccountConcurrency
(
ctx
context
.
Context
,
accountID
int64
)
(
int
,
error
)
// User slot management - each slot is a separate key with independent TTL
// Key format: concurrency:user:{userID}:{requestID}
AcquireUserSlot
(
ctx
context
.
Context
,
userID
int64
,
maxConcurrency
int
,
requestID
string
)
(
bool
,
error
)
ReleaseUserSlot
(
ctx
context
.
Context
,
userID
int64
,
requestID
string
)
error
GetUserConcurrency
(
ctx
context
.
Context
,
userID
int64
)
(
int
,
error
)
// Wait queue - uses counter with TTL set only on creation
IncrementWaitCount
(
ctx
context
.
Context
,
userID
int64
,
maxWait
int
)
(
bool
,
error
)
DecrementWaitCount
(
ctx
context
.
Context
,
userID
int64
)
error
}
backend/internal/service/ports/email_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
)
// VerificationCodeData represents verification code data
type
VerificationCodeData
struct
{
Code
string
Attempts
int
CreatedAt
time
.
Time
}
// EmailCache defines cache operations for email service
type
EmailCache
interface
{
GetVerificationCode
(
ctx
context
.
Context
,
email
string
)
(
*
VerificationCodeData
,
error
)
SetVerificationCode
(
ctx
context
.
Context
,
email
string
,
data
*
VerificationCodeData
,
ttl
time
.
Duration
)
error
DeleteVerificationCode
(
ctx
context
.
Context
,
email
string
)
error
}
backend/internal/service/ports/gateway_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
)
// GatewayCache defines cache operations for gateway service
type
GatewayCache
interface
{
GetSessionAccountID
(
ctx
context
.
Context
,
sessionHash
string
)
(
int64
,
error
)
SetSessionAccountID
(
ctx
context
.
Context
,
sessionHash
string
,
accountID
int64
,
ttl
time
.
Duration
)
error
RefreshSessionTTL
(
ctx
context
.
Context
,
sessionHash
string
,
ttl
time
.
Duration
)
error
}
backend/internal/service/ports/group.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"gorm.io/gorm"
)
type
GroupRepository
interface
{
Create
(
ctx
context
.
Context
,
group
*
model
.
Group
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
Group
,
error
)
Update
(
ctx
context
.
Context
,
group
*
model
.
Group
)
error
Delete
(
ctx
context
.
Context
,
id
int64
)
error
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
model
.
Group
,
*
pagination
.
PaginationResult
,
error
)
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
platform
,
status
string
,
isExclusive
*
bool
)
([]
model
.
Group
,
*
pagination
.
PaginationResult
,
error
)
ListActive
(
ctx
context
.
Context
)
([]
model
.
Group
,
error
)
ListActiveByPlatform
(
ctx
context
.
Context
,
platform
string
)
([]
model
.
Group
,
error
)
ExistsByName
(
ctx
context
.
Context
,
name
string
)
(
bool
,
error
)
GetAccountCount
(
ctx
context
.
Context
,
groupID
int64
)
(
int64
,
error
)
DeleteAccountGroupsByGroupID
(
ctx
context
.
Context
,
groupID
int64
)
(
int64
,
error
)
DB
()
*
gorm
.
DB
}
backend/internal/service/ports/identity_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
"context"
// Fingerprint represents account fingerprint data
type
Fingerprint
struct
{
ClientID
string
UserAgent
string
StainlessLang
string
StainlessPackageVersion
string
StainlessOS
string
StainlessArch
string
StainlessRuntime
string
StainlessRuntimeVersion
string
}
// IdentityCache defines cache operations for identity service
type
IdentityCache
interface
{
GetFingerprint
(
ctx
context
.
Context
,
accountID
int64
)
(
*
Fingerprint
,
error
)
SetFingerprint
(
ctx
context
.
Context
,
accountID
int64
,
fp
*
Fingerprint
)
error
}
backend/internal/service/ports/openai_oauth.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
)
// OpenAIOAuthClient interface for OpenAI OAuth operations
type
OpenAIOAuthClient
interface
{
ExchangeCode
(
ctx
context
.
Context
,
code
,
codeVerifier
,
redirectURI
,
proxyURL
string
)
(
*
openai
.
TokenResponse
,
error
)
RefreshToken
(
ctx
context
.
Context
,
refreshToken
,
proxyURL
string
)
(
*
openai
.
TokenResponse
,
error
)
}
backend/internal/service/ports/proxy.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
)
type
ProxyRepository
interface
{
Create
(
ctx
context
.
Context
,
proxy
*
model
.
Proxy
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
Proxy
,
error
)
Update
(
ctx
context
.
Context
,
proxy
*
model
.
Proxy
)
error
Delete
(
ctx
context
.
Context
,
id
int64
)
error
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
model
.
Proxy
,
*
pagination
.
PaginationResult
,
error
)
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
protocol
,
status
,
search
string
)
([]
model
.
Proxy
,
*
pagination
.
PaginationResult
,
error
)
ListActive
(
ctx
context
.
Context
)
([]
model
.
Proxy
,
error
)
ListActiveWithAccountCount
(
ctx
context
.
Context
)
([]
model
.
ProxyWithAccountCount
,
error
)
ExistsByHostPortAuth
(
ctx
context
.
Context
,
host
string
,
port
int
,
username
,
password
string
)
(
bool
,
error
)
CountAccountsByProxyID
(
ctx
context
.
Context
,
proxyID
int64
)
(
int64
,
error
)
}
backend/internal/service/ports/redeem_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
)
// RedeemCache defines cache operations for redeem service
type
RedeemCache
interface
{
GetRedeemAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
(
int
,
error
)
IncrementRedeemAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
error
AcquireRedeemLock
(
ctx
context
.
Context
,
code
string
,
ttl
time
.
Duration
)
(
bool
,
error
)
ReleaseRedeemLock
(
ctx
context
.
Context
,
code
string
)
error
}
backend/internal/service/ports/redeem_code.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
)
type
RedeemCodeRepository
interface
{
Create
(
ctx
context
.
Context
,
code
*
model
.
RedeemCode
)
error
CreateBatch
(
ctx
context
.
Context
,
codes
[]
model
.
RedeemCode
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
RedeemCode
,
error
)
GetByCode
(
ctx
context
.
Context
,
code
string
)
(
*
model
.
RedeemCode
,
error
)
Update
(
ctx
context
.
Context
,
code
*
model
.
RedeemCode
)
error
Delete
(
ctx
context
.
Context
,
id
int64
)
error
Use
(
ctx
context
.
Context
,
id
,
userID
int64
)
error
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
model
.
RedeemCode
,
*
pagination
.
PaginationResult
,
error
)
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
codeType
,
status
,
search
string
)
([]
model
.
RedeemCode
,
*
pagination
.
PaginationResult
,
error
)
ListByUser
(
ctx
context
.
Context
,
userID
int64
,
limit
int
)
([]
model
.
RedeemCode
,
error
)
}
backend/internal/service/ports/setting.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"github.com/Wei-Shaw/sub2api/internal/model"
)
type
SettingRepository
interface
{
Get
(
ctx
context
.
Context
,
key
string
)
(
*
model
.
Setting
,
error
)
GetValue
(
ctx
context
.
Context
,
key
string
)
(
string
,
error
)
Set
(
ctx
context
.
Context
,
key
,
value
string
)
error
GetMultiple
(
ctx
context
.
Context
,
keys
[]
string
)
(
map
[
string
]
string
,
error
)
SetMultiple
(
ctx
context
.
Context
,
settings
map
[
string
]
string
)
error
GetAll
(
ctx
context
.
Context
)
(
map
[
string
]
string
,
error
)
Delete
(
ctx
context
.
Context
,
key
string
)
error
}
backend/internal/service/ports/update_cache.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
)
// UpdateCache defines cache operations for update service
type
UpdateCache
interface
{
GetUpdateInfo
(
ctx
context
.
Context
)
(
string
,
error
)
SetUpdateInfo
(
ctx
context
.
Context
,
data
string
,
ttl
time
.
Duration
)
error
}
backend/internal/service/ports/usage_log.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"time"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/pkg/usagestats"
)
type
UsageLogRepository
interface
{
Create
(
ctx
context
.
Context
,
log
*
model
.
UsageLog
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
UsageLog
,
error
)
Delete
(
ctx
context
.
Context
,
id
int64
)
error
ListByUser
(
ctx
context
.
Context
,
userID
int64
,
params
pagination
.
PaginationParams
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
ListByApiKey
(
ctx
context
.
Context
,
apiKeyID
int64
,
params
pagination
.
PaginationParams
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
ListByAccount
(
ctx
context
.
Context
,
accountID
int64
,
params
pagination
.
PaginationParams
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
ListByUserAndTimeRange
(
ctx
context
.
Context
,
userID
int64
,
startTime
,
endTime
time
.
Time
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
ListByApiKeyAndTimeRange
(
ctx
context
.
Context
,
apiKeyID
int64
,
startTime
,
endTime
time
.
Time
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
ListByAccountAndTimeRange
(
ctx
context
.
Context
,
accountID
int64
,
startTime
,
endTime
time
.
Time
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
ListByModelAndTimeRange
(
ctx
context
.
Context
,
modelName
string
,
startTime
,
endTime
time
.
Time
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
GetAccountWindowStats
(
ctx
context
.
Context
,
accountID
int64
,
startTime
time
.
Time
)
(
*
usagestats
.
AccountStats
,
error
)
GetAccountTodayStats
(
ctx
context
.
Context
,
accountID
int64
)
(
*
usagestats
.
AccountStats
,
error
)
// Admin dashboard stats
GetDashboardStats
(
ctx
context
.
Context
)
(
*
usagestats
.
DashboardStats
,
error
)
GetUsageTrendWithFilters
(
ctx
context
.
Context
,
startTime
,
endTime
time
.
Time
,
granularity
string
,
userID
,
apiKeyID
int64
)
([]
usagestats
.
TrendDataPoint
,
error
)
GetModelStatsWithFilters
(
ctx
context
.
Context
,
startTime
,
endTime
time
.
Time
,
userID
,
apiKeyID
,
accountID
int64
)
([]
usagestats
.
ModelStat
,
error
)
GetApiKeyUsageTrend
(
ctx
context
.
Context
,
startTime
,
endTime
time
.
Time
,
granularity
string
,
limit
int
)
([]
usagestats
.
ApiKeyUsageTrendPoint
,
error
)
GetUserUsageTrend
(
ctx
context
.
Context
,
startTime
,
endTime
time
.
Time
,
granularity
string
,
limit
int
)
([]
usagestats
.
UserUsageTrendPoint
,
error
)
GetBatchUserUsageStats
(
ctx
context
.
Context
,
userIDs
[]
int64
)
(
map
[
int64
]
*
usagestats
.
BatchUserUsageStats
,
error
)
GetBatchApiKeyUsageStats
(
ctx
context
.
Context
,
apiKeyIDs
[]
int64
)
(
map
[
int64
]
*
usagestats
.
BatchApiKeyUsageStats
,
error
)
// User dashboard stats
GetUserDashboardStats
(
ctx
context
.
Context
,
userID
int64
)
(
*
usagestats
.
UserDashboardStats
,
error
)
GetUserUsageTrendByUserID
(
ctx
context
.
Context
,
userID
int64
,
startTime
,
endTime
time
.
Time
,
granularity
string
)
([]
usagestats
.
TrendDataPoint
,
error
)
GetUserModelStats
(
ctx
context
.
Context
,
userID
int64
,
startTime
,
endTime
time
.
Time
)
([]
usagestats
.
ModelStat
,
error
)
// Admin usage listing/stats
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
filters
usagestats
.
UsageLogFilters
)
([]
model
.
UsageLog
,
*
pagination
.
PaginationResult
,
error
)
GetGlobalStats
(
ctx
context
.
Context
,
startTime
,
endTime
time
.
Time
)
(
*
usagestats
.
UsageStats
,
error
)
// Account stats
GetAccountUsageStats
(
ctx
context
.
Context
,
accountID
int64
,
startTime
,
endTime
time
.
Time
)
(
*
usagestats
.
AccountUsageStatsResponse
,
error
)
}
backend/internal/service/ports/user.go
deleted
100644 → 0
View file @
f57f12c6
package
ports
import
(
"context"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
)
type
UserRepository
interface
{
Create
(
ctx
context
.
Context
,
user
*
model
.
User
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
User
,
error
)
GetByEmail
(
ctx
context
.
Context
,
email
string
)
(
*
model
.
User
,
error
)
GetFirstAdmin
(
ctx
context
.
Context
)
(
*
model
.
User
,
error
)
Update
(
ctx
context
.
Context
,
user
*
model
.
User
)
error
Delete
(
ctx
context
.
Context
,
id
int64
)
error
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
model
.
User
,
*
pagination
.
PaginationResult
,
error
)
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
status
,
role
,
search
string
)
([]
model
.
User
,
*
pagination
.
PaginationResult
,
error
)
UpdateBalance
(
ctx
context
.
Context
,
id
int64
,
amount
float64
)
error
DeductBalance
(
ctx
context
.
Context
,
id
int64
,
amount
float64
)
error
UpdateConcurrency
(
ctx
context
.
Context
,
id
int64
,
amount
int
)
error
ExistsByEmail
(
ctx
context
.
Context
,
email
string
)
(
bool
,
error
)
RemoveGroupFromAllowedGroups
(
ctx
context
.
Context
,
groupID
int64
)
(
int64
,
error
)
}
backend/internal/service/proxy_service.go
View file @
f51ad2e1
...
...
@@ -4,10 +4,9 @@ import (
"context"
"errors"
"fmt"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/service/ports"
"gorm.io/gorm"
)
...
...
@@ -15,6 +14,21 @@ var (
ErrProxyNotFound
=
errors
.
New
(
"proxy not found"
)
)
type
ProxyRepository
interface
{
Create
(
ctx
context
.
Context
,
proxy
*
model
.
Proxy
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
Proxy
,
error
)
Update
(
ctx
context
.
Context
,
proxy
*
model
.
Proxy
)
error
Delete
(
ctx
context
.
Context
,
id
int64
)
error
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
model
.
Proxy
,
*
pagination
.
PaginationResult
,
error
)
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
protocol
,
status
,
search
string
)
([]
model
.
Proxy
,
*
pagination
.
PaginationResult
,
error
)
ListActive
(
ctx
context
.
Context
)
([]
model
.
Proxy
,
error
)
ListActiveWithAccountCount
(
ctx
context
.
Context
)
([]
model
.
ProxyWithAccountCount
,
error
)
ExistsByHostPortAuth
(
ctx
context
.
Context
,
host
string
,
port
int
,
username
,
password
string
)
(
bool
,
error
)
CountAccountsByProxyID
(
ctx
context
.
Context
,
proxyID
int64
)
(
int64
,
error
)
}
// CreateProxyRequest 创建代理请求
type
CreateProxyRequest
struct
{
Name
string
`json:"name"`
...
...
@@ -38,11 +52,11 @@ type UpdateProxyRequest struct {
// ProxyService 代理管理服务
type
ProxyService
struct
{
proxyRepo
ports
.
ProxyRepository
proxyRepo
ProxyRepository
}
// NewProxyService 创建代理服务实例
func
NewProxyService
(
proxyRepo
ports
.
ProxyRepository
)
*
ProxyService
{
func
NewProxyService
(
proxyRepo
ProxyRepository
)
*
ProxyService
{
return
&
ProxyService
{
proxyRepo
:
proxyRepo
,
}
...
...
backend/internal/service/ratelimit_service.go
View file @
f51ad2e1
...
...
@@ -9,17 +9,16 @@ import (
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/service/ports"
)
// RateLimitService 处理限流和过载状态管理
type
RateLimitService
struct
{
accountRepo
ports
.
AccountRepository
accountRepo
AccountRepository
cfg
*
config
.
Config
}
// NewRateLimitService 创建RateLimitService实例
func
NewRateLimitService
(
accountRepo
ports
.
AccountRepository
,
cfg
*
config
.
Config
)
*
RateLimitService
{
func
NewRateLimitService
(
accountRepo
AccountRepository
,
cfg
*
config
.
Config
)
*
RateLimitService
{
return
&
RateLimitService
{
accountRepo
:
accountRepo
,
cfg
:
cfg
,
...
...
backend/internal/service/redeem_service.go
View file @
f51ad2e1
...
...
@@ -6,12 +6,11 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/service/ports"
"strings"
"time"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
...
...
@@ -31,6 +30,29 @@ const (
redeemLockDuration
=
10
*
time
.
Second
// 锁超时时间,防止死锁
)
// RedeemCache defines cache operations for redeem service
type
RedeemCache
interface
{
GetRedeemAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
(
int
,
error
)
IncrementRedeemAttemptCount
(
ctx
context
.
Context
,
userID
int64
)
error
AcquireRedeemLock
(
ctx
context
.
Context
,
code
string
,
ttl
time
.
Duration
)
(
bool
,
error
)
ReleaseRedeemLock
(
ctx
context
.
Context
,
code
string
)
error
}
type
RedeemCodeRepository
interface
{
Create
(
ctx
context
.
Context
,
code
*
model
.
RedeemCode
)
error
CreateBatch
(
ctx
context
.
Context
,
codes
[]
model
.
RedeemCode
)
error
GetByID
(
ctx
context
.
Context
,
id
int64
)
(
*
model
.
RedeemCode
,
error
)
GetByCode
(
ctx
context
.
Context
,
code
string
)
(
*
model
.
RedeemCode
,
error
)
Update
(
ctx
context
.
Context
,
code
*
model
.
RedeemCode
)
error
Delete
(
ctx
context
.
Context
,
id
int64
)
error
Use
(
ctx
context
.
Context
,
id
,
userID
int64
)
error
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
model
.
RedeemCode
,
*
pagination
.
PaginationResult
,
error
)
ListWithFilters
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
,
codeType
,
status
,
search
string
)
([]
model
.
RedeemCode
,
*
pagination
.
PaginationResult
,
error
)
ListByUser
(
ctx
context
.
Context
,
userID
int64
,
limit
int
)
([]
model
.
RedeemCode
,
error
)
}
// GenerateCodesRequest 生成兑换码请求
type
GenerateCodesRequest
struct
{
Count
int
`json:"count"`
...
...
@@ -48,19 +70,19 @@ type RedeemCodeResponse struct {
// RedeemService 兑换码服务
type
RedeemService
struct
{
redeemRepo
ports
.
RedeemCodeRepository
userRepo
ports
.
UserRepository
redeemRepo
RedeemCodeRepository
userRepo
UserRepository
subscriptionService
*
SubscriptionService
cache
ports
.
RedeemCache
cache
RedeemCache
billingCacheService
*
BillingCacheService
}
// NewRedeemService 创建兑换码服务实例
func
NewRedeemService
(
redeemRepo
ports
.
RedeemCodeRepository
,
userRepo
ports
.
UserRepository
,
redeemRepo
RedeemCodeRepository
,
userRepo
UserRepository
,
subscriptionService
*
SubscriptionService
,
cache
ports
.
RedeemCache
,
cache
RedeemCache
,
billingCacheService
*
BillingCacheService
,
)
*
RedeemService
{
return
&
RedeemService
{
...
...
backend/internal/service/setting_service.go
View file @
f51ad2e1
...
...
@@ -6,10 +6,10 @@ import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/service/ports"
"strconv"
"gorm.io/gorm"
)
...
...
@@ -18,14 +18,24 @@ var (
ErrRegistrationDisabled
=
errors
.
New
(
"registration is currently disabled"
)
)
type
SettingRepository
interface
{
Get
(
ctx
context
.
Context
,
key
string
)
(
*
model
.
Setting
,
error
)
GetValue
(
ctx
context
.
Context
,
key
string
)
(
string
,
error
)
Set
(
ctx
context
.
Context
,
key
,
value
string
)
error
GetMultiple
(
ctx
context
.
Context
,
keys
[]
string
)
(
map
[
string
]
string
,
error
)
SetMultiple
(
ctx
context
.
Context
,
settings
map
[
string
]
string
)
error
GetAll
(
ctx
context
.
Context
)
(
map
[
string
]
string
,
error
)
Delete
(
ctx
context
.
Context
,
key
string
)
error
}
// SettingService 系统设置服务
type
SettingService
struct
{
settingRepo
ports
.
SettingRepository
settingRepo
SettingRepository
cfg
*
config
.
Config
}
// NewSettingService 创建系统设置服务实例
func
NewSettingService
(
settingRepo
ports
.
SettingRepository
,
cfg
*
config
.
Config
)
*
SettingService
{
func
NewSettingService
(
settingRepo
SettingRepository
,
cfg
*
config
.
Config
)
*
SettingService
{
return
&
SettingService
{
settingRepo
:
settingRepo
,
cfg
:
cfg
,
...
...
backend/internal/service/subscription_service.go
View file @
f51ad2e1
...
...
@@ -9,7 +9,6 @@ import (
"github.com/Wei-Shaw/sub2api/internal/model"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/service/ports"
)
var
(
...
...
@@ -25,13 +24,13 @@ var (
// SubscriptionService 订阅服务
type
SubscriptionService
struct
{
groupRepo
ports
.
GroupRepository
userSubRepo
ports
.
UserSubscriptionRepository
groupRepo
GroupRepository
userSubRepo
UserSubscriptionRepository
billingCacheService
*
BillingCacheService
}
// NewSubscriptionService 创建订阅服务
func
NewSubscriptionService
(
groupRepo
ports
.
GroupRepository
,
userSubRepo
ports
.
UserSubscriptionRepository
,
billingCacheService
*
BillingCacheService
)
*
SubscriptionService
{
func
NewSubscriptionService
(
groupRepo
GroupRepository
,
userSubRepo
UserSubscriptionRepository
,
billingCacheService
*
BillingCacheService
)
*
SubscriptionService
{
return
&
SubscriptionService
{
groupRepo
:
groupRepo
,
userSubRepo
:
userSubRepo
,
...
...
Prev
1
2
3
4
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment