6.4. 你的第一个模块

使用自定义模块,你可以做各种各样的事情

有不同类型的模块,例如

还有其他模块类型。 有关完整参考,请参阅 Roxen 1.3 程序员指南,网址为 http://caudium.info/

有关如何编写容器的示例,请参阅fnord.pike/Caudium/sources中。 因为位置模块是必须的,所以这里有另一个例子

例 6-6. 一个示例模块。

// It is intended to show a simple example of a location module.

// This module output the result of the who(1) command. It is not meant to
// be really useful since it would be better to do a <who /> tag thus
// having data in the HTML files and code here

// This variable is shown in the configuration interface as the
// version of the module.
string CVS_version = "$Id";

// Tell Caudium that this module is 'thread safe', that is, there is no
// request-specific data in global variables.
int thread_safe=1;

#include <module.h>
inherit "module";
// for the http_string_answer API
inherit "caudiumlib"; 
 
// Documentation:
 
constant module_type = MODULE_LOCATION;
constant module_name = "Who";
constant module_doc  = "Send the result of the who(1) command ";
constant module_unique = 1;

// The constructor of this module.
// This function is called each time you/Caudium load the module
void create()
{
  defvar("location", "/who", "Mount point", TYPE_LOCATION, 
  "The mount point of this module"); 
  /* each string have to be on a single
   line, don't do: "The mount point of
   this module".
   You can however do "The mount point of "
   "this module";
   */
  defvar("path2who", "/usr/bin/who", 
  "Path to the who command", TYPE_FILE);
  defvar("options2who", "-a", 
  "Options given to who", TYPE_STRING);
  defvar("codebeforewho", "<html><body><p>", 
  "The code to output before who", TYPE_STRING);
  defvar("codeafterwho", "</p></body></html>", "The code to output after who",
  TYPE_STRING);
}

// This function is called when a user access mount point
// path is the path to the URL he used
// id contains Caudium global variables such as browser name,...
mixed find_file(string path, object id)
{
  // get the contents of the CIF. variables path2who and options2who 
  // and put a single space between it.
  string command = QUERY(path2who)+" "+QUERY(options2who);
  // this will write the result of command to the debug log file
  // very useful for debug
  write(sprintf("command=%s\n", command));
  string result = Process.popen(command);
  // replacing \n by \n<br /> for better output
  result = replace(result, "\n","\n<br />");
  return http_string_answer(QUERY(codebeforewho)+result+QUERY(codeafterwho));
}
      

将此代码放在../local/modules/who.pike相对于/usr/local/caudium/server在我们的示例中。 登录到 CIF.,如果不是这种情况,请进入主操作标签 ->缓存 -> 刷新缓存。 检查模块缓存选中复选框并按下一页,然后确定.

返回到主虚拟服务器标签,然后选择你的一个服务器。 执行添加模块并选择who模块。 如果你没有 who 模块,请检查你的事件日志。

你不需要编译即可拥有一个可工作的模块。 你甚至不需要重启 Web 服务器。 当你开发一个模块并每 30 秒更改代码时,你只需按下重新加载按钮即可获取更改。 这大约需要一秒钟,如果存在编译错误,旧副本将保留供用户使用。