changeset 394:9784f19f7e1b lua-scripting

path relative to configuration path, list of lua scripts
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 May 2013 11:02:15 +0200
parents 7035f4a5b07b
children 941ea46e9e26
files OrthancServer/OrthancInitialization.cpp OrthancServer/OrthancInitialization.h OrthancServer/ServerContext.cpp OrthancServer/main.cpp Resources/Configuration.json Resources/Toolbox.lua
diffstat 6 files changed, 68 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancInitialization.cpp	Tue Apr 30 15:41:07 2013 +0200
+++ b/OrthancServer/OrthancInitialization.cpp	Thu May 02 11:02:15 2013 +0200
@@ -47,6 +47,7 @@
 
   static boost::mutex globalMutex_;
   static std::auto_ptr<Json::Value> configuration_;
+  static boost::filesystem::path defaultDirectory_;
 
 
   static void ReadGlobalConfiguration(const char* configurationFile)
@@ -58,6 +59,7 @@
     if (configurationFile)
     {
       Toolbox::ReadFile(content, configurationFile);
+      defaultDirectory_ = boost::filesystem::path(configurationFile).parent_path();
       LOG(INFO) << "Using the configuration from: " << configurationFile;
     }
     else
@@ -119,6 +121,7 @@
   void OrthancInitialize(const char* configurationFile)
   {
     boost::mutex::scoped_lock lock(globalMutex_);
+    defaultDirectory_ = boost::filesystem::current_path();
     ReadGlobalConfiguration(configurationFile);
     curl_global_init(CURL_GLOBAL_ALL);
   }
@@ -275,4 +278,37 @@
       httpServer.RegisterUser(username.c_str(), password.c_str());
     }
   }
+
+
+  std::string InterpretStringParameterAsPath(const std::string& parameter)
+  {
+    boost::mutex::scoped_lock lock(globalMutex_);
+    return (defaultDirectory_ / parameter).string();
+  }
+
+
+  void GetGlobalListOfStringsParameter(std::list<std::string>& target,
+                                       const std::string& key)
+  {
+    boost::mutex::scoped_lock lock(globalMutex_);
+
+    target.clear();
+  
+    if (!configuration_->isMember(key))
+    {
+      return;
+    }
+
+    const Json::Value& lst = (*configuration_) [key];
+
+    if (lst.type() != Json::arrayValue)
+    {
+      throw OrthancException("Badly formatted list of strings");
+    }
+
+    for (Json::Value::ArrayIndex i = 0; i < lst.size(); i++)
+    {
+      target.push_back(lst[i].asString());
+    }    
+  }
 }
--- a/OrthancServer/OrthancInitialization.h	Tue Apr 30 15:41:07 2013 +0200
+++ b/OrthancServer/OrthancInitialization.h	Thu May 02 11:02:15 2013 +0200
@@ -61,4 +61,9 @@
   void GetListOfDicomModalities(std::set<std::string>& target);
 
   void SetupRegisteredUsers(MongooseServer& httpServer);
+
+  std::string InterpretStringParameterAsPath(const std::string& parameter);
+
+  void GetGlobalListOfStringsParameter(std::list<std::string>& target,
+                                       const std::string& key);
 }
--- a/OrthancServer/ServerContext.cpp	Tue Apr 30 15:41:07 2013 +0200
+++ b/OrthancServer/ServerContext.cpp	Thu May 02 11:02:15 2013 +0200
@@ -35,6 +35,7 @@
 #include "../Core/HttpServer/FilesystemHttpSender.h"
 
 #include <glog/logging.h>
+#include <EmbeddedResources.h>
 
 #define ENABLE_DICOM_CACHE  1
 
@@ -60,6 +61,7 @@
     provider_(*this),
     dicomCache_(provider_, DICOM_CACHE_SIZE)
   {
+    lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
   }
 
   void ServerContext::SetCompressionEnabled(bool enabled)
--- a/OrthancServer/main.cpp	Tue Apr 30 15:41:07 2013 +0200
+++ b/OrthancServer/main.cpp	Thu May 02 11:02:15 2013 +0200
@@ -222,8 +222,10 @@
       OrthancInitialize();
     }
 
-    boost::filesystem::path storageDirectory = GetGlobalStringParameter("StorageDirectory", "OrthancStorage");
-    boost::filesystem::path indexDirectory = GetGlobalStringParameter("IndexDirectory", storageDirectory.string());
+    boost::filesystem::path storageDirectory = 
+      InterpretStringParameterAsPath(GetGlobalStringParameter("StorageDirectory", "OrthancStorage"));
+    boost::filesystem::path indexDirectory = 
+      InterpretStringParameterAsPath(GetGlobalStringParameter("IndexDirectory", storageDirectory.string()));
     ServerContext context(storageDirectory, indexDirectory);
 
     LOG(WARNING) << "Storage directory: " << storageDirectory;
@@ -231,16 +233,16 @@
 
     context.SetCompressionEnabled(GetGlobalBoolParameter("StorageCompression", false));
 
+    std::list<std::string> luaScripts;
+    GetGlobalListOfStringsParameter(luaScripts, "LuaScripts");
+    for (std::list<std::string>::const_iterator
+           it = luaScripts.begin(); it != luaScripts.end(); it++)
     {
-      std::string path = GetGlobalStringParameter("Scripting", "");
-      if (path.size() > 0)
-      {
-        LOG(WARNING) << "Installing the Lua scripts from: " << path;
-        std::string lua;
-        Toolbox::ReadFile(lua, path);
-        context.GetLuaContext().Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
-        context.GetLuaContext().Execute(lua);
-      }
+      std::string path = InterpretStringParameterAsPath(*it);
+      LOG(WARNING) << "Installing the Lua scripts from: " << path;
+      std::string script;
+      Toolbox::ReadFile(script, path);
+      context.GetLuaContext().Execute(script);
     }
 
 
@@ -283,7 +285,8 @@
 
       if (GetGlobalBoolParameter("SslEnabled", false))
       {
-        std::string certificate = GetGlobalStringParameter("SslCertificate", "certificate.pem");
+        std::string certificate = 
+          InterpretStringParameterAsPath(GetGlobalStringParameter("SslCertificate", "certificate.pem"));
         httpServer.SetSslEnabled(true);
         httpServer.SetSslCertificate(certificate.c_str());
       }
--- a/Resources/Configuration.json	Tue Apr 30 15:41:07 2013 +0200
+++ b/Resources/Configuration.json	Thu May 02 11:02:15 2013 +0200
@@ -28,9 +28,11 @@
     // of patients)
     "MaximumPatientCount" : 0,
   
-    // Path to the custom Lua scripts for this instance of Orthanc (an
-    // empty string denotes the absence of custom scripts)
-    "Scripting" : "",
+    // List of paths to the custom Lua scripts to load into this
+    // instance of Orthanc
+    "LuaScripts" : [
+    ],
+
 
 
     /**
--- a/Resources/Toolbox.lua	Tue Apr 30 15:41:07 2013 +0200
+++ b/Resources/Toolbox.lua	Thu May 02 11:02:15 2013 +0200
@@ -1,17 +1,18 @@
---[[ rPrint(struct, [limit], [indent])   Recursively print arbitrary data. 
+--[[ printRecursive(struct, [limit], [indent])   Recursively print arbitrary data. 
 Set limit (default 100) to stanch infinite loops.
 Indents tables as [KEY] VALUE, nested tables as [KEY] [KEY]...[KEY] VALUE
 Set indent ("") to prefix each line:    Mytable [KEY] [KEY]...[KEY] VALUE
-https://gist.github.com/stuby/5445834#file-rprint-lua
+Source: https://gist.github.com/stuby/5445834#file-rprint-lua
 --]]
-function rPrint(s, l, i) -- recursive Print (structure, limit, indent)
+
+function printRecursive(s, l, i) -- recursive Print (structure, limit, indent)
    l = (l) or 100; i = i or "";	-- default item limit, indent string
    if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
    local ts = type(s);
    if (ts ~= "table") then print (i,ts,s); return l-1 end
    print (i,ts);           -- print "table"
    for k,v in pairs(s) do  -- print "[KEY] VALUE"
-      l = rPrint(v, l, i.."\t["..tostring(k).."]");
+      l = printRecursive(v, l, i.."\t["..tostring(k).."]");
       if (l < 0) then break end
    end
    return l