fsxNet Wiki

BBS Development & Resources

User Tools

Site Tools


tutorials:crystal_bbs:part_one

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorials:crystal_bbs:part_one [2017/03/19 22:39]
sardaukar
tutorials:crystal_bbs:part_one [2018/03/29 01:58]
Line 1: Line 1:
-===== Crystal BBS - Part One ===== 
  
-==== Getting the ball rolling ==== 
- 
-OK, so let's start with some actual BBS programming! I've chosen Crystal for this tutorial, so you'll need to install it. At the moment this is being written, the current version is 0.21.1. As the language is still in alpha, a lot may break until it reaches 1.0 (scheduled for [[https://​crystal-lang.org/​2016/​12/​29/​crystal-new-year-resolutions-for-2017-1-0.html|later in 2017]]) and I'll try to keep this updated. 
- 
-I won't cover installation,​ since the [[https://​crystal-lang.org/​docs/​installation/​index.html|official docs]] are pretty good at that. You will need a macOS or Linux setup, either native or in a virtual machine. 
- 
-Once you have it installed, it's time to get going. Crystal has a "​skeleton"​ too built-in that creates an initial app and creates a local Git repository as well. Git is not part of the tutorial, but you should always keep your code in check with a source code management system, so have a look at it when you can. 
- 
-To create the initial skeleton, type 
- 
-<​code>​ 
-crystal init app bbs 
-</​code>​ 
- 
-This will create the skeleton inside the ''​bbs''​ subfolder of the current directory. Jump into it and have a look around at the files created. ​ 
- 
-One thing the app creator leaves out is an easy way to compile the project - we //can// just type ''​crystal build src/bbs.cr --release -o bbs_release''​ every time we want to compile, but that would get old really quick. So instead we'll create a ''​Makefile''​ and use good old //make// to perform all these repetitive tasks on the project. Here's how it could look: 
- 
-<code make> 
-.PHONY: clean 
- 
-run: bbs 
- ./​bbs_release 
-clean: 
- rm -f bbs_release 
- 
-all: clean 
- crystal build src/bbs.cr --release -o bbs_release 
- 
-bbs: 
- rm -f bbs_release 
- crystal build src/bbs.cr --release -o bbs_release 
-</​code>​ 
- 
-Make is also out of scope for this tutorial, but it won't get more complicated than this. From now on, just type ''​make''​ in the project'​s root directory to erase the existing binary and compile a new one. If you type it now, you should see: 
- 
-<​code>​ 
-$ make 
-rm -f bbs_release 
-crystal build src/bbs.cr --release -o bbs_release 
-./​bbs_release 
-</​code>​ 
- 
-That last line was our binary running, but of course it doesn'​t do anything yet! 
- 
-Let's get the basics out of the way before we go into the meat of the problem. We want to store configuration settings, so let's do that first, and then move on to the main network loop. 
- 
-Your ''​src/​bbs.cr''​ file should look like this now: 
- 
-<​code>​ 
-require "​./​bbs/​*"​ 
- 
-module Bbs 
-  # TODO Put your code here 
-end 
-</​code>​ 
-  
- 
-Change it to: 
- 
-<​code>​ 
-require "​./​bbs/​*"​ 
- 
-BBS::​Main.new.go! 
-</​code>​ 
- 
-This will call a ''​go!''​ method on the BBS module'​s ''​Main''​ class. Let's create it, but on a separate file. Create a ''​main.cr''​ file inside the ''​src''​ directory with: 
- 
-<​code>​ 
-require "​yaml"​ 
- 
-module BBS 
-  class Main 
-    @config = YAML::Any 
- 
-    def initialize 
-      load_config 
-    end 
- 
-    def go! 
-      puts config.inspect 
-    end 
- 
-    private getter :config 
- 
-    private def load_config 
-      @config = YAML.parse(File.read("​config.yml"​)) 
-    end 
-  end 
-end 
-</​code>​ 
- 
-This file will be required by ''​src/​bbs.cr''​ (as mandated by its line 1) and will add the method to the BBS module. Let's create a basic config file as ''​config.yml''​ on the root directory of the project (more info on YAML [[https://​en.wikipedia.org/​wiki/​YAML|here]]:​ 
- 
-<code yaml> 
-settings: 
-  port: 2023 
-</​code>​ 
- 
-If you type ''​make''​ now, you should see: 
- 
-<​code>​ 
-$ make 
-rm -f bbs_release 
-crystal build src/bbs.cr --release -o bbs_release 
-./​bbs_release 
-{"​settings"​ => {"​port"​ => "​2023"​}} 
-</​code>​ 
- 
-Yay, we're live with the basics and our settings are being read! Let's recap for a bit, since this class introduces a few important concepts. ​ 
tutorials/crystal_bbs/part_one.txt ยท Last modified: 2018/03/29 01:58 (external edit)