2024-04-12 16:41:11 +02:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Session sessions.Options
|
|
|
|
KeyPrefix string
|
2024-11-14 10:09:33 +01:00
|
|
|
TTL time.Duration
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptionFunc func(opts *Options)
|
|
|
|
|
|
|
|
func NewOptions(funcs ...OptionFunc) *Options {
|
|
|
|
opts := &Options{
|
|
|
|
Session: sessions.Options{
|
|
|
|
Path: "/",
|
|
|
|
Domain: "",
|
|
|
|
MaxAge: int(time.Hour.Seconds()),
|
|
|
|
HttpOnly: true,
|
|
|
|
Secure: false,
|
|
|
|
SameSite: http.SameSiteDefaultMode,
|
|
|
|
},
|
|
|
|
KeyPrefix: "session:",
|
2024-11-14 10:09:33 +01:00
|
|
|
TTL: time.Hour,
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSessionOptions(options sessions.Options) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Session = options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithKeyPrefix(prefix string) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.KeyPrefix = prefix
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 10:09:33 +01:00
|
|
|
|
|
|
|
func WithTTL(ttl time.Duration) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.TTL = ttl
|
|
|
|
}
|
|
|
|
}
|