crystalworld
Example Crystal codebase that adheres to the RealWorld API spec.
Welcome to the Crystal World, a RealWorld back-end application written in Crystal!
About
This project is aimed to demonstrate of how easy and enjoyable it is to develop a highly-perfomant web application in Crystal, which is proven to be both fast and beautiful language.
Crystal features:
- Efficiency comparable to languages like Go and C decrease server costs
- Beauty inherited from Ruby increase developers' happiness
- Type system with smart compilator catch errors early
Go ahead and read the source code, you'll find it delightfully simple and understandeable!
Crystal World uses Prism web framework and Core ORM as its foundation. The database chosen is PostgreSQL.
Note that although Crystal itself is not officially released yet, it's already being used in many real-world (including business) applications.
Installation
First of all, you'll need to install Crystal Programming Language. Then clone this repository with git clone https://github.com/vladfaust/crystalworld.git && cd crystalworld
.
To run Crystal World, you'll 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. Obviously, production code is much 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
- Build the app:
shards build --production
- Set env vars:
export DATABASE_URL="" JWT_SECRET_KEY=""
- Apply migrations:
./bin/cake db:migrate
- Launch the server:
./bin/server
Production build with Docker
Remember that you'll need to somehow link PostgreSQL to the image
- Build an image:
docker build -t crystalworld:latest .
- Apply migrations:
docker run -e DATABASE_URL="" crystalworld bin/cake db:migrate
- Launch the server:
docker run -p 5000:5000 -e DATABASE_URL="" -e JWT_SECRET_KEY="" crystalworld
Development build
- Create an
/.env.development
file and put some environment variables there (see below) - Apply migrations:
crystal src/cake.cr -- db:migrate
(or install Cake and execcake db:migrate
) - Launch the server in development mode:
crystal src/server.cr
Example output:
[21:57:17:276] DEBUG > Welcome to 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
- @vladfaust Vlad Faust - creator, maintainer