Well, thanks for getting back to me.
I have just very few static files (for the purpose of this
discussion, say just one: main.css), and would like to serve
them, without having to put them in a directory static,
from my current directory ie.
I would then reference "main.css" with
<link href="./main.css" media="screen" type="text/css" rel="stylesheet" />
for example, not with
<link href="./static/main.css" media="screen" type="text/css" rel="stylesheet" />
Thus you may read my question as: "serve a single static file from the current
directory?". - If I have more than one static file to serve (say three), then
it doesn't really matter to me, if I have three lines of code to add (pseudo code):
serve file1.css
serve file2.pdf
serve file3.txt
or (preferrably) just one line to add: serve current directory
serve "."
I do not want to get into a discussion, whether it's a good idea to do so, or not
(I can well see the advantage of having a directory "static" for a production system),
I just want the system to behave my preferred way (...href="./main.css"), not the other
way around: having to adapt to the system's limitations (...href="./static/main.css").
For what it's worth: I have meanwhile found a solution, albeit terribly
complicated (see below), thus I wonder if there is simpler way:
Thanks again,
Andreas
--------------------
import GHC.Generics
import Network.HTTP.Media ((//))
import Network.Wai.Handler.Warp
import Servant
import Data.Typeable (Typeable)
import qualified Data.ByteString.Lazy as L
import Network.HTTP.Media ((/:))
data Css deriving Typeable
instance Accept Css where
contentType _ = "text" // "css" /: ("charset", "utf-8")
newtype RawFile = RawFile { unRawFile :: L.ByteString }
instance MimeRender Css RawFile where
mimeRender _ = unRawFile
-- It can be anywhere in your API except the last line.
type API = ...
:<|> "main.css" :> Get '[Css] RawFile
api :: Proxy API
api = Proxy
-- srv :: Handler RawHtml
srv :: Server API
srv
= ...
:<|> ( fmap RawFile (liftIO $ do L.readFile "/home/rx/work/site/main.css" ) )