-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea68a6
commit 354d8f0
Showing
8 changed files
with
145 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,66 @@ | ||
module LavinMQ | ||
class CacheEntry(T) | ||
getter value : T | ||
getter expires_at : Time | ||
# module LavinMQ | ||
# class CacheEntry(T) | ||
# getter value : T | ||
# getter expires_at : Time | ||
|
||
def initialize(@value : T, ttl : Time::Span) | ||
@expires_at = Time.utc + ttl | ||
end | ||
# def initialize(@value : T, ttl : Time::Span) | ||
# @expires_at = Time.utc + ttl | ||
# end | ||
|
||
def expired? : Bool | ||
Time.utc > @expires_at | ||
end | ||
end | ||
# def expired? : Bool | ||
# Time.utc > @expires_at | ||
# end | ||
# end | ||
|
||
class Cache(K, V) | ||
def initialize(@default_ttl : Time::Span = 1.hour) | ||
@mutex = Mutex.new | ||
@data = Hash(K, CacheEntry(V)).new | ||
end | ||
# class Cache(K, V) | ||
# def initialize(@default_ttl : Time::Span = 1.hour) | ||
# @mutex = Mutex.new | ||
# @data = Hash(K, CacheEntry(V)).new | ||
# end | ||
|
||
def set(key : K, value : V, ttl : Time::Span = @default_ttl) : V | ||
@mutex.synchronize do | ||
@data[key] = CacheEntry.new(value, ttl) | ||
value | ||
end | ||
end | ||
# def set(key : K, value : V, ttl : Time::Span = @default_ttl) : V | ||
# @mutex.synchronize do | ||
# @data[key] = CacheEntry.new(value, ttl) | ||
# value | ||
# end | ||
# end | ||
|
||
def get?(key : K) : V? | ||
@mutex.synchronize do | ||
entry = @data[key]? | ||
return nil unless entry | ||
# def get?(key : K) : V? | ||
# @mutex.synchronize do | ||
# entry = @data[key]? | ||
# return nil unless entry | ||
|
||
if entry.expired? | ||
@data.delete(key) | ||
nil | ||
else | ||
entry.value | ||
end | ||
end | ||
end | ||
# if entry.expired? | ||
# @data.delete(key) | ||
# nil | ||
# else | ||
# entry.value | ||
# end | ||
# end | ||
# end | ||
|
||
def delete(key : K) : Bool | ||
@mutex.synchronize do | ||
@data.delete(key) ? true : false | ||
end | ||
end | ||
# def delete(key : K) : Bool | ||
# @mutex.synchronize do | ||
# @data.delete(key) ? true : false | ||
# end | ||
# end | ||
|
||
def cleanup | ||
@mutex.synchronize do | ||
@data.reject! { |_, entry| entry.expired? } | ||
end | ||
end | ||
# def cleanup | ||
# @mutex.synchronize do | ||
# @data.reject! { |_, entry| entry.expired? } | ||
# end | ||
# end | ||
|
||
def clear | ||
@mutex.synchronize do | ||
@data.clear | ||
end | ||
end | ||
# def clear | ||
# @mutex.synchronize do | ||
# @data.clear | ||
# end | ||
# end | ||
|
||
def size : Int32 | ||
@mutex.synchronize do | ||
@data.size | ||
end | ||
end | ||
end | ||
end | ||
# def size : Int32 | ||
# @mutex.synchronize do | ||
# @data.size | ||
# end | ||
# end | ||
# end | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module LavinMQ | ||
class AuthChain | ||
@first_handler : AuthHandler? | ||
|
||
def initialize | ||
backends = Config.instance.auth_backends | ||
if backends.empty? | ||
add_handler(BasicAuthHandler.new) | ||
else | ||
# need to add initializers to these in order to only send in username & password in auth | ||
backends.each do |backend| | ||
case backend | ||
when "oauth" | ||
add_handler(OAuth2Handler.new) | ||
when "http" | ||
add_handler(HTTPAuthHandler.new) | ||
when "basic" | ||
add_handler(BasicAuthHandler.new) | ||
else | ||
raise "Unsupported authentication backend: #{backend}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
def add_handler(handler : AuthHandler) | ||
pp "Adding handler #{handler}" | ||
if first = @first_handler | ||
current = first | ||
while next_handler = current.@successor | ||
current = next_handler | ||
end | ||
current.then(handler) | ||
else | ||
@first_handler = handler | ||
end | ||
self | ||
end | ||
|
||
def authenticate(username : String, password : String) | ||
pp "hello #{username} #{password}" | ||
pp @first_handler | ||
|
||
@first_handler.try &.authenticate(username, password) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters