This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
tutorials:crystal_bbs:part_one [2017/03/19 23:54] sardaukar |
tutorials:crystal_bbs:part_one [2018/03/29 01:58] (current) |
||
---|---|---|---|
Line 17: | Line 17: | ||
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. | 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: | + | 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 real 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> | <code make> | ||
Line 136: | Line 136: | ||
</code> | </code> | ||
- | This is the method called in ''bbs.cr'' (our main entry point for the whole project)_after instantiating a new instance of the ''Main'' class. At this stage, it just prints out the hash parsed from the ''config.yml'' file that we created. | + | This is the method called in ''bbs.cr'' (our main entry point for the whole project) after instantiating a new instance of the ''Main'' class. At this stage, it just prints out the hash parsed from the ''config.yml'' file that we created. |
<code ruby> | <code ruby> | ||
Line 223: | Line 223: | ||
With this in place, we need to change the type annotation for the ''@config'' instance variable. | With this in place, we need to change the type annotation for the ''@config'' instance variable. | ||
+ | |||
+ | <code ruby> | ||
+ | @config : Config | ||
+ | </code> | ||
+ | |||
+ | And the parser method needs to be adjusted too: | ||
+ | |||
+ | <code ruby> | ||
+ | private def load_config | ||
+ | Config.from_yaml(File.read("config.yml")) | ||
+ | end | ||
+ | </code> | ||
+ | |||
+ | Finally, we can access the settings in a less ugly way: | ||
+ | |||
+ | <code ruby> | ||
+ | telnet_port = @config.settings["port"] | ||
+ | </code> | ||
+ | |||
+ | Much better! This concludes part one of the tutorial, tune in soon for [[tutorials:crystal_bbs:part_two|part two]] where we will be negotiating features with the client! For reference, here's the whole code for the ''src/bbs/main.cr'' file: | ||
+ | |||
+ | <code ruby> | ||
+ | require "yaml" | ||
+ | require "socket" | ||
+ | |||
+ | module BBS | ||
+ | class Config | ||
+ | YAML.mapping( | ||
+ | settings: Hash(String, Int32) | ||
+ | ) | ||
+ | end | ||
+ | |||
+ | class Main | ||
+ | @config : Config | ||
+ | |||
+ | def initialize | ||
+ | @config = load_config | ||
+ | end | ||
+ | |||
+ | def go! | ||
+ | telnet_port = @config.settings["port"] | ||
+ | |||
+ | server = TCPServer.new("localhost", telnet_port) | ||
+ | puts "now listening in port #{telnet_port}" | ||
+ | loop do | ||
+ | if socket = server.accept? | ||
+ | spawn handle_connection(socket) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | private def handle_connection(socket) | ||
+ | socket << "hello world\n" | ||
+ | socket.close | ||
+ | end | ||
+ | |||
+ | private def load_config | ||
+ | Config.from_yaml(File.read("config.yml")) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | </code> |