crystalworld

realworld.io back-end API implementation
0.0.1 released
vladfaust/crystalworld
42 5 2
Vlad Faust

Crystal World

Built with Crystal Build status Awesome vladfaust.com

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

  1. Build the app: shards build --production
  2. Set env vars: export DATABASE_URL="" JWT_SECRET_KEY=""
  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/cake.cr -- db:migrate (or install Cake and exec cake db:migrate)
  3. 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

crystalworld:
  github: vladfaust/crystalworld
  version: ~> 0.0.1
License MIT
Crystal 0.24.2

Authors

Dependencies 6

  • cake-bake ~> 0.2.0
    {'github' => 'vladfaust/cake-bake.cr', 'version' => '~> 0.2.0'}
  • onyx-sql v0.4.0-rc.2
    {'tag' => 'v0.4.0-rc.2', 'github' => 'vladfaust/core.cr'}
  • jwt ~> 0.2.2
    {'github' => 'crystal-community/jwt', 'version' => '~> 0.2.2'}
  • migrate v0.2.0-pre.1
    {'tag' => 'v0.2.0-pre.1', 'github' => 'vladfaust/migrate.cr'}
  • pg ~> 0.14.1
    {'github' => 'will/crystal-pg', 'version' => '~> 0.14.1'}
  • onyx-http v0.2.0-rc.4
    {'tag' => 'v0.2.0-rc.4', 'github' => 'vladfaust/prism'}

Development Dependencies 1

  • dotenv ~> 0.1.0
    {'github' => 'gdotdesign/cr-dotenv', 'version' => '~> 0.1.0'}

Dependents 0

Last synced .
search fire star recently