• Stars
    star
    56
  • Rank 512,598 (Top 11 %)
  • Language
    Crystal
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sinatra-like DSL for the Crystal language (abusing macros)

Artanis

Crystal's metaprogramming macros to build a Sinatra-like DSL for the Crystal language.

Rationale

The DSL doesn't stock blocks to be invoked later on, but rather produces actual methods using macros (match and the sugar get, post, etc.) where special chars like /, ., ( and ) are replaced as _SLASH_, _DOT_, _LPAREN_ and _RPAREN_. Also, :param segments are replaced to _PARAM_.

Eventually methods look like:

get "/posts"        |    def match_GET__SLASH_posts
get "/posts.xml"    |    def match_GET__SLASH_posts_DOT_xml

Please read dsl.cr for more details.

Eventually a call method is generated. It iterates over the class methods, selects the generated match_* methods and generates a big case statement, transforming the method names back to regular expressions, and eventually calling the method with matched data (if any).

Please read application.cr for more details.

Usage

require "http/server"
require "artanis"

class App < Artanis::Application
  get "/" do
    "ROOT"
  end

  get "/forbidden" do
    403
  end

  get "/posts/:id.:format" do
    p params["id"]
    p params["format"]
    "post"
  end

  get "/posts/:post_id/comments/:id(.:format)" do |post_id, id, format|
    p params["format"]?
    200
  end
end

server = HTTP::Server.new(9292) do |context|
  App.call(context)
end

puts "Listening on http://0.0.0.0:9292"
server.listen

Benchmark

Running wrk against the above example (pointless hello world) gives the following results (TL;DR 12µs per request):

$ wrk -c 1000 -t 2 -d 5 http://localhost:9292/fast
Running 5s test @ http://localhost:9292/fast
  2 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    12.26ms   14.48ms 423.28ms   99.05%
    Req/Sec    41.17k     2.93k   48.65k    76.00%
  409663 requests in 5.01s, 25.79MB read
Requests/sec:  81722.08
Transfer/sec:      5.14MB

A better benchmark is available in test/dsl_bench.cr which monitors some limits of the generated Crystal code, like going over all routes to find nothing takes an awful lot of time, since it must build/execute a regular expression against EVERY routes to eventually... find nothing.

$ crystal run --release test/dsl_bench.cr
get root: 0.84 µs
get param: 1.51 µs
get params (block args): 2.18 µs
get many params: 3.75 µs
get many params (block args): 2.59 µs
not found (method): 0.73 µs
not found (path): 15.93 µs

Keep in mind these numbers tell nothing about reality. They only measure how fast the generated Application.call(request) method is in predefined cases.

License

Licensed under the MIT License

More Repositories

1

prax

Rack proxy server for development
Ruby
475
star
2

prax.cr

Rack proxy server for development (Crystal port)
Crystal
150
star
3

minitest.cr

Test Unit for the Crystal programming language
Crystal
144
star
4

frost

Web Framework for Crystal
Crystal
117
star
5

sdl.cr

SDL2 bindings for Crystal
Crystal
99
star
6

earl

Service Objects for Crystal (Agents, Artists, Supervisors, Pools, ...)
Crystal
94
star
7

http2

HTTP/2 Protocol Implementation for the Crystal Language
Crystal
93
star
8

gc

A garbage collector for Crystal
C
91
star
9

pool

Generic (connection) pools for Crystal
Crystal
76
star
10

selenium-webdriver-crystal

Selenium Webdriver client for the Crystal programming language
Crystal
56
star
11

posix

POSIX/C bindings generator for the Crystal programming language
Crystal
33
star
12

panzer

Multi-process, zero-downtime service monitor for Crystal
Crystal
29
star
13

android.cr

Create Android applications using Crystal and the NDK
Crystal
22
star
14

java.cr

Java Native Interface (JNI) bindings (and generator) for Crystal
Crystal
22
star
15

scrypt-crystal

Crystal bindings for Colin Percival's scrypt key derivation function
Crystal
21
star
16

muco

Multithreaded Coroutines library
C
20
star
17

crystal_library

Static/Shared libraries written in Crystal (Proof of Concept)
Crystal
20
star
18

crystal-cmark

Crystal wrapper for libcmark, the reference CommonMark C library
Crystal
18
star
19

syn

Synchronization primitives to build concurrent and parallel-safe data structures in Crystal
Crystal
17
star
20

nanolib.cr

Crystal
15
star
21

ruby-akismet

Ruby library for the Akismet anti-spam service.
Ruby
13
star
22

rome

Crystal
12
star
23

janus

Authentication engine for Ruby on Rails.
Ruby
12
star
24

shards-registry

Crystal
8
star
25

minitest-js

A JavaScript port of the minitest ruby gem
JavaScript
7
star
26

sql

SQL query builder for Crystal
Crystal
7
star
27

jsruby

Ruby Virtual Machine using node.js
JavaScript
6
star
28

microtest.cr

A test framework for raw Crystal (empty prelude)
Crystal
6
star
29

siphash.cr

SipHash family of pseudo random functions implemented in Crystal
Crystal
5
star
30

php5-redis

Yet another Redis client for PHP.
PHP
5
star
31

teascript

An attempt at creating a Ruby inspired language that compiles to JavaScript.
JavaScript
4
star
32

misago

Yet Another PHP 5 Framework
PHP
4
star
33

sinapse

Push service for Ruby using EventSource
Ruby
4
star
34

alpha

JavaScript
4
star
35

calliope

Blog engine for Ruby on Rails
Ruby
3
star
36

earl.rb

Service Objects for Ruby (Agents, Artists, Supervisors, Pools, ...)
Ruby
3
star
37

acts_as_enum

Treat an integer or string ActiveRecord column as a list of symbols.
Ruby
3
star
38

960-atatonic

CSS framework experiment based on 960 and Atatonic
3
star
39

pdoc

A minimalistic documentation tool for PHP.
PHP
3
star
40

ewkb

Crystal
2
star
41

pake

pake is meant to provide centralized CLI scripts to your PHP 5 projects
PHP
2
star
42

ng-rails-ujs

Angular directives for Rails UJS
JavaScript
2
star
43

markup

Ruby
2
star
44

redis_model

Ruby
2
star
45

zombie_admin

A brainless alternative to ActiveAdmin that speaks zombie fluently.
Ruby
2
star
46

mdown

Markdown editor with live preview (web app experiment)
Ruby
2
star
47

wyme

JavaScript
2
star
48

imhotep

Wiki engine for Ruby on Rails.
Ruby
2
star
49

acts_as_publish

Provides ActiveRecords with publishing capabilities
Ruby
2
star
50

alpha-control

JavaScript
2
star
51

ejs

ruby-like template engine for javascript
JavaScript
2
star
52

alpha-core

An obstrusive JavaScript library that aims at fixing, implementing and emulating standard JavaScript and DOM in broken and old browsers.
JavaScript
2
star
53

acts_as_sanitize

Sanitizes user data within ActiveRecord
Ruby
2
star
54

commentable

Ruby
2
star
55

sechat

Q&A engine for Ruby on Rails
Ruby
2
star
56

erb_assets

Render your Rails views into the Assets Pipeline
Ruby
1
star
57

rbs

Ruby inspired language that transcompiles to simple JavaScript
Ruby
1
star
58

dockerdev.cr

Crystal
1
star
59

swagger.cr

Crystal
1
star
60

brium-app

Crystal
1
star
61

calliper

Protractor for Ruby, or testing your Angular application with elegance
Ruby
1
star
62

execution_context

Crystal
1
star