ysbaddaden / artanis Goto Github PK
View Code? Open in Web Editor NEWSinatra-like DSL for the Crystal language (abusing macros)
License: MIT License
Sinatra-like DSL for the Crystal language (abusing macros)
License: MIT License
One issue is in https://github.com/ysbaddaden/artanis/blob/master/src/dsl.cr#L57, \\Z
is interpreted literally now, replacing it with \z
(note the lowercase z) works and fixes most tests, but there are a couple of other failures I couldn't figure out.
The idea is to generate a tree of routes, using the different segments for each node, which may either be a static string (eg: posts
) or a variable (eg: :id
) with or without a regexp match (eg: /^\d+$/
) or always matching (eg: *path
).
Resolving a route should then be (almost) constant time, and not tied to wherever the route is defined first or last. Currently, hitting the first matching route is fast (we run a single regular expression) but matching the last route is slow (we run N regular expressions, where N can grow quickly). We can define the most important routes first, and the less used ones last, but what about when the route doesn't exist? A potential DDoS is to hit long URI, so each regexp is slow, and that doesn't match.
I don't know what will happen to the before
and after
blocks. Maybe they can just become attributes to the tree node, and run whenever the node matches.
def test_flushes_body_to_io
call("GET", "/custom", io = MemoryIO.new)
body = io.to_s
assert_match /LAYOUT: CUSTOM/, body
assert_match /message: custom/, body
end
[brian@orville artanis]$ make
crystal test/*_test.cr
................F..
Finished in 00:00:00.0068770, 145.412 runs/s
19 tests, 1 failures, 0 errors, 0 skips
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 255
Premise: there's no views
folder and I'm using OSX 10.10 (zsh as shell).
require "artanis"
class App < Artanis::Application
end
The views_path
macro produces this (invalid) code:
def render_-n_ecr
render_-n_ecr {}
end
def render_-n_ecr(&block)
String.build do |__str__|
embed_ecr "views/-n.ecr", "__str__"
end
end
The issue seems to be the echo -n ""
part of the command, producing -n
as output (I don't know whether the backtick syntax executes the command inside a shell).
I temporarily worked around the issue by using true
instead of echo -n ""
, which fixes the issue since the output is the same.
require "artanis"
class App < Artanis::Application
end
The call
macro generates an invalid case
statement:
case request.method.upcase
else
no_such_route
end
response
Checking whether there actually are handlers defined fixes the issue:
macro def call : HTTP::Response
{% if @type.methods.size > 0 %}
case request.method.upcase
{{
@type.methods
.map(&.name.stringify)
.select(&.starts_with?("match_"))
.map { |method_name| method_name.split("_")[1] }
.uniq
.map { |method| "when #{method}\n call_method(#{method})" }
.join("\n ")
.id
}}
else
no_such_route
end
{% else %}
no_such_route
{% end %}
response
end
Hello @ysbaddaden:
I discovered this dsl today, nice work. Is there any way to integrate chained filters for each macro get, post, etc?
For example a normal pattern will be to have jwt integration in a JSON api for a lot of get and post routes, and if there's a pre filter with a token verification error it should always return 403 (forbidden).
Thanks in advance,
raydf
Using latest shards / crystal installation.
[brian@orville artanis]$ shards
Updating https://github.com/ysbaddaden/minitest.cr.git
Using minitest (f2014c2175064e0b41bdfdc4b64f8a69b25da075)
[brian@orville artanis]$ make
crystal test/*_test.cr
Error in ./test/dsl_test.cr:1: while requiring "./test_helper"
require "./test_helper"
^
in ./test/test_helper.cr:1: while requiring "../libs/minitest/autorun"
require "../libs/minitest/autorun"
^
in ./libs/minitest/autorun.cr:1: while requiring "./minitest"
require "./minitest"
^
in ./libs/minitest/minitest.cr:5: while requiring "./minitest/test"
require "./minitest/test"
^
in ./libs/minitest/minitest/test.cr:1: while requiring "./assertions"
require "./assertions"
^
Syntax error in ./libs/minitest/minitest/assertions.cr:7: the syntax for an argument with a default value V and type T is arg : T = V
def initialize(@message = nil : String?, @Cause = nil : Exception?, @file = FILE, @line = LINE)
^
Makefile:3: recipe for target 'test' failed
make: *** [test] Error 1
[brian@orville artanis]$ crystal --version
Crystal 0.14.2 [e1af3d5](Wed Mar 23 00:03:46 UTC 2016)
I'm using crystal 0.17 on a debian 32 bit system and I am getting:
$ crystal run example.cr
Error in ./example.cr:26: instantiating 'App:Class#call(HTTP::Server::Context)'
App.call(context)
^~~~
in ./libs/artanis/application.cr:50: instantiating 'App#call()'
new(context).call
^~~~
in macro 'call_action' /media/disk/bentkus/Projects/crystal/artanis/libs/artanis/dsl.cr:87, line 1:
1. if __temp_99 = URI.unescape(request.path || "/").match(MATCH_GET__SLASH_)
2. __temp_100 = match_GET__SLASH_(__temp_99)
3.
4.
5. break unless __temp_100 == :pass
6.
7. end
8.
if __temp_99 = URI.unescape(request.path || "/").match(MATCH_GET__SLASH_)
^~~~~~~~~~~~~~~~~
undefined constant MATCH_GET__SLASH_
I haven't looked into the code yet, it was pointed out to me in the channel that this library makes use of the macro features and that I should look into it to see what is possible.
Any quick idea on why this error is thrown?
[brian@orville artanis]$ make
crystal test/*_test.cr
Error in ./test/dsl_test.cr:1: while requiring "./test_helper"
require "./test_helper"
^
in ./test/test_helper.cr:3: while requiring "../src/artanis"
require "../src/artanis"
^
in ./src/artanis.cr:1: while requiring "./application"
require "./application"
^
in ./src/application.cr:3: while requiring "http/response": can't find file 'http/response' relative to '/data2/Projects/artanis/src'
require "http/response"
^
Makefile:3: recipe for target 'test' failed
make: *** [test] Error 1
Any ideas ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.