diff options
author | Matthias Hecker <apoc@geekosphere.org> | 2015-07-04 21:24:01 +0200 |
---|---|---|
committer | Matthias Hecker <apoc@geekosphere.org> | 2015-07-04 21:24:01 +0200 |
commit | e8c2391f8e949024a5e31314be6e963cc90ba552 (patch) | |
tree | 883f27ee252618ed4345a46b76e24072aac3ccf5 | |
parent | a2eeb15faadf94c879d749e211d160cb86b659c6 (diff) |
webservice: add erb template render methods
-rw-r--r-- | lib/rbot/core/webservice.rb | 32 | ||||
-rw-r--r-- | lib/rbot/plugins.rb | 3 |
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/rbot/core/webservice.rb b/lib/rbot/core/webservice.rb index 0ddbc2d5..eb01226c 100644 --- a/lib/rbot/core/webservice.rb +++ b/lib/rbot/core/webservice.rb @@ -17,6 +17,8 @@ require 'webrick/https' require 'openssl' require 'cgi' require 'json' +require 'erb' +require 'ostruct' module ::Irc class Bot @@ -82,7 +84,10 @@ class Bot } @path = req.path - debug '@path = ' + @path.inspect + + @load_path = [File.join(Config::datadir, 'web')] + @load_path += @bot.plugins.core_module_dirs + @load_path += @bot.plugins.plugin_dirs end def parse_query(query) @@ -124,6 +129,27 @@ class Bot def send_html(body, status=200) send_response(body, status, 'text/html') end + + # Expands a relative filename to absolute using a list of load paths. + def get_load_path(filename) + @load_path.each do |path| + file = File.join(path, filename) + return file if File.exists?(file) + end + end + + # Renders a erb template and responds it + def render(template, args={}) + file = get_load_path template + if not file + raise 'template not found: ' + template + end + + tmpl = ERB.new(IO.read(file)) + ns = OpenStruct.new(args) + body = tmpl.result(ns.instance_eval { binding }) + send_html(body, 200) + end end # works similar to a message mapper but for url paths @@ -394,6 +420,10 @@ class WebServiceModule < CoreBotModule :requires_rescan => true, :desc => 'Host the web service will bind on') + Config.register Config::StringValue.new('webservice.url', + :default => 'http://127.0.0.1:7268', + :desc => 'The public URL of the web service.') + Config.register Config::BooleanValue.new('webservice.ssl', :default => false, :requires_rescan => true, diff --git a/lib/rbot/plugins.rb b/lib/rbot/plugins.rb index e40cfcc4..3b01509b 100644 --- a/lib/rbot/plugins.rb +++ b/lib/rbot/plugins.rb @@ -410,6 +410,9 @@ module Plugins attr_reader :botmodules attr_reader :maps + attr_reader :core_module_dirs + attr_reader :plugin_dirs + # This is the list of patterns commonly delegated to plugins. # A fast delegation lookup is enabled for them. DEFAULT_DELEGATE_PATTERNS = %r{^(?: |