crystalworld

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

Crystal World

Built with Crystal Build status Awesome vladfaust.com

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

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

crystalworld:
  github: vladfaust/crystalworld
  version: ~> 0.1.0
License MIT
Crystal 0.25.0

Authors

Dependencies 6

  • cake-bake ~> 0.2.0
    {'github' => 'vladfaust/cake-bake.cr', 'version' => '~> 0.2.0'}
  • onyx-sql ~> 0.4.0
    {'github' => 'vladfaust/core', 'version' => '~> 0.4.0'}
  • jwt ~> 0.3.0
    {'github' => 'crystal-community/jwt', 'version' => '~> 0.3.0'}
  • migrate ~> 0.2.0
    {'github' => 'vladfaust/migrate.cr', 'version' => '~> 0.2.0'}
  • pg ~> 0.15.0
    {'github' => 'will/crystal-pg', 'version' => '~> 0.15.0'}
  • onyx-http ~> 0.2.0
    {'github' => 'vladfaust/prism', 'version' => '~> 0.2.0'}

Development Dependencies 1

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

Dependents 0

Last synced .
search fire star recently