Kemal routing redirecting a subtree

This code snippet shows you how to redirect an entire subtree and remove the first part of the subtree. This might come in useful, for example when you have outside links referencing different languages (e.g. /de /fr /it) and content paths in them, and you want to serve everything from the same endpoints. Crystal Lang…

Weiterlesen

Dealing with NIL in JSON::Any in Crystal Lang

One of the challenges when developing with Crystal is that it is not as forgiving about data types as, say, Python or Ruby. nil will keep you occupied. The following error message: cast from Nil to String failed, at /usr/share/crystal/src/json/any.cr:220:5:220 can be solved by: # cast to string, THEN check if it is nil if…

Weiterlesen

mongo_orm custom name for collection

mongo_orm will automatically determine the name for your collection by using the Module and Class name, and appending an “s”. In cases where you want to name your collection, you can simply do this: class PapiTest < Mongo::ORM::Document     collection_name “Buster”     field test : String end get “/” do     pt = PapiTest.new     pt.test = “I want…

Weiterlesen

Instructions for adding a user to a mongo database for mongo_orm

You don’t want your web application to access the database using your database admin user. Additionally you want it to access only one specific database – maybe you have additional databases running in your mongo instance. Prerequisites: mongo_orm expects the database configuration as environment variables or as a file config/database.yml in your project. The config/database.yml…

Weiterlesen

Using modules in crystal

As your application grows, you would like to move from one big .cr file to separating your code into smaller files. You can use require from your main .cr file to add code from other files: require “./tools/*” This statement will add code from the tools directory, relative to your main .cr file. This will…

Weiterlesen

Install Crystal on Docker: how to add the Crystal repository to Docker in the Dockerfile

Dockerfile (excerpt) ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn RUN apt-get update && apt-get install -y \          software-properties-common \          build-essential \         libevent-dev \         libssl-dev \         libxml2-dev \         libyaml-dev \         libgmp-dev \         libreadline-dev \         apt-transport-https \         iputils-ping \         git \         aptitude \         nano \         openssh-server \         && apt-key adv –keyserver keys.gnupg.net –recv-keys 09617FD37CC06B54 \         && add-apt-repository ‘deb https://dist.crystal-lang.org/apt crystal…

Weiterlesen

Crystal & Alpine on the Raspberry Pi

Bringing together two interesting technologies: Alpine Linux – a small-size and security oriented Linux distribution Crystal – a compiled language with a nice syntax (similar to Ruby) on the Raspberry Pi (armhf / ARMv6, which would also support the Pi Zero W) is not possible currently, as of 21.12.2018. There is no (at least no…

Weiterlesen