Built in function for statics from dir
This commit is contained in:
parent
7df28f4f30
commit
5231da57a3
1
TODO
1
TODO
@ -1,3 +1,4 @@
|
|||||||
Catch exceptions and return as 500 errors
|
Catch exceptions and return as 500 errors
|
||||||
approot
|
approot
|
||||||
Request parameters without a request object?
|
Request parameters without a request object?
|
||||||
|
More checking on parameters (minimum length etc)
|
||||||
|
|||||||
@ -16,14 +16,26 @@
|
|||||||
module Web.Restful.Helpers.Static
|
module Web.Restful.Helpers.Static
|
||||||
( serveStatic
|
( serveStatic
|
||||||
, FileLookup
|
, FileLookup
|
||||||
|
, fileLookupDir
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
|
import System.Directory (doesFileExist)
|
||||||
|
import Control.Applicative ((<$>))
|
||||||
|
|
||||||
import Web.Restful
|
import Web.Restful
|
||||||
|
|
||||||
type FileLookup = FilePath -> IO (Maybe B.ByteString)
|
type FileLookup = FilePath -> IO (Maybe B.ByteString)
|
||||||
|
|
||||||
|
-- | A 'FileLookup' for files in a directory.
|
||||||
|
fileLookupDir :: FilePath -> FileLookup
|
||||||
|
fileLookupDir dir fp = do
|
||||||
|
let fp' = dir ++ '/' : fp -- FIXME incredibly insecure...
|
||||||
|
exists <- doesFileExist fp'
|
||||||
|
if exists
|
||||||
|
then Just <$> B.readFile fp'
|
||||||
|
else return Nothing
|
||||||
|
|
||||||
serveStatic :: FileLookup -> Verb -> Handler
|
serveStatic :: FileLookup -> Verb -> Handler
|
||||||
serveStatic fl Get = getStatic fl
|
serveStatic fl Get = getStatic fl
|
||||||
serveStatic _ _ = notFound
|
serveStatic _ _ = notFound
|
||||||
|
|||||||
@ -36,7 +36,8 @@ library
|
|||||||
test-framework-hunit,
|
test-framework-hunit,
|
||||||
HUnit,
|
HUnit,
|
||||||
QuickCheck == 1.*,
|
QuickCheck == 1.*,
|
||||||
enumerable >= 0.0.3
|
enumerable >= 0.0.3,
|
||||||
|
directory >= 1
|
||||||
exposed-modules: Web.Restful,
|
exposed-modules: Web.Restful,
|
||||||
Web.Restful.Constants,
|
Web.Restful.Constants,
|
||||||
Web.Restful.Request,
|
Web.Restful.Request,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user