From e8c2391f8e949024a5e31314be6e963cc90ba552 Mon Sep 17 00:00:00 2001 From: Matthias Hecker Date: Sat, 4 Jul 2015 21:24:01 +0200 Subject: [PATCH] webservice: add erb template render methods --- lib/rbot/core/webservice.rb | 32 +++++++++++++++++++++++++++++++- lib/rbot/plugins.rb | 3 +++ 2 files changed, 34 insertions(+), 1 deletion(-) 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{^(?: -- 2.39.2