Yessiest
8 months ago
17 changed files with 304 additions and 65 deletions
-
30HACKING.md
-
0LAYOUT.md
-
38config.ru
-
6examples/assets/index.html
-
3examples/assets/pee.css
-
18examples/config.ru
-
1examples/lib
-
4lib/hyde/dsl/path_constructors.rb
-
64lib/hyde/dsl/path_methods.rb
-
22lib/hyde/node.rb
-
60lib/hyde/path.rb
-
6lib/hyde/probe.rb
-
2lib/hyde/probe/handler.rb
-
28lib/hyde/probe/serve_handler.rb
-
49lib/hyde/request.rb
-
29lib/hyde/response.rb
-
9lib/hyde/server.rb
@ -0,0 +1,30 @@ |
|||
# Notes and things to consider on Hyde hacking |
|||
|
|||
The structure of the Hyde rewrite was specifically redesigned to allow for |
|||
extensive modification and extension. So to keep things that way, you may |
|||
want to consider the methodology of writing Hyde code. |
|||
|
|||
## Recommendations |
|||
|
|||
To keep things beautiful, consider following recommendations: |
|||
|
|||
- **USE COMMON SENSE**. None of these guidelines will ever be adequate |
|||
enough to replace common sense. |
|||
- **Code less, think with ~~portals~~ what you have**. To minimize code |
|||
overhead, try to use existing functionality to get the effect you want. |
|||
(i.e. if you want to append headers to a request when it traverses a path, |
|||
don't write a new class variable and handler for this - just create a new |
|||
DSL method that really just appends a preprocessor to the path. Or avoid |
|||
making something like this at all - after all, preprocessors exist |
|||
exactly for that reason.) |
|||
- Preferably, **extend the DSL and not the class*. Extensive class |
|||
modifications make code a lot less maintainable, if it wasn't obvious |
|||
already. If it can't be helped, then at the very least use Rubocop. |
|||
- Document classes as if the next maintainer after you has you at gunpoint. |
|||
Document thoroughly, use YARD tags and **never** skip on public method |
|||
docs and class docs. As an example, consider Hyde::PatternMatching::Glob. |
|||
- Unit tests suck for many reasons. However, if you're writing a class that |
|||
does not have any dependents and which is frequently used, consider making |
|||
a unit test for it. People that might have to fix things further along |
|||
will be very thankful. |
|||
|
@ -1,38 +0,0 @@ |
|||
# frozen_string_literal: true |
|||
|
|||
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib") |
|||
require_relative 'lib/hyde' |
|||
|
|||
app = Hyde::Server.new do |
|||
path /^test\/\w+/ do |
|||
probe "probe" |
|||
end |
|||
|
|||
path "/subdir/test" do |
|||
probe "probe" |
|||
end |
|||
|
|||
path "/match/*/test/:test/" do |
|||
probe "probe" |
|||
end |
|||
|
|||
path "/match/:test/" do |
|||
probe "probe" |
|||
end |
|||
|
|||
path "/match2/*/" do |
|||
head "probe" do |
|||
"<html><body><p>Hello world!</p></body></html>" |
|||
end |
|||
get "probe" do |
|||
code 400 |
|||
header "Random-Shit", "peepee" |
|||
"<html><body><p>Hello world!</p></body></html>" |
|||
end |
|||
post "probe" do |
|||
"<html><body><p>Hello world!</p></body></html>" |
|||
end |
|||
end |
|||
end |
|||
|
|||
run app |
@ -0,0 +1,6 @@ |
|||
<!DOCTYPE HTML> |
|||
<html> |
|||
<body> |
|||
<p> index page </p> |
|||
</body> |
|||
</html> |
@ -0,0 +1,3 @@ |
|||
.a { |
|||
color: #FF00FF; |
|||
} |
@ -0,0 +1,18 @@ |
|||
# 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 |
@ -0,0 +1 @@ |
|||
../lib |
@ -0,0 +1,64 @@ |
|||
# frozen_string_literal: true |
|||
|
|||
module Hyde |
|||
# Shared DSL methods |
|||
module DSL |
|||
# Common path methods |
|||
module PathMethods |
|||
# Set path index |
|||
# @param index [Array,String] |
|||
def index(index) |
|||
case index |
|||
when Array |
|||
@origin.properties['index'] = index |
|||
when String |
|||
@origin.properties['index'] = [index] |
|||
else |
|||
raise StandardError, "index should be an Array or a String" |
|||
end |
|||
end |
|||
|
|||
# Set root path (appends matched part of the path). |
|||
# @param path [String |
|||
def root(path) |
|||
raise StandardError, "path should be a String" unless path.is_a? String |
|||
|
|||
@origin.root = path |
|||
end |
|||
|
|||
# Set root path (without appending matched part). |
|||
# @param path [String |
|||
def remap(path) |
|||
root(path) |
|||
@origin.remap = true |
|||
end |
|||
|
|||
# Add a preprocessor to the path. |
|||
# Does not modify path execution. |
|||
# @param block [#call] |
|||
# @yieldparam request [Hyde::Request] |
|||
def preprocess(&block) |
|||
@origin.preprocess(&block) |
|||
block |
|||
end |
|||
|
|||
# Add a postprocessor to the path. |
|||
# @param block [#call] |
|||
# @yieldparam request [Hyde::Request] |
|||
# @yieldparam response [Hyde::Response] |
|||
def postprocess(&block) |
|||
@origin.postprocess(&block) |
|||
block |
|||
end |
|||
|
|||
# Add a filter to the path. |
|||
# Blocks path access if a filter returns false. |
|||
# @param block [#call] |
|||
# @yieldparam request [Hyde::Request] |
|||
def filter(&block) |
|||
@origin.filter(&block) |
|||
block |
|||
end |
|||
end |
|||
end |
|||
end |
@ -0,0 +1,28 @@ |
|||
# frozen_string_literal: true |
|||
|
|||
require_relative '../probe' |
|||
require_relative 'binding' |
|||
|
|||
module Hyde |
|||
# Probe that sends files from a location |
|||
class ServeHandler < Hyde::Probe |
|||
# @param path [Object] |
|||
# @param parent [Hyde::Node] |
|||
# @param exec [#call] |
|||
def initialize(path, parent:) |
|||
super(path, parent: parent) |
|||
end |
|||
|
|||
attr_accessor :response |
|||
|
|||
# Method callback on successful request navigation. |
|||
# Tries to serve files matched by handler |
|||
# @param request [Hyde::Request] |
|||
# @return [Boolean] true if file was found |
|||
def process(request) |
|||
File.open(request.filepath.delete_suffix("/")) |
|||
rescue StandardError |
|||
false |
|||
end |
|||
end |
|||
end |
Write
Preview
Loading…
Cancel
Save
Reference in new issue