Skip to content

Commit 9795bd3

Browse files
committed
add session expiration days to config
1 parent 9de41bf commit 9795bd3

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

internal/config/config.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ var (
1313
)
1414

1515
type Config struct {
16-
Port string // flag and env
17-
IsDev bool // flag
18-
DBHost string // env
19-
DBPort string // env
20-
DBName string // env
21-
DBUser string // env
22-
DBPassword string // env
23-
SecretKey string // env
16+
Port string // flag and env
17+
IsDev bool // flag
18+
DBHost string // env
19+
DBPort string // env
20+
DBName string // env
21+
DBUser string // env
22+
DBPassword string // env
23+
SecretKey string // env
24+
SessionExpirationDays string // env
2425
}
2526

2627
func (c *Config) GetDataSourceName() string {
@@ -40,14 +41,15 @@ func NewConfig() *Config {
4041
flag.BoolVar(&isDev, "dev", false, "enable development mode")
4142
flag.Parse()
4243
appConfig = &Config{
43-
Port: getEnv("PORT", "8081"),
44-
IsDev: isDev,
45-
DBHost: getEnv("MY_DB_HOST", "localhost"),
46-
DBPort: getEnv("MY_DB_PORT", "5433"),
47-
DBName: getEnv("MY_DB_NAME", "sandbox"),
48-
DBUser: getEnv("MY_DB_USER", "postgres"),
49-
DBPassword: getEnv("MY_DB_PASSWORD", "postgres"),
50-
SecretKey: getEnv("MY_SECRET_KEY", "12345678901234567890123456789012"),
44+
Port: getEnv("PORT", "8081"),
45+
IsDev: isDev,
46+
DBHost: getEnv("MY_DB_HOST", "localhost"),
47+
DBPort: getEnv("MY_DB_PORT", "5433"),
48+
DBName: getEnv("MY_DB_NAME", "sandbox"),
49+
DBUser: getEnv("MY_DB_USER", "postgres"),
50+
DBPassword: getEnv("MY_DB_PASSWORD", "postgres"),
51+
SecretKey: getEnv("MY_SECRET_KEY", "12345678901234567890123456789012"),
52+
SessionExpirationDays: getEnv("MY_SESSION_EXPIRATION_DAYS", "90"),
5153
}
5254

5355
if isDev {

internal/handlers/session.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"sandbox-go-api-sqlboiler-rest-auth/internal/middleware"
66
"sandbox-go-api-sqlboiler-rest-auth/models"
7+
"strconv"
78
"time"
89

910
"github.com/google/uuid"
@@ -40,11 +41,13 @@ func CreateSession(c echo.Context) error {
4041
return echo.NewHTTPError(http.StatusBadRequest)
4142
}
4243

44+
expirationDays := strconv.Atoi(cc.Config.SessionExpirationDays)
45+
4346
var s models.Session
4447
var uid, _ = uuid.NewUUID()
4548
s.ID = uid.String()
4649
s.UserID = u.ID
47-
s.ExpiresAt = time.Now().Add(time.Hour * 24 * 30)
50+
s.ExpiresAt = time.Now().Add(time.Hour * 24 * time.Duration(expirationDays))
4851
err = s.Insert(ctx, cc.DB, boil.Infer())
4952
if err != nil {
5053
return echo.NewHTTPError(http.StatusInternalServerError)
@@ -54,11 +57,12 @@ func CreateSession(c echo.Context) error {
5457
if err != nil {
5558
return echo.NewHTTPError(http.StatusInternalServerError)
5659
}
60+
5761
cookie := &http.Cookie{
5862
Name: "session",
5963
Value: encoded,
6064
Path: "/",
61-
Expires: time.Now().Add(time.Hour * 24 * 30),
65+
Expires: time.Now().Add(time.Hour * 24 * time.Duration(expirationDays)),
6266
Secure: !cc.Config.IsDev,
6367
HttpOnly: true,
6468
SameSite: 2,

0 commit comments

Comments
 (0)