Requests and responses are in separate files and directories.

This commit is contained in:
Karchnu 2020-12-09 19:00:18 +01:00
parent 7e211cead1
commit 5dd1d13b46
5 changed files with 76 additions and 21 deletions

View File

@ -12,24 +12,5 @@ module DNSManager
class_getter responses = [] of IPC::JSON.class
end
class DNSManager::Response
IPC::JSON.message Error, 0 do
property reason : String | Array(String)
def initialize(@reason)
end
end
IPC::JSON.message Success, 1 do
def initialize
end
end
end
class DNSManager::Request
IPC::JSON.message Login, 0 do
end
IPC::JSON.message Logout, 1 do
end
DNSManager.requests << Logout
end
# require "./requests/*"
require "./requests/*"
require "./responses/*"

32
src/requests/admin.cr Normal file
View File

@ -0,0 +1,32 @@
class DNSManager::Request
# Periodic actions to perform as an administrator.
IPC::JSON.message Maintainance, 7 do
enum Subject
Verbosity # Change the verbosity of dnsmanagerd.
end
property key : String
property subject : Subject
property value : Int32?
def initialize(@key, @subject)
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event::Events)
# This request means serious business.
raise AdminAuthorizationException.new if key != dnsmanagerd.authd.key
case @subject
when Subject::Verbosity
if verbosity = @value
Baguette::Context.verbosity = verbosity
end
Response::Success.new
else
Response::Error.new "not implemented"
end
end
end
DNSManager.requests << Maintainance
end

25
src/requests/login.cr Normal file
View File

@ -0,0 +1,25 @@
class DNSManager::Request
IPC::JSON.message Login, 0 do
property token : String
def initialize(@token)
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event::Events)
user, _ = dnsmanagerd.decode_token token
dnsmanagerd.logged_users[event.fd] = user
# In case we want to log their last connection.
#dnsmanagerd.auth.edit_profile_content user.uid, {
# "dnsmanager-last-connection" => JSON::Any.new Time.utc.to_s
#}
return Response::Success.new
rescue e
# FIXME: Should those be logged?
return Response::Error.new "unauthorized"
end
end
DNSManager.requests << Login
end

9
src/responses/error.cr Normal file
View File

@ -0,0 +1,9 @@
class DNSManager::Response
IPC::JSON.message Error, 0 do
property reason : String | Array(String)
def initialize(@reason)
end
end
DNSManager.responses << Error
end

8
src/responses/success.cr Normal file
View File

@ -0,0 +1,8 @@
class DNSManager::Response
IPC::JSON.message Success, 1 do
def initialize
end
end
DNSManager.responses << Success
end