From 9ba3bddc9645d245f440c6406da4fb58f6f2fc35 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Fri, 8 Sep 2023 01:09:10 +0400 Subject: [PATCH] fixed dsl stuff, made a better example --- examples/config.ru | 18 ---------- examples/lib | 1 - .../assets/index.html | 0 .../{ => norxondor_gorgonax}/assets/pee.css | 0 examples/norxondor_gorgonax/config.ru | 36 +++++++++++++++++++ examples/norxondor_gorgonax/lib | 1 + examples/norxondor_gorgonax/readme.txt | 1 + lib/hyde/dsl/probe_methods.rb | 8 ++--- lib/hyde/probe.rb | 2 ++ lib/hyde/probe/handler.rb | 1 + 10 files changed, 45 insertions(+), 23 deletions(-) delete mode 100644 examples/config.ru delete mode 120000 examples/lib rename examples/{ => norxondor_gorgonax}/assets/index.html (100%) rename examples/{ => norxondor_gorgonax}/assets/pee.css (100%) create mode 100644 examples/norxondor_gorgonax/config.ru create mode 120000 examples/norxondor_gorgonax/lib create mode 100644 examples/norxondor_gorgonax/readme.txt diff --git a/examples/config.ru b/examples/config.ru deleted file mode 100644 index 3f36242..0000000 --- a/examples/config.ru +++ /dev/null @@ -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 diff --git a/examples/lib b/examples/lib deleted file mode 120000 index dc598c5..0000000 --- a/examples/lib +++ /dev/null @@ -1 +0,0 @@ -../lib \ No newline at end of file diff --git a/examples/assets/index.html b/examples/norxondor_gorgonax/assets/index.html similarity index 100% rename from examples/assets/index.html rename to examples/norxondor_gorgonax/assets/index.html diff --git a/examples/assets/pee.css b/examples/norxondor_gorgonax/assets/pee.css similarity index 100% rename from examples/assets/pee.css rename to examples/norxondor_gorgonax/assets/pee.css diff --git a/examples/norxondor_gorgonax/config.ru b/examples/norxondor_gorgonax/config.ru new file mode 100644 index 0000000..abf32ed --- /dev/null +++ b/examples/norxondor_gorgonax/config.ru @@ -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 diff --git a/examples/norxondor_gorgonax/lib b/examples/norxondor_gorgonax/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/norxondor_gorgonax/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/norxondor_gorgonax/readme.txt b/examples/norxondor_gorgonax/readme.txt new file mode 100644 index 0000000..a7a8d3c --- /dev/null +++ b/examples/norxondor_gorgonax/readme.txt @@ -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. diff --git a/lib/hyde/dsl/probe_methods.rb b/lib/hyde/dsl/probe_methods.rb index 7673ecb..5d113c7 100644 --- a/lib/hyde/dsl/probe_methods.rb +++ b/lib/hyde/dsl/probe_methods.rb @@ -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) diff --git a/lib/hyde/probe.rb b/lib/hyde/probe.rb index e64f50d..0dd351a 100644 --- a/lib/hyde/probe.rb +++ b/lib/hyde/probe.rb @@ -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. diff --git a/lib/hyde/probe/handler.rb b/lib/hyde/probe/handler.rb index 0cbb29c..7df15c1 100644 --- a/lib/hyde/probe/handler.rb +++ b/lib/hyde/probe/handler.rb @@ -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.