aboutsummaryrefslogtreecommitdiff
path: root/src/guff/server.cr
blob: 1d87db73b6eaeedcec462244a758512c26993cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require "http/server"
require "./config"
require "./model"
require "./handlers"

module Guff
  class Server
    def self.run(model : Model)
      new(model).run
    end

    def initialize(@model : Model)
      # create server
      @server = HTTP::Server.new(
        @model.config["host"],
        @model.config["port"].to_i,
        get_handlers(@model)
      )
    end

    def run
      puts "listening on %s:%s" % %w{host port}.map { |k| @model.config[k] }
      @server.listen
    end

    private def get_handlers(model : Model) : Array(HTTP::Handler)
      @handlers ||= Handlers.get(model)
    end
  end
end