fixed dsl stuff, made a better example
This commit is contained in:
parent
bbfb6f00d9
commit
9ba3bddc96
|
@ -1,18 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
|
||||
require_relative 'lib/hyde'
|
||||
|
||||
app = Hyde::Server.new do
|
||||
preprocess do |request|
|
||||
puts "New request: #{request}"
|
||||
end
|
||||
filter do
|
||||
rand < 0.5
|
||||
end
|
||||
index ["index"]
|
||||
root "#{ENV['PWD']}/assets"
|
||||
serve "*.(html|css|js)"
|
||||
end
|
||||
|
||||
run app
|
|
@ -1 +0,0 @@
|
|||
../lib
|
|
@ -0,0 +1,36 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
|
||||
require_relative 'lib/hyde'
|
||||
|
||||
app = Hyde::Server.new do
|
||||
preprocess do |request|
|
||||
puts "New request: #{request}"
|
||||
end
|
||||
filter do
|
||||
rand < 0.9
|
||||
end
|
||||
postprocess do |request, response|
|
||||
puts "Request: #{request}, response: #{response}"
|
||||
end
|
||||
index ["index"]
|
||||
root "#{ENV['PWD']}/assets"
|
||||
serve "*.(html|css|js)"
|
||||
get "/wormhole/:test/*" do |suffix, test: nil|
|
||||
<<~RESPONSE
|
||||
You tried accessing #{suffix} at named param #{test}
|
||||
Your request is a #{request} object
|
||||
|
||||
#{Hyde::VLINE}
|
||||
RESPONSE
|
||||
end
|
||||
get "/bounce-random" do
|
||||
bounce if rand < 0.5
|
||||
"you win!"
|
||||
end
|
||||
get "/404" do
|
||||
die(404, backtrace: ["backtrace field", "you can write a lot of crap here"])
|
||||
end
|
||||
end
|
||||
|
||||
run app
|
|
@ -0,0 +1 @@
|
|||
../../lib
|
|
@ -0,0 +1 @@
|
|||
This example just shows a lot of stuff for no particular reason. Should work out of the box without any dependencies or anything.
|
|
@ -9,7 +9,7 @@ module Hyde
|
|||
# Get the current request
|
||||
# @return [Hyde::Request]
|
||||
def request
|
||||
@request
|
||||
@origin.request
|
||||
end
|
||||
|
||||
# Stop execution and generate a boilerplate response with the given code
|
||||
|
@ -18,8 +18,8 @@ module Hyde
|
|||
# @raise [UncaughtThrowError] throws :finish to return back to Server
|
||||
def die(errorcode, backtrace: nil)
|
||||
throw :finish, [errorcode].append(
|
||||
*(@properties["handle.#{errorcode}"] or
|
||||
@properties["handle.default"]).call(
|
||||
*(@origin.properties["handle.#{errorcode}"] or
|
||||
@origin.properties["handle.default"]).call(
|
||||
errorcode,
|
||||
backtrace: backtrace
|
||||
)
|
||||
|
@ -29,7 +29,7 @@ module Hyde
|
|||
# Bounce request to the next handler
|
||||
# @raise [UncaughtThrowError] throws :break to get out of the callback
|
||||
def bounce
|
||||
raise :break
|
||||
throw :break
|
||||
end
|
||||
|
||||
# Set response status (generate response if one doesn't exist yet)
|
||||
|
|
|
@ -25,6 +25,8 @@ module Hyde
|
|||
@properties = Hyde::Util::Lookup.new(parent&.properties)
|
||||
end
|
||||
|
||||
attr_reader :properties
|
||||
|
||||
# Method callback on successful request navigation.
|
||||
# Throws an error upon reaching the path.
|
||||
# This behaviour should only be used internally.
|
||||
|
|
|
@ -17,6 +17,7 @@ module Hyde
|
|||
end
|
||||
|
||||
attr_accessor :response
|
||||
attr_reader :request
|
||||
|
||||
# Method callback on successful request navigation.
|
||||
# Runs block supplied with object initialization.
|
||||
|
|
Loading…
Reference in New Issue