# NanoMQ Configuration 0.14.0

# #============================================================
# # NanoMQ Broker
# #============================================================

system {
	daemon = false
	num_taskq_thread = 0
	max_taskq_thread = 0
	parallel = 0
}

mqtt.session {
	property_size = 32
	max_packet_size = 1024
	client_max_packet_size = 1024
	msq_len = 2048
	qos_duration = 10s
	keepalive_backoff = 1250
	allow_anonymous = true
}

listeners.tcp {
	enable = true
	bind = "0.0.0.0:1883"
}

listeners.ssl {
	enable = false
	bind = "0.0.0.0:8883"
	keyfile = "/etc/certs/key.pem"
	certfile = "/etc/certs/cert.pem"
	cacertfile = "/etc/certs/cacert.pem"
	verify_peer = false
	fail_if_no_peer_cert = false
}

listeners.ws {
	enable = true
	bind = "0.0.0.0:8083/mqtt"
}

http_server {
	enable = false
	port = 8081
	parallel = 2
	username = admin
	password = public
	auth_type = basic
	jwt {
		public.keyfile = "/etc/certs/jwt/jwtRS256.key.pub"
		private.keyfile = "/etc/certs/jwt/jwtRS256.key"
	}
}


log {
	to = [file, console]
	level = warn
	dir = "/tmp"
	file = "nanomq.log"
	rotation {
		size = 10MB
		count = 5
	}
}


sqlite {
	enable = false,
	disk_cache_size = 102400,
	# mounted_file_path="/tmp/"
	flush_mem_threshold = 100
	resend_interval = 5000
}


webhook {
	enable = false
	url = "http://127.0.0.1:80"
	headers.content-type = "application/json"
	body.encoding = plain
	pool_size = 32
}

webhook.rule.client.connect = [{action: on_client_connect}]
webhook.rule.client.connack = [{action: on_client_connack}]
webhook.rule.client.connected = [{action: on_client_connected}]
webhook.rule.client.disconnected = [{action: on_client_disconnected}]
webhook.rule.client.subscribe = [{action: on_client_subscribe}]
webhook.rule.client.unsubscribe = [{action: on_client_unsubscribe}]
webhook.rule.session.subscribed = [{action: on_session_subscribed}]
webhook.rule.session.unsubscribed = [{action: on_session_unsubscribed}]
webhook.rule.session.terminated = [{action: on_session_terminated}]
webhook.rule.message.publish = [{action: on_message_publish}]
webhook.rule.message.delivered = [{action: on_message_delivered}]
webhook.rule.message.acked = [{action: on_message_acked}]


authorization {
	no_match = allow
	deny_action = ignore

	cache = {
		enable = false
		max_size = 32
		ttl = 1m
	}

	sources = [
		{
			type = simple
			enable = false

			users = [
				{
					username = "admin"
					password = "public"
				}
				{
					username = "client"
					password = "public"
				}
			]
		},
		{
			type = file
			enable = false

			rules = [
				## Allow MQTT client using username "dashboard"  to subscribe to "$SYS/#" topics
				{"permit": "allow", "username": "dashboard", "action": "subscribe", "topics": ["$SYS/#"]}

				## Deny "All Users" subscribe to "$SYS/#" "#" Topics
				{"permit": "deny", "username": "#", "action": "subscribe", "topics": ["$SYS/#", "#"]}

				## Allow any other publish/subscribe operation
				{"permit": "allow"}
			]
		},
		{
			type = http
			enable = false
			auth_req {
				url = "http://127.0.0.1:80/mqtt/auth"
				method = post
				
				headers.content-type = "application/x-www-form-urlencoded"
			
				params = {clientid = "%c", username = "%u", password = "%p"}
			}
			
			super_req {
				url = "http://127.0.0.1:80/mqtt/superuser"
				method = "post"
				headers.content-type = "application/x-www-form-urlencoded"
				params = {clientid = "%c", username = "%u", password = "%p"}
			}
			
			acl_req {
				url = "http://127.0.0.1:8991/mqtt/acl"
				method = "post"
				headers.content-type = "application/x-www-form-urlencoded"
				params = {clientid = "%c", username = "%u", access = "%A", ipaddr = "%a", topic = "%t", mountpoint = "%m"}
			}
			
			timeout = 5s
			connect_timeout = 5s
			pool_size = 32	
		}
	]
}

bridges.mqtt {
	nodes = [
		{
			name = emqx
			enable = false
			connector {
				server = "mqtt-tcp://localhost:1883"
				proto_ver = 4
				username = user
				password = pwd
				clean_start = true
				keepalive = 60s
				ssl {
					enable = false
					keyfile = "/etc/certs/key.pem"
					certfile = "/etc/certs/cert.pem"
					cacertfile = "/etc/certs/cacert.pem"
				}
			}
			forwards = ["topic1/#", "topic2/#"]
			quic_keepalive = 120s
			quic_idle_timeout = 120s
			quic_discon_timeout = 20s
			quic_handshake_timeout = 60s
			hybrid_bridging = false
			congestion_control = cubic
			subscription = [
				{
					topic = "cmd/topic1"
					qos = 1
				},
				{
					topic = "cmd/topic2"
					qos = 2
				}
			]
            parallel = 2
            max_send_queue_len = 1024
            max_recv_queue_len = 1024
		}

	]
	sqlite {
		enable = false
		disk_cache_size = 102400
		mounted_file_path = "/tmp/"
		flush_mem_threshold = 100
		resend_interval = 5000
	}

}


bridges.aws.nodes = [
	{
		name = aws
		enable = false

		connector {
			server = "localhost:1883"
			proto_ver = 4
			username = user
			password = pwd
			clientid = "aws_bridge_client"
			clean_start = true
			keepalive = 60s
			ssl {
				enable = false
				keyfile = "/etc/certs/key.pem"
				certfile = "/etc/certs/cert.pem"
				cacertfile = "/etc/certs/cacert.pem"
			}
		}
		
		forwards = ["topic1/#", "topic2/#"]
		subscription = [
			{
				topic = "cmd/topic1"
				qos = 1
			},
			{
				topic = "cmd/topic2"
				qos = 2
			}
		]
		parallel = 2
	}
]


rules.option = OFF

rules.sqlite {
	path = "/tmp/sqlite_rule.db"
	enable = false
	rules = [
		{
			enable = true
			sql = "SELECT payload.x.y as y, payload.z as z FROM \"#\" WHERE y > 10 and z != 'str'"
			table = broker
		},
		{
			enable = true
			sql = "SELECT topic, payload FROM \"abc\""
			table = broker1
		},
		{
			enable = true
			sql = "SELECT * FROM \"#\" "
			table = broker2
		}
	]
}


rules.repub {
	enable = false
	rules = [
		{
			enable = true
			server = "mqtt-tcp://localhost:1883"
			topic = "topic/repub1"
			proto_ver = 4
			clientid = "repub_client1"
			keepalive = 60s
			clean_start = true
			username = username
			password = passwd
			sql = "SELECT payload.x.y as y, payload.z as z FROM \"#\" WHERE y > 10 and z != 'str'"
		},
		{
			enable = true
			server = "mqtt-tcp://localhost:1883"
			topic = "topic/repub2"
			proto_ver = 4
			clientid = "repub_client2"
			keepalive = 60s
			clean_start = true
			username = username
			password = passwd
			sql = "SELECT topic, payload FROM \"abc\""
		}
	]
}

rules.mysql {
	name = "mysql_rule_db"
	enable = false
	rules = [
		{
			enable = true
			table = broker
			host = localhost
			username = username
			password = password
			sql = "SELECT payload.x.y as y, payload.z as z FROM \"#\" WHERE y > 10 and z != 'str'"
		},
		{
			enable = true
			table = broker1
			host = localhost
			username = username
			password = password
			sql = "SELECT * FROM \"abc\""
		},
		{
			enable = true
			table = broker2
			host = localhost
			username = username
			password = password
			sql = "SELECT payload, qos FROM \"#\" WHERE qos > 0"
		}
	]
}
