Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags and description to vhost #689

Merged
merged 12 commits into from
Jun 18, 2024
5 changes: 4 additions & 1 deletion src/lavinmq/http/controller/vhosts.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ module LavinMQ
u = user(context)
refuse_unless_administrator(context, u)
name = URI.decode_www_form(params["name"])
body = parse_body(context)
tags = body["tags"]?.to_s.split(',').map(&.strip).reject(&.empty?)
description = body["description"]?.to_s
is_update = @amqp_server.vhosts[name]?
if name.bytesize > UInt8::MAX
bad_request(context, "Vhost name too long, can't exceed 255 characters")
end
@amqp_server.vhosts.create(name, u)
@amqp_server.vhosts.create(name, u, description, tags)
context.response.status_code = is_update ? 204 : 201
context
end
Expand Down
4 changes: 3 additions & 1 deletion src/lavinmq/vhost.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module LavinMQ
@definitions_file_path : String
@definitions_deletes = 0

def initialize(@name : String, @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator)
def initialize(@name : String, @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator, @description = "", @tags = Array(String).new(0))
@log = Log.for "vhost[name=#{@name}]"
@dir = Digest::SHA1.hexdigest(@name)
@data_dir = File.join(@server_data_dir, @dir)
Expand Down Expand Up @@ -212,6 +212,8 @@ module LavinMQ
name: @name,
dir: @dir,
tracing: false,
tags: @tags,
description: @description,
cluster_state: NamedTuple.new,
}
end
Expand Down
8 changes: 5 additions & 3 deletions src/lavinmq/vhost_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ module LavinMQ
end
end

def create(name : String, user : User = @users.default_user, save : Bool = true)
def create(name : String, user : User = @users.default_user, description = "", tags = Array(String).new(0), save : Bool = true)
if v = @vhosts[name]?
return v
end
vhost = VHost.new(name, @data_dir, @users, @replicator)
vhost = VHost.new(name, @data_dir, @users, @replicator, description, tags)
Log.info { "Created vhost #{name}" }
@users.add_permission(user.name, name, /.*/, /.*/, /.*/)
@users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/)
Expand Down Expand Up @@ -61,7 +61,9 @@ module LavinMQ
File.open(path) do |f|
JSON.parse(f).as_a.each do |vhost|
name = vhost["name"].as_s
@vhosts[name] = VHost.new(name, @data_dir, @users, @replicator)
tags = vhost["tags"]?.try(&.as_a.map(&.to_s)) || [] of String
description = vhost["description"]?.try &.as_s || ""
@vhosts[name] = VHost.new(name, @data_dir, @users, @replicator, description, tags)
@users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/)
end
@replicator.register_file(f)
Expand Down