backend

Version, currently master branch1 version
  • master branchlatestSep 15, 2016

github.com/dantebronto/crystal-api-backend

Proof-of-concept JSON API written in Crystal

27 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  backend:
    github: dantebronto/crystal-api-backend
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
no constraint declared
License
MIT
Author
Kellen Presley

Dependencies

Runtime Dependencies

  • pg*github: will/crystal-pg, commit: cafe2a2c90a22595abe1312d330567cd85e683b2
  • jwt*github: akwiatkowski/crystal-jwt, branch: master
  • pool*github: ysbaddaden/pool, branch: master
  • uuid*github: raydf/crystal-uuid, branch: master
  • kemal*github: sdogruyol/kemal, commit: dc3cc7479871776f33c92f22025e70ddea55d595
  • micrate*github: juanedi/micrate, branch: master

Development Dependencies

  • spec2*github: waterlink/spec2.cr, branch: masterdev

README

Crystal API Backend

Just a proof-of-concept JSON API written in Crystal. I wanted to get a rough idea of the usability and performance of the language. Conclusion: it works, and it's fast.

Features

  • Built on top of Kemal
  • Source organized around namespaced "concepts" (see src/concepts)
  • Simple chainable string builder for SQL queries
  • SQL query logging
  • Global error handlers
  • Routing sugar to map controller instance methods to block based Kemal handlers (see config/routes.cr)
  • Secure BCrypt password generation with JWT tokens for API authentication
  • Routing index action that can return a list of routes to frontend applications
  • Config file and loader code to set environment variables from ENV or a yml file
  • Migrations from micrate ./db/migrate
  • DB seeding via ./db/seed
  • Controller and model specs crystal spec/backend_spec.cr (runs in about 3s including compilation)
  • Mock request code for testing API calls (Crystal still needs something similar to Rack::Test)

Benchmarks

After running ./db/seed, crystal build src/backend.cr --release and starting the server with LOG_DB=false ./backend

curl -i -H "Content-Type: application/json" -X POST -d '{ "email": "admin@example.com", "password": "changeme"}' http://localhost:3000/api/sessions

export TOKEN=token-that-comes-back-on-successful-auth

Using Siege with max concurrency:

siege -c 255 -r 50 --header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json" http://localhost:3000/api/users

Transactions:          12750 hits
Availability:         100.00 %
Elapsed time:          33.49 secs
Data transferred:         1.87 MB
Response time:            0.00 secs
Transaction rate:       380.71 trans/sec
Throughput:           0.06 MB/sec
Concurrency:            1.18
Successful transactions:       12750
Failed transactions:             0
Longest transaction:          0.04
Shortest transaction:         0.00

That's pretty neat.

It uses ~10% of a single i7 CPU for the Crystal server.

Automatic compilation and restart:

I use nodemon to detect changes and automatically restart the server and run tests:

nodemon --exec crystal src/backend.cr

nodemon --exec crystal spec/backend_spec.cr

It seems to get tripped up and needs to be restarted when the output is a really long stacktrace or something ¯\(ツ)