Skip to content

Commit

Permalink
Add tags and description to vhost (#689)
Browse files Browse the repository at this point in the history
* add tags and description to vhost
---------
Co-authored-by: Carl Hörberg <[email protected]>
  • Loading branch information
kickster97 authored Jun 18, 2024
1 parent c8f6757 commit 878b9f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
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 : Clustering::Replicator)
def initialize(@name : String, @server_data_dir : String, @users : UserStore, @replicator : Clustering::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

0 comments on commit 878b9f4

Please sign in to comment.