Skip to content

Commit 285d602

Browse files
committed
feat: add offer, user session expires at
1 parent 5149617 commit 285d602

4 files changed

Lines changed: 47 additions & 38 deletions

File tree

.env.example

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ DB_NAME="name"
77

88
# API
99
PORT=3333
10-
COOKIE_NAME="Authorization"
10+
GO_ENV="local"
1111
SECRET_KEY="SecretKey"
12+
COOKIE_NAME="Authorization"
13+
COOKIE_EXPIRES_AT="1h"
1214
SKIP_AUTH=false
1315

1416
# NATS
@@ -20,4 +22,5 @@ RDB_PASSWORD=""
2022
RDB_DB=0
2123
RATE_LIMIT=10
2224
RATE_LIMIT_WINDOW="1m"
23-
25+
USER_SESSION_EXPIRES_AT="1h"
26+
OFFER_EXPIRES_AT="1h"

config/env.go

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ import (
1010
)
1111

1212
type EnvVariables struct {
13-
DBUsername string
14-
DBPassword string
15-
DBHost string
16-
DBPort string
17-
DBName string
18-
Port string
19-
GoEnv GoEnv
20-
SecretKey string
21-
SkipAuth bool
22-
CookieName string
23-
CookieExpiresAt time.Duration
24-
NatsURL string
25-
RdbAddress string
26-
RdbPassword string
27-
RdbDB int
28-
RateLimit int
29-
RateLimitWindow time.Duration
13+
DBUsername string
14+
DBPassword string
15+
DBHost string
16+
DBPort string
17+
DBName string
18+
Port string
19+
GoEnv GoEnv
20+
SecretKey string
21+
SkipAuth bool
22+
CookieName string
23+
CookieExpiresAt time.Duration
24+
NatsURL string
25+
RdbAddress string
26+
RdbPassword string
27+
RdbDB int
28+
RateLimit int
29+
RateLimitWindow time.Duration
30+
UserSessionExpiresAt time.Duration
31+
OfferExpiresAt time.Duration
3032
}
3133

3234
var Env EnvVariables
@@ -42,24 +44,28 @@ func Init() {
4244
rateLimit, _ := strconv.Atoi(os.Getenv("RATE_LIMIT"))
4345
rateLimitWindow, _ := time.ParseDuration(os.Getenv("RATE_LIMIT_WINDOW"))
4446
cookieExpiresAt, _ := time.ParseDuration(os.Getenv("COOKIE_EXPIRES_AT"))
47+
userSessionExpiresAt, _ := time.ParseDuration(os.Getenv("USER_SESSION_EXPIRES_AT"))
48+
offerExpiresAt, _ := time.ParseDuration(os.Getenv("OFFER_EXPIRES_AT"))
4549

4650
Env = EnvVariables{
47-
DBUsername: os.Getenv("DB_USERNAME"),
48-
DBPassword: os.Getenv("DB_PASSWORD"),
49-
DBHost: os.Getenv("DB_HOST"),
50-
DBPort: os.Getenv("DB_PORT"),
51-
DBName: os.Getenv("DB_NAME"),
52-
Port: os.Getenv("PORT"),
53-
GoEnv: GoEnv(os.Getenv("GO_ENV")),
54-
SecretKey: os.Getenv("SECRET_KEY"),
55-
SkipAuth: skipAuth,
56-
CookieName: os.Getenv("COOKIE_NAME"),
57-
CookieExpiresAt: cookieExpiresAt,
58-
NatsURL: os.Getenv("NATS_URL"),
59-
RdbAddress: os.Getenv("RDB_ADDRESS"),
60-
RdbPassword: os.Getenv("RDB_PASSWORD"),
61-
RdbDB: rdbDb,
62-
RateLimit: rateLimit,
63-
RateLimitWindow: rateLimitWindow,
51+
DBUsername: os.Getenv("DB_USERNAME"),
52+
DBPassword: os.Getenv("DB_PASSWORD"),
53+
DBHost: os.Getenv("DB_HOST"),
54+
DBPort: os.Getenv("DB_PORT"),
55+
DBName: os.Getenv("DB_NAME"),
56+
Port: os.Getenv("PORT"),
57+
GoEnv: GoEnv(os.Getenv("GO_ENV")),
58+
SecretKey: os.Getenv("SECRET_KEY"),
59+
SkipAuth: skipAuth,
60+
CookieName: os.Getenv("COOKIE_NAME"),
61+
CookieExpiresAt: cookieExpiresAt,
62+
NatsURL: os.Getenv("NATS_URL"),
63+
RdbAddress: os.Getenv("RDB_ADDRESS"),
64+
RdbPassword: os.Getenv("RDB_PASSWORD"),
65+
RdbDB: rdbDb,
66+
RateLimit: rateLimit,
67+
RateLimitWindow: rateLimitWindow,
68+
UserSessionExpiresAt: userSessionExpiresAt,
69+
OfferExpiresAt: offerExpiresAt,
6470
}
6571
}

internal/infra/nats/consumer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (o *OffersConsumer) HandlingAccptedOffers() {
145145
offer.Product.SetOfferPrice(offer.Price)
146146
offerProductJson, _ := json.Marshal(offer.Product)
147147

148-
go redis.Redis.Set(context.Background(), "offer-"+offer.Buyer.ID+"-"+offer.Product.ID, string(offerProductJson), 0)
148+
go redis.Redis.Set(context.Background(), "offer-"+offer.Buyer.ID+"-"+offer.Product.ID, string(offerProductJson), config.Env.OfferExpiresAt)
149149

150150
config.Logger.Info(
151151
"order status updated to accepted",

internal/usecase/auth/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (a *AuthUseCase) Login(input AuthInputDto) (string, *entity.User, error) {
5959
return "", nil, err
6060
}
6161

62-
infra.Redis.Set(context.Background(), "user-"+output.ID, string(userJson), 0)
62+
infra.Redis.Set(context.Background(), "user-"+output.ID, string(userJson), config.Env.UserSessionExpiresAt)
6363
return token, output, nil
6464
}
6565

0 commit comments

Comments
 (0)