2023-03-20 16:40:08 +01:00
|
|
|
package http
|
|
|
|
|
2023-04-05 15:19:22 +02:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetCookieDomainFunc func(r *http.Request) (string, error)
|
|
|
|
|
|
|
|
func defaultGetCookieDomain(r *http.Request) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-03-28 20:37:57 +02:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
type LocalHandlerOptions struct {
|
2023-04-05 15:19:22 +02:00
|
|
|
RoutePrefix string
|
|
|
|
Accounts []LocalAccount
|
|
|
|
GetCookieDomain GetCookieDomainFunc
|
|
|
|
CookieDuration time.Duration
|
2023-03-20 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type LocalHandlerOptionFunc func(*LocalHandlerOptions)
|
|
|
|
|
|
|
|
func defaultLocalHandlerOptions() *LocalHandlerOptions {
|
|
|
|
return &LocalHandlerOptions{
|
2023-04-05 15:19:22 +02:00
|
|
|
RoutePrefix: "",
|
|
|
|
Accounts: make([]LocalAccount, 0),
|
|
|
|
GetCookieDomain: defaultGetCookieDomain,
|
|
|
|
CookieDuration: 24 * time.Hour,
|
2023-03-20 16:40:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAccounts(accounts ...LocalAccount) LocalHandlerOptionFunc {
|
|
|
|
return func(opts *LocalHandlerOptions) {
|
|
|
|
opts.Accounts = accounts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithRoutePrefix(prefix string) LocalHandlerOptionFunc {
|
|
|
|
return func(opts *LocalHandlerOptions) {
|
|
|
|
opts.RoutePrefix = prefix
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 20:37:57 +02:00
|
|
|
|
2023-04-05 15:19:22 +02:00
|
|
|
func WithCookieOptions(getCookieDomain GetCookieDomainFunc, duration time.Duration) LocalHandlerOptionFunc {
|
2023-03-28 20:37:57 +02:00
|
|
|
return func(opts *LocalHandlerOptions) {
|
2023-04-05 15:19:22 +02:00
|
|
|
opts.GetCookieDomain = getCookieDomain
|
2023-03-28 20:37:57 +02:00
|
|
|
opts.CookieDuration = duration
|
|
|
|
}
|
|
|
|
}
|