RealWorld back-end API implementation ✔️
Switch branches/tags
Clone or download
Permalink
Failed to load latest commit information.
db/migrations initial commit May 25, 2018
media initial commit May 25, 2018
spec
src
.dockerignore initial commit May 25, 2018
.editorconfig initial commit May 25, 2018
.env.example initial commit May 25, 2018
.gitignore initial commit May 25, 2018
.travis.yml initial commit May 25, 2018
Cakefile
Dockerfile
LICENSE initial commit May 25, 2018
README.md docs: add patreon-related information to README Sep 22, 2018
shard.lock feat: update to work with Crystal 0.26 Aug 16, 2018
shard.yml feat: update to work with Crystal 0.26 Aug 16, 2018

README.md

Crystal World

Built with Crystal Build status Awesome vladfaust.com Patrons count

Welcome to the Crystal World, a RealWorld back-end API application implemented in Crystal!

Become Patron

About

This project is aimed to demonstrate how easy and enjoyable it is to develop a highly-performant web application in Crystal, which is proven to be both fast and slick language, featuring:

  • ⚡️ Efficiency comparable to languages like Go and C
  • 💎 Beauty inherited from Ruby
  • 🔒 Type system with smart compilator

Crystal World relies on Prism web framework and Core ORM as its foundation. The database chosen is PostgreSQL. Go ahead and read the source code, you'll find it delightfully simple and understandeable! 🍰

Note that although Crystal itself is not officially released yet, it's already being used in many real-world (including business) applications.

Installation

Clone this repository with

git clone https://github.com/vladfaust/crystalworld.git && cd crystalworld

You'll need to install Crystal Programming Language for non-docker based installation. You'll also need PostgreSQL installed and running. Write down its access url (e.g. postgres://postgres:postgres@localhost:5432/crystalworld), it will be used as DATABASE_URL environment variable.

Crystal is a compiled language. It has different compilation modes, e.g. development and production. Production code is faster and packed into a single binary, however it builds slightly longer. Therefore, there are multiple ways to proceed:

Production build from the source code

  1. Set env vars: export APP_ENV=production DATABASE_URL="" JWT_SECRET_KEY=""
  2. Build the app: shards build --production --release --no-debug
  3. Apply migrations: ./bin/cake db:migrate
  4. Launch the server: ./bin/server

Production build with Docker

Remember that you'll need to somehow link PostgreSQL to the image.

  1. Build an image: docker build -t crystalworld:latest .
  2. Apply migrations:docker run -e DATABASE_URL="" crystalworld bin/cake db:migrate
  3. Launch the server: docker run -p 5000:5000 -e DATABASE_URL="" -e JWT_SECRET_KEY="" crystalworld

Development build

  1. Create an /.env.development file and put some environment variables there (see below)
  2. Apply migrations: crystal src/bin/cake.cr -- db:migrate (or install Cake and exec cake db:migrate)
  3. Launch the server in development mode: crystal src/bin/server.cr

Example output:

[21:57:17:276] DEBUG > Welcome to the Crystal World! ✨ https://github.com/vladfaust/crystalworld
[21:57:17:276]  INFO > Prism::Server v0.2.0 is listening on http://0.0.0.0:5000
[21:59:39:561]  INFO > SELECT articles.*, '' AS _author, author.username FROM articles JOIN users AS "author" ON "author".id = articles.author_id WHERE (articles.slug = $1)
[21:59:39:562]  INFO > 909μs
[21:59:39:562]  INFO > SELECT favorites.* FROM favorites WHERE (favorites.article_id = $1 AND favorites.user_id = $2)
[21:59:39:563]  INFO > 527μs
[21:59:39:563]  INFO >     GET /articles/how-to-train-your-dragon 200 2.043ms

Environment variables

Crystal World follows twelve-factor methodology and therefore relies on environment variables. You can see example and default variables at /.env.example file.

Testing

Crystal has a convenient testing environment built-in. Read more at Testing docs. Basically, you place specs under /spec folder and run them with crystal spec. It's also a good idea to have a separate /.env.test file.

Contributors