mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Make session name and expire time configurable
This commit is contained in:
parent
6d4bf575cb
commit
8c600cb26e
4 changed files with 15 additions and 12 deletions
|
@ -15,13 +15,14 @@ module Kemal
|
|||
{% end %}
|
||||
|
||||
property host_binding, ssl, port, env, public_folder, logging,
|
||||
always_rescue, serve_static : (Bool | Hash(String, Bool)), server, extra_options
|
||||
always_rescue, serve_static : (Bool | Hash(String, Bool)), server, session : Hash(String, Time::Span | String), extra_options
|
||||
|
||||
def initialize
|
||||
@host_binding = "0.0.0.0"
|
||||
@port = 3000
|
||||
@env = "development"
|
||||
@serve_static = {"dir_listing" => false, "gzip" => true}
|
||||
@session = {"name" => "kemal_session", "expire_time" => 48.hours}
|
||||
@public_folder = "./public"
|
||||
@logging = true
|
||||
@logger = nil
|
||||
|
|
|
@ -15,15 +15,9 @@ module Kemal
|
|||
#
|
||||
# Sessions are pruned hourly after 48 hours of inactivity.
|
||||
class Sessions
|
||||
NAME = "SessionId"
|
||||
|
||||
# Session Types are String, Integer, Float and Boolean
|
||||
alias SessionTypes = String | Int64 | Float64 | Bool
|
||||
|
||||
# I hate websites which require daily login so the default
|
||||
# inactivity timeout is 48 hours.
|
||||
TTL = 48.hours
|
||||
|
||||
# In-memory, ephemeral datastore only.
|
||||
#
|
||||
# Implementing Redis or Memcached as a datastore
|
||||
|
@ -68,7 +62,7 @@ module Kemal
|
|||
getter! id : String
|
||||
|
||||
def initialize(ctx : HTTP::Server::Context)
|
||||
id = ctx.request.cookies[NAME]?.try &.value
|
||||
id = ctx.request.cookies[Kemal.config.session["name"].as(String)]?.try &.value
|
||||
if id && id.size == 32
|
||||
# valid
|
||||
else
|
||||
|
@ -76,7 +70,7 @@ module Kemal
|
|||
id = SecureRandom.hex
|
||||
end
|
||||
|
||||
ctx.response.cookies << HTTP::Cookie.new(name: NAME, value: id, http_only: true)
|
||||
ctx.response.cookies << HTTP::Cookie.new(name: Kemal.config.session["name"].as(String), value: id, http_only: true)
|
||||
@id = id
|
||||
end
|
||||
|
||||
|
@ -99,7 +93,7 @@ module Kemal
|
|||
STORE[@id]?.try &.delete(key)
|
||||
end
|
||||
|
||||
def self.prune!(before = (Time.now - Kemal::Sessions::TTL).epoch_ms)
|
||||
def self.prune!(before = (Time.now - Kemal.config.session["expire_time"].as(Time::Span)).epoch_ms)
|
||||
Kemal::Sessions::STORE.delete_if { |id, entry| entry.last_access_at < before }
|
||||
nil
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue