Add support for /log/ API
This commit is contained in:
36
src/Main.hs
36
src/Main.hs
@@ -251,7 +251,7 @@ makeApplication ApplicationOptions{..} request respond = do
|
||||
Nothing -> noSuchPath
|
||||
Just storePath -> return storePath
|
||||
|
||||
PathInfo{ narSize, narHash } <- liftIO (Nix.queryPathInfo storePath)
|
||||
PathInfo{ narHash } <- liftIO (Nix.queryPathInfo storePath)
|
||||
|
||||
Monad.unless (all (narHash ==) maybeExpectedNarHash) do
|
||||
let headers = [ ("Content-Type", "text/plain") ]
|
||||
@@ -273,21 +273,31 @@ makeApplication ApplicationOptions{..} request respond = do
|
||||
Nothing -> noSuchPath
|
||||
Just bytes -> return bytes
|
||||
|
||||
let contentLength =
|
||||
( ByteString.Lazy.toStrict
|
||||
. Builder.toLazyByteString
|
||||
. Builder.word64Dec
|
||||
) narSize
|
||||
let lazyBytes = ByteString.Lazy.fromStrict bytes
|
||||
|
||||
let headers =
|
||||
[ ("Content-Type", "text/plain")
|
||||
, ("Content-Length", contentLength)
|
||||
]
|
||||
|
||||
let builder = Builder.byteString bytes
|
||||
let headers = [ ("Content-Type", "text/plain") ]
|
||||
|
||||
let response =
|
||||
Wai.responseBuilder Types.status200 headers builder
|
||||
Wai.responseLBS Types.status200 headers lazyBytes
|
||||
|
||||
done response
|
||||
|
||||
| Just suffix <- ByteString.stripPrefix "/log/" rawPath
|
||||
, 32 <= ByteString.length suffix -> do
|
||||
let hashPart = ByteString.take 32 suffix
|
||||
|
||||
maybeBytes <- liftIO (Nix.dumpLog hashPart)
|
||||
|
||||
bytes <- case maybeBytes of
|
||||
Nothing -> noSuchPath
|
||||
Just bytes -> return bytes
|
||||
|
||||
let lazyBytes = ByteString.Lazy.fromStrict bytes
|
||||
|
||||
let headers = [ ("Content-Type", "text/plain") ]
|
||||
|
||||
let response =
|
||||
Wai.responseLBS Types.status200 headers lazyBytes
|
||||
|
||||
done response
|
||||
|
||||
|
||||
15
src/Nix.hsc
15
src/Nix.hsc
@@ -275,3 +275,18 @@ dumpPath hashPart = do
|
||||
if data_ == Foreign.nullPtr
|
||||
then return Nothing
|
||||
else fmap Just (fromString_ string_)
|
||||
|
||||
foreign import ccall "dumpLog" dumpLog_
|
||||
:: CString -> Ptr String_ -> IO ()
|
||||
|
||||
dumpLog :: ByteString -> IO (Maybe ByteString)
|
||||
dumpLog hashPart = do
|
||||
ByteString.useAsCString hashPart \cHashPart -> do
|
||||
Foreign.alloca \output -> do
|
||||
let open = dumpLog_ cHashPart output
|
||||
let close = freeString output
|
||||
Exception.bracket_ open close do
|
||||
string_@String_{ data_} <- peek output
|
||||
if data_ == Foreign.nullPtr
|
||||
then return Nothing
|
||||
else fmap Just (fromString_ string_)
|
||||
|
||||
Reference in New Issue
Block a user