fixed dsl stuff, made a better example

This commit is contained in:
Yessiest 2023-09-08 01:09:10 +04:00
parent bbfb6f00d9
commit 9ba3bddc96
10 changed files with 45 additions and 23 deletions

View File

@ -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

View File

@ -1 +0,0 @@
../lib

View File

@ -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

View File

@ -0,0 +1 @@
../../lib

View File

@ -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.

View File

@ -9,7 +9,7 @@ module Hyde
# Get the current request # Get the current request
# @return [Hyde::Request] # @return [Hyde::Request]
def request def request
@request @origin.request
end end
# Stop execution and generate a boilerplate response with the given code # 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 # @raise [UncaughtThrowError] throws :finish to return back to Server
def die(errorcode, backtrace: nil) def die(errorcode, backtrace: nil)
throw :finish, [errorcode].append( throw :finish, [errorcode].append(
*(@properties["handle.#{errorcode}"] or *(@origin.properties["handle.#{errorcode}"] or
@properties["handle.default"]).call( @origin.properties["handle.default"]).call(
errorcode, errorcode,
backtrace: backtrace backtrace: backtrace
) )
@ -29,7 +29,7 @@ module Hyde
# Bounce request to the next handler # Bounce request to the next handler
# @raise [UncaughtThrowError] throws :break to get out of the callback # @raise [UncaughtThrowError] throws :break to get out of the callback
def bounce def bounce
raise :break throw :break
end end
# Set response status (generate response if one doesn't exist yet) # Set response status (generate response if one doesn't exist yet)

View File

@ -25,6 +25,8 @@ module Hyde
@properties = Hyde::Util::Lookup.new(parent&.properties) @properties = Hyde::Util::Lookup.new(parent&.properties)
end end
attr_reader :properties
# Method callback on successful request navigation. # Method callback on successful request navigation.
# Throws an error upon reaching the path. # Throws an error upon reaching the path.
# This behaviour should only be used internally. # This behaviour should only be used internally.

View File

@ -17,6 +17,7 @@ module Hyde
end end
attr_accessor :response attr_accessor :response
attr_reader :request
# Method callback on successful request navigation. # Method callback on successful request navigation.
# Runs block supplied with object initialization. # Runs block supplied with object initialization.