Add web routes
This commit is contained in:
parent
72fe4e5569
commit
81da30fb01
@ -1,18 +1,38 @@
|
|||||||
require "kemal"
|
require "kemal"
|
||||||
require "./dayoff/**"
|
require "./dayoff/**"
|
||||||
|
require "./handlers"
|
||||||
|
|
||||||
module Dayoff
|
base_path = "./tmp"
|
||||||
VERSION = "0.1.0"
|
|
||||||
|
|
||||||
class App
|
app = Dayoff::App.new base_path
|
||||||
def profile(profile_id : ProfileId)
|
|
||||||
Profile.new(profile_id.to_s)
|
add_handler CheckProfileHandler.new(app)
|
||||||
end
|
|
||||||
end
|
def now
|
||||||
|
Time.local(Time::Location.load("Europe/Moscow"))
|
||||||
end
|
end
|
||||||
|
|
||||||
get "/" do
|
post "/api/start" do |env|
|
||||||
"Hello World!"
|
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
|
end
|
||||||
|
|
||||||
Kemal.run
|
Kemal.run
|
||||||
|
15
src/dayoff/app.cr
Normal file
15
src/dayoff/app.cr
Normal file
@ -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
|
@ -1,6 +1,14 @@
|
|||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
module Dayoff
|
module Dayoff
|
||||||
|
# Fix for timezone
|
||||||
|
struct Time::Format
|
||||||
|
def from_json(pull : JSON::PullParser)
|
||||||
|
string = pull.read_string
|
||||||
|
parse(string)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class PlannedDate
|
class PlannedDate
|
||||||
FORMAT = "%Y-%m-%d"
|
FORMAT = "%Y-%m-%d"
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ module Dayoff
|
|||||||
def initialize(@id : String)
|
def initialize(@id : String)
|
||||||
end
|
end
|
||||||
|
|
||||||
getter id
|
|
||||||
|
|
||||||
def to_s : String
|
def to_s : String
|
||||||
@id
|
@id
|
||||||
end
|
end
|
||||||
|
32
src/handlers.cr
Normal file
32
src/handlers.cr
Normal file
@ -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
|
@ -9,6 +9,7 @@ if [ -t 1 ] ; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
docker run -i $TTY \
|
docker run -i $TTY \
|
||||||
|
--init \
|
||||||
-u "$(id -u):$(id -g)" \
|
-u "$(id -u):$(id -g)" \
|
||||||
-v "$PWD:/app" \
|
-v "$PWD:/app" \
|
||||||
-p "3000:3000" \
|
-p "3000:3000" \
|
||||||
|
Loading…
Reference in New Issue
Block a user