-------------------------------------------------------------------------------- -- Title: WikiMainService.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: January 30, 2007 -- Legal: Copyright (C) 2007 Raphaël Szwarc -- Under the terms of the MIT License -- http://www.opensource.org/licenses/mit-license.html -------------------------------------------------------------------------------- -- import dependencies local HTTP = require( 'HTTP' ) local HTTPService = require( 'HTTPService' ) local WikiDAV = require( 'WikiDAV' ) local getmetatable = getmetatable local require = require local setmetatable = setmetatable local tostring = tostring -------------------------------------------------------------------------------- -- WikiMainService -------------------------------------------------------------------------------- module( 'WikiMainService' ) _VERSION = '1.0' local self = setmetatable( _M, {} ) local meta = getmetatable( self ) -------------------------------------------------------------------------------- -- Utitities -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -- DAV Utitities -------------------------------------------------------------------------------- local function DAVIterator() local WikiIndex = require( 'WikiIndex' ) local aCreation = WikiIndex.creation local someNames = { 'date', 'index', 'recent' } local aCount = #someNames local anIndex = 1 return function() if anIndex <= aCount then local aName = someNames[ anIndex ] local aResource = { name = aName, mode = 'directory', modification = aCreation, size = 0 } anIndex = anIndex + 1 return aResource end end end local function DAVResource() local WikiIndex = require( 'WikiIndex' ) local anIterator = DAVIterator() local aModification = WikiIndex[ 'modification' ] local aResource = { iterator = anIterator, mode = 'directory', modification = aModification, size = 0 } return aResource end -------------------------------------------------------------------------------- -- Service methods -------------------------------------------------------------------------------- function self:get() local WikiContent = require( 'WikiContent' ) local WikiContentService = require( 'WikiContentService' ) return nil, HTTPService[ WikiContentService( WikiContent( 'main' ) ) ] end -------------------------------------------------------------------------------- -- DAV service methods -------------------------------------------------------------------------------- function self:options() HTTP.response.header[ 'allow' ] = 'GET, HEAD, OPTIONS, PROPFIND' HTTP.response.header[ 'content-type' ] = 'text/plain' return HTTP.response.header[ 'allow' ] end function self:propfind() local aResource = DAVResource() return WikiDAV( aResource ):propfind() end -------------------------------------------------------------------------------- -- Metamethods -------------------------------------------------------------------------------- function meta:__call() local aService = {} setmetatable( aService, self ) self.__index = self return aService end function meta:__concat( aValue ) return tostring( self ) .. tostring( aValue ) end function meta:__tostring() return ( '%s/%s' ):format( self._NAME, self._VERSION ) end