module Muzu

Constants

VERSION

Public Class Methods

action_dispatcher( request ) click to toggle source
# File lib/muzu.rb, line 25
def self.action_dispatcher( request )
  splited_request = request.split(" ")
  request_path = splited_request[1]
  request_type = splited_request[0]

  if request_path.include? '.'
    Muzu::output_file( request_path )
  else
    Muzu::output_template( request_path )
  end
end
init() click to toggle source
# File lib/muzu.rb, line 10
def self.init
  $cfg = YAML::load(File.open(Dir.pwd+'/config/general.yml'))

  if $cfg['server']['template'] == 'haml'; require 'haml'; end
end
init_database() click to toggle source
# File lib/muzu.rb, line 20
def self.init_database
  $db = ActiveRecord::Base.establish_connection($cfg['database'])
  $db_admin = ActiveRecord::Base.establish_connection( $cfg['database'].merge({'database' => 'postgres', 'schema_search_path' => 'public'}) )
end
init_server() click to toggle source
# File lib/muzu.rb, line 16
def self.init_server
  $server = TCPServer.new($cfg['server']['host'], $cfg['server']['port'])
end
output_file( path ) click to toggle source
# File lib/muzu.rb, line 37
def self.output_file( path )
  puts "file "+path
  File.read( path )
end
output_template( path ) click to toggle source
# File lib/muzu.rb, line 42
def self.output_template( path )
  puts "template "+path
  puts path
end
serve() click to toggle source
# File lib/muzu.rb, line 47
def self.serve
  Muzu::init

  Muzu::init_server
  Muzu::init_database

  puts "starting server on http://#{$cfg['server']['host']}:#{$cfg['server']['port']}"

      loop do
    Thread.start($server.accept) do |client|

      Muzu::action_dispatcher( client.gets )

      response = "Hello World!\n"
      client.print "HTTP/1.1 200 OK\r\n" +
               "Content-Type: text/plain\r\n" +
               "Content-Length: #{response.bytesize}\r\n" +
               "Connection: close\r\n"

      client.print "\r\n"
      client.print response

      client.close
    end
  end
end