starting over

This commit is contained in:
Yessiest 2023-04-30 11:56:21 +04:00
parent 3e656163f9
commit 25647e3e95
3 changed files with 128 additions and 1 deletions

2
hyde

@ -1 +1 @@
Subproject commit c6e89afcfd115680ac4ddd7178f71782e9137402
Subproject commit fd76422b760e6c045581bcb11842ee42221d06e0

83
proto.rb Normal file
View File

@ -0,0 +1,83 @@
UIDS = {}
module Heimdall
class UID
def initialize
@UID_prefix = "abstract" if not @UID_prefix
id = (1..32).map { |x| (rand()*10).floor }.join
while UIDS.has_key? id do
id = (1..32).map { |x| (rand()*10).floor }.join
end
UIDS[@UID_prefix+id] = self
@UID = id
end
attr_reader :UID
end
class UserCache
def initialize
@users = {}
end
def add(user)
@users[user.protoid] = user
end
def get(protoid)
return @users[user.protoid]
end
end
class User < UID
def initialize(username, protoid)
@username = username
@protoid = protoid
@UID_prefix = "user"
super()
end
attr_reader :username
attr_reader :protoid
end
class Server < UID
def initialize
@UID_prefix = "server"
super()
end
end
class Channel < UID
def initialize
@UID_prefix = "channel"
super()
end
end
class DirectChannel < Channel
def initialize
@UID_prefix = "dchannel"
super()
end
end
class ServerChannel < Channel
def initialize
@UID_prefix = "schannel"
super()
end
end
class Message < UID
def initialize
@UID_prefix = "message"
super()
end
end
class MsgStack < UID
def initialize
@UID_prefix = "msgstack"
super()
end
end
end

44
server.rb Normal file
View File

@ -0,0 +1,44 @@
require_relative "proto"
require_relative "hyde/hyde"
require "json"
Users = Heimdall::UserCache.new
def require_keys(dict,key_dict)
raise KeyError, "not a dict" unless dict.kind_of? Hash
key_dict.each_pair { |k,v|
unless (dict.has_key? k) and (dict[k].kind_of? v) then
raise KeyError, "key #{k} of type #{v} required"
end
}
end
server = Hyde::Server.new Port: 8000 do
path "user" do
post "new" do |ctx|
req,res = ctx.request,ctx.response
begin
data = JSON::Parser.new(req.body).parse
require_keys(data,{
username: String,
protocol_id: String
})
Users.add(Heimdall::User.new data[:username], data[:protocol_id])
rescue JSON::ParserError => jsonerror
res.body = JSON::fast_generate({
error: "#{jsonerror}"
})
res['Content-Type'] = "application/json"
res.status = 400
rescue KeyError => keyerror
res.body = JSON::fast_generate({
error: "#{keyerror}"
})
res['Content-Type'] = "application/json"
res.status = 400
end
end
end
end
server.start