#!/usr/bin/env tarantool

require("strict").on()

-- Configure path so that you can run application
-- from outside the root directory.
if package.setsearchroot ~= nil then
	package.setsearchroot()
else
	-- Workaround for rocks loading in tarantool 1.10.
	-- It can be removed in tarantool > 2.2.
	-- By default, when you do require('mymodule'), tarantool looks into
	-- the current working directory and whatever is specified in
	-- package.path and package.cpath. If you run your app while in the
	-- root directory of that app, everything goes fine, but if you try to
	-- start your app with "tarantool myapp/init.lua", it will fail to load
	-- its modules, and modules from myapp/.rocks.
	local fio = require("fio")
	local app_dir = fio.abspath(fio.dirname(arg[0]))
	package.path = app_dir .. "/?.lua;" .. package.path
	package.path = app_dir .. "/?/init.lua;" .. package.path
	package.path = app_dir .. "/.rocks/share/tarantool/?.lua;" .. package.path
	package.path = app_dir .. "/.rocks/share/tarantool/?/init.lua;" .. package.path
	package.cpath = app_dir .. "/?.so;" .. package.cpath
	package.cpath = app_dir .. "/?.dylib;" .. package.cpath
	package.cpath = app_dir .. "/.rocks/lib/tarantool/?.so;" .. package.cpath
	package.cpath = app_dir .. "/.rocks/lib/tarantool/?.dylib;" .. package.cpath
end

local has_compat, compat = pcall(require, "compat")
if has_compat then
	compat.fiber_slice_default = "new"
end

-- Configure cartridge.

local cartridge = require("cartridge")

local ok, err = cartridge.cfg({
	roles = {
		"cartridge.roles.vshard-storage",
		"cartridge.roles.vshard-router",
		"cartridge.roles.metrics",
		"app.roles.custom",
		"app.roles.storage",
	},
	cluster_cookie = "admin",
})

assert(ok, tostring(err))

-- Register admin function to use it with 'cartridge admin' command.

local admin = require("app.admin")
admin.init()

local metrics = require("cartridge.roles.metrics")
metrics.set_export({
	{
		path = "/metrics",
		format = "prometheus",
	},
	{
		path = "/health",
		format = "health",
	},
})

-- require("msgpack").cfg({
-- 	encode_invalid_as_nil = true,
-- })
