credger/credger.cr

64 lines
1.6 KiB
Crystal
Raw Normal View History

2017-11-24 10:58:58 +01:00
require "kemal"
require "./ledger"
2019-02-14 11:59:31 +01:00
ENV["CREDGER_CURRENCY"] ||= ""
ENV["CREDGER_SEPARATOR"] ||= ","
2021-05-12 16:58:10 +02:00
ENV["CREDGER_PORT"] ||= "3000"
2021-05-12 17:27:25 +02:00
ENV["CREDGER_VERBOSE"] ||= "false"
2017-11-24 15:32:19 +01:00
WD = File.dirname( Process.executable_path.to_s )
public_folder( "#{WD}/public" )
2021-05-12 17:27:25 +02:00
logging ENV["CREDGER_VERBOSE"] == "true"
2017-11-24 15:32:19 +01:00
2017-11-24 10:58:58 +01:00
# Matches GET "http://host:port/"
get "/" do |env|
env.response.content_type = "text/html"
2017-11-24 14:58:56 +01:00
send_file( env, "#{WD}/public/angularjs.html" )
2017-11-24 14:58:56 +01:00
end
2021-05-12 16:58:10 +02:00
ledger = Ledger.new
2017-11-24 14:58:56 +01:00
get "/api/ledger/version" do |env|
env.response.content_type = "text"
ledger.version
2017-11-24 10:58:58 +01:00
end
get "/api/ledger/accounts" do |env|
env.response.content_type = "application/json"
2017-11-24 14:58:56 +01:00
2017-11-24 10:58:58 +01:00
ledger.accounts.to_json
end
get "/api/ledger/balance" do |env|
env.response.content_type = "application/json"
2017-11-24 16:12:18 +01:00
ledger.balance( env.params.query.has_key?( "cleared" ) ? env.params.query[ "cleared" ] == "true" : false,
2017-11-24 10:58:58 +01:00
env.params.query[ "depth" ].to_i,
env.params.query[ "period" ],
env.params.query[ "categories" ] )
.to_json
end
get "/api/ledger/graph_values" do |env|
env.response.content_type = "application/json"
2017-11-24 16:12:18 +01:00
ledger.graph_values( env.params.query["period"],
2017-12-15 20:25:37 +01:00
"--#{env.params.query["granularity"]}",
2017-11-24 16:12:18 +01:00
env.params.query["categories"].split(" ") ).to_json
2017-11-24 10:58:58 +01:00
end
2017-11-24 14:58:56 +01:00
get "/api/ledger/register" do |env|
env.response.content_type = "application/json"
{ key: env.params.query[ "categories" ],
values: ledger.register( env.params.query[ "period" ],
env.params.query[ "categories" ].split(" ") ) }
.to_json
2017-11-24 10:58:58 +01:00
end
2021-05-12 16:58:10 +02:00
Kemal.run( ENV["CREDGER_PORT"].to_i )