Files

Thoth

The main Thoth namespace.

Attributes

db[R]

Public Class Methods

create(path) click to toggle source

Creates a new Thoth home directory with a sample config file at the specified path.

# File lib/thoth.rb, line 96
def create(path)
  path = File.expand_path(path)

  if File.exist?(path)
    raise "specified path already exists: #{path}"
  end

  FileUtils.mkdir_p(path/:log)
  FileUtils.mkdir(path/:media)
  FileUtils.mkdir(path/:plugin)
  FileUtils.mkdir(path/:public)
  FileUtils.mkdir(path/:view)

  FileUtils.cp(LIB_DIR/'..'/:proto/'thoth.conf.sample', path/'thoth.conf')
  File.chmod(0750, path/:log)
  File.chmod(0640, path/'thoth.conf')
end
init_ramaze() click to toggle source

Initializes Ramaze (but doesn't actually start the server).

# File lib/thoth.rb, line 115
def init_ramaze
  Ramaze::Global.setup(
    :root                 => LIB_DIR,
    :public_root          => PUBLIC_DIR,
    :view_root            => VIEW_DIR,
    :actionless_templates => false,
    :compile              => Config.server.compile_views
  )

  # Display a 404 error for requests that don't map to a controller or
  # action.
  Ramaze::Dispatcher::Error::HANDLE_ERROR.update({
    Ramaze::Error::NoAction     => [404, 'error_404'],
    Ramaze::Error::NoController => [404, 'error_404']
  })

  case trait[:mode]
  when :devel
    Ramaze::Global.benchmarking = true

  when :production
    Ramaze::Global.sourcereload = false

    # Log all errors to the error log file if one is configured.
    if Config.server.error_log.empty?
      Ramaze::Log.loggers = []
    else
      log_dir = File.dirname(Config.server.error_log)

      unless File.directory?(log_dir)
        FileUtils.mkdir_p(log_dir)
        File.chmod(0750, log_dir)
      end

      Ramaze::Log.loggers = [
        Ramaze::Logging::Logger::Informer.new(Config.server.error_log,
            [:error])
      ]
    end

    # Don't expose argument errors or exceptions in production mode.
    Ramaze::Dispatcher::Error::HANDLE_ERROR.update({
      ArgumentError => [404, 'error_404'],
      Exception     => [500, 'error_500']
    })

  else
    raise "Invalid mode: #{trait[:mode]}"
  end

  Ramaze::Global.console = trait[:irb]
end
init_thoth() click to toggle source

Opens a connection to the Thoth database and loads helpers, controllers, models and plugins.

# File lib/thoth.rb, line 170
def init_thoth
  trait[:ip]   ||= Config.server.address
  trait[:port] ||= Config.server.port

  Ramaze::Log.info "Thoth home: #{HOME_DIR}"
  Ramaze::Log.info "Thoth lib : #{LIB_DIR}"

  open_db

  unless @db.table_exists?(:posts)
    raise SchemaError, "Database schema is missing or out of date. " <<
        "Please run `thoth --migrate`."
  end

  acquire LIB_DIR/:helper/'*'
  require LIB_DIR/:controller/:post # must be loaded first
  acquire LIB_DIR/:controller/'*'
  acquire LIB_DIR/:controller/:api/'*'
  acquire LIB_DIR/:model/'*'

  # Use Erubis as the template engine for all controllers.
  Ramaze::Global.mapping.values.each do |controller|
    controller.trait[:engine] = Ramaze::Template::Erubis
  end

  # If minification is enabled, intercept CSS/JS requests and route them to
  # the MinifyController.
  if Config.server.enable_minify
    Ramaze::Rewrite[/^\/(css|js)\/(.+)$/] = '/minify/%s/%s'
  end

  Config.plugins.each {|plugin| Plugin.load(plugin)}
end
open_db() click to toggle source

Opens a Sequel database connection to the Thoth database.

# File lib/thoth.rb, line 205
def open_db
  if Config.db =~ /^sqlite:\/{3}(.+)$/
    dir = File.dirname($1)
    FileUtils.mkdir_p(dir) unless File.directory?(dir)
  end

  Sequel.datetime_class = Time

  @db = Sequel.open(Config.db)

  if trait[:sql_log]
    require 'logger'
    @db.logger = Logger.new(trait[:sql_log])
  end
end
restart() click to toggle source

Restarts the running Thoth daemon (if any).

# File lib/thoth.rb, line 222
def restart
  stop
  sleep(1)
  start
end
run() click to toggle source

Runs Thoth.

# File lib/thoth.rb, line 229
def run
  init_ramaze
  init_thoth

  Ramaze.startup(
    :force   => true,
    :adapter => trait[:adapter],
    :host    => trait[:ip],
    :port    => trait[:port]
  )
end
start() click to toggle source

Starts Thoth as a daemon.

# File lib/thoth.rb, line 242
def start
  if File.file?(trait[:pidfile])
    pid = File.read(trait[:pidfile], 20).strip
    abort("thoth already running? (pid=#{pid})")
  end

  puts "Starting thoth."

  fork do
    Process.setsid
    exit if fork

    File.open(trait[:pidfile], 'w') {|file| file << Process.pid}
    at_exit {FileUtils.rm(trait[:pidfile]) if File.exist?(trait[:pidfile])}

    Dir.chdir(HOME_DIR)
    File.umask(0000)

    STDIN.reopen('/dev/null')
    STDOUT.reopen('/dev/null', 'a')
    STDERR.reopen(STDOUT)

    run
  end
end
stop() click to toggle source

Stops the running Thoth daemon (if any).

# File lib/thoth.rb, line 269
def stop
  unless File.file?(trait[:pidfile])
    abort("thoth not running? (check #{trait[:pidfile]}).")
  end

  puts "Stopping thoth."

  pid = File.read(trait[:pidfile], 20).strip
  FileUtils.rm(trait[:pidfile]) if File.exist?(trait[:pidfile])
  pid && Process.kill('TERM', pid.to_i)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.