From 81da30fb0168c055116361087ea1f8e2f71eb439 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Tue, 5 Nov 2019 22:54:03 +0300 Subject: [PATCH] Add web routes --- src/dayoff.cr | 38 +++++++++++++++++++++++++++++--------- src/dayoff/app.cr | 15 +++++++++++++++ src/dayoff/entities.cr | 8 ++++++++ src/dayoff/profile.cr | 2 -- src/handlers.cr | 32 ++++++++++++++++++++++++++++++++ tools/server | 1 + 6 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/dayoff/app.cr create mode 100644 src/handlers.cr diff --git a/src/dayoff.cr b/src/dayoff.cr index 2b43e0d..327a5be 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -1,18 +1,38 @@ require "kemal" require "./dayoff/**" +require "./handlers" -module Dayoff - VERSION = "0.1.0" +base_path = "./tmp" - class App - def profile(profile_id : ProfileId) - Profile.new(profile_id.to_s) - end - end +app = Dayoff::App.new base_path + +add_handler CheckProfileHandler.new(app) + +def now + Time.local(Time::Location.load("Europe/Moscow")) end -get "/" do - "Hello World!" +post "/api/start" do |env| + profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s) + profile.start now + env.response.status_code = 201 +end + +post "/api/finish" do |env| + profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s) + profile.finish now + env.response.status_code = 204 +end + +get "/api/remaining-time" do |env| + profile = app.profile Dayoff::ProfileId.new(env.get("profile_id").to_s) + rem_span = profile.remaining_time now + data = { + hours: rem_span.total_hours.to_i32, + minutes: rem_span.minutes.to_i32, + } + env.response.content_type = "application/json" + data.to_json end Kemal.run diff --git a/src/dayoff/app.cr b/src/dayoff/app.cr new file mode 100644 index 0000000..9dd5641 --- /dev/null +++ b/src/dayoff/app.cr @@ -0,0 +1,15 @@ +module Dayoff + class App + def initialize(@base_path : String) + end + + def profile?(profile_id : ProfileId) : Bool + Dir.exists? File.join(@base_path, profile_id.to_s) + end + + def profile(profile_id : ProfileId) : Profile + storage = FileStorage.new File.join(@base_path, profile_id.to_s) + Profile.new(storage) + end + end +end diff --git a/src/dayoff/entities.cr b/src/dayoff/entities.cr index b017413..a08d78d 100644 --- a/src/dayoff/entities.cr +++ b/src/dayoff/entities.cr @@ -1,6 +1,14 @@ require "json" module Dayoff + # Fix for timezone + struct Time::Format + def from_json(pull : JSON::PullParser) + string = pull.read_string + parse(string) + end + end + class PlannedDate FORMAT = "%Y-%m-%d" diff --git a/src/dayoff/profile.cr b/src/dayoff/profile.cr index b2d8ebe..02f6eca 100644 --- a/src/dayoff/profile.cr +++ b/src/dayoff/profile.cr @@ -3,8 +3,6 @@ module Dayoff def initialize(@id : String) end - getter id - def to_s : String @id end diff --git a/src/handlers.cr b/src/handlers.cr new file mode 100644 index 0000000..bcfc7e9 --- /dev/null +++ b/src/handlers.cr @@ -0,0 +1,32 @@ +class CheckProfileHandler < Kemal::Handler + QUERY_PARAM = "profile_id" + HEADER_PARAM = "X-Dayoff-Profile-Id" + + def initialize(@app : Dayoff::App) + end + + def call(env) + path = env.request.path + puts "run check profile" + if /^\/api/.match(path) + profile_id = get_profile_id env + if @app.profile? profile_id + env.set "profile_id", profile_id.to_s + call_next(env) + else + env.response.status_code = 403 + env.response.print "Forbidden" + env.response.close + end + else + call_next(env) + end + end + + private def get_profile_id(env) : Dayoff::ProfileId + profile_id = env.params.query[QUERY_PARAM]? || + env.request.headers[HEADER_PARAM]? || "" + puts "PROFILE_ID", profile_id + Dayoff::ProfileId.new profile_id + end +end diff --git a/tools/server b/tools/server index 3a251b8..d26e8e8 100755 --- a/tools/server +++ b/tools/server @@ -9,6 +9,7 @@ if [ -t 1 ] ; then fi docker run -i $TTY \ + --init \ -u "$(id -u):$(id -g)" \ -v "$PWD:/app" \ -p "3000:3000" \