Add web routes
This commit is contained in:
parent
72fe4e5569
commit
81da30fb01
@ -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
|
||||
|
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"
|
||||
|
||||
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"
|
||||
|
||||
|
@ -3,8 +3,6 @@ module Dayoff
|
||||
def initialize(@id : String)
|
||||
end
|
||||
|
||||
getter id
|
||||
|
||||
def to_s : String
|
||||
@id
|
||||
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
|
||||
|
||||
docker run -i $TTY \
|
||||
--init \
|
||||
-u "$(id -u):$(id -g)" \
|
||||
-v "$PWD:/app" \
|
||||
-p "3000:3000" \
|
||||
|
Loading…
Reference in New Issue
Block a user