mirror of https://github.com/voidlizard/hbs2
wip
This commit is contained in:
parent
130444181f
commit
d962b46786
|
@ -9,6 +9,9 @@ import HBS2.Data.Detect
|
||||||
import HBS2.Data.Types.Refs
|
import HBS2.Data.Types.Refs
|
||||||
import HBS2.Merkle
|
import HBS2.Merkle
|
||||||
import HBS2.Peer.Proto
|
import HBS2.Peer.Proto
|
||||||
|
import HBS2.Peer.Proto.LWWRef
|
||||||
|
import HBS2.Net.Auth.Schema
|
||||||
|
import HBS2.Data.Types.SignedBox
|
||||||
import HBS2.Events
|
import HBS2.Events
|
||||||
import HBS2.Storage.Operations.ByteString
|
import HBS2.Storage.Operations.ByteString
|
||||||
|
|
||||||
|
@ -24,8 +27,6 @@ import Network.Wai.Middleware.RequestLogger
|
||||||
import Text.InterpolatedString.Perl6 (qc)
|
import Text.InterpolatedString.Perl6 (qc)
|
||||||
import Web.Scotty
|
import Web.Scotty
|
||||||
|
|
||||||
import Network.Wai (responseStream)
|
|
||||||
import Network.Wai.Internal (Response(..))
|
|
||||||
import Data.ByteString.Builder (byteString, Builder)
|
import Data.ByteString.Builder (byteString, Builder)
|
||||||
|
|
||||||
import Data.Either
|
import Data.Either
|
||||||
|
@ -48,6 +49,9 @@ extractMetadataHash what blob =
|
||||||
MerkleAnn (MTreeAnn {_mtaMeta = AnnHashRef h, _mtaCrypt = NullEncryption}) -> Just h
|
MerkleAnn (MTreeAnn {_mtaMeta = AnnHashRef h, _mtaCrypt = NullEncryption}) -> Just h
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
|
orElse :: m r -> Maybe a -> ContT r m a
|
||||||
|
orElse a mb = ContT $ maybe1 mb a
|
||||||
|
|
||||||
httpWorker :: forall e s m . ( MyPeer e
|
httpWorker :: forall e s m . ( MyPeer e
|
||||||
, MonadIO m
|
, MonadIO m
|
||||||
, HasStorage m
|
, HasStorage m
|
||||||
|
@ -55,6 +59,7 @@ httpWorker :: forall e s m . ( MyPeer e
|
||||||
, s ~ Encryption e
|
, s ~ Encryption e
|
||||||
, m ~ PeerM e IO
|
, m ~ PeerM e IO
|
||||||
, e ~ L4Proto
|
, e ~ L4Proto
|
||||||
|
-- , ForLWWRefProto e
|
||||||
) => PeerConfig -> AnnMetaData -> DownloadEnv e -> m ()
|
) => PeerConfig -> AnnMetaData -> DownloadEnv e -> m ()
|
||||||
|
|
||||||
httpWorker (PeerConfig syn) pmeta e = do
|
httpWorker (PeerConfig syn) pmeta e = do
|
||||||
|
@ -76,6 +81,24 @@ httpWorker (PeerConfig syn) pmeta e = do
|
||||||
Just n -> do
|
Just n -> do
|
||||||
json n
|
json n
|
||||||
|
|
||||||
|
get "/ref/:key" do
|
||||||
|
|
||||||
|
void $ flip runContT pure do
|
||||||
|
what <- lift (param @String "key" <&> fromStringMay @(LWWRefKey HBS2Basic))
|
||||||
|
>>= orElse (status status404)
|
||||||
|
|
||||||
|
rv <- getRef sto what
|
||||||
|
>>= orElse (status status404)
|
||||||
|
>>= getBlock sto
|
||||||
|
>>= orElse (status status404)
|
||||||
|
<&> either (const Nothing) Just . deserialiseOrFail @(SignedBox (LWWRef e) e)
|
||||||
|
>>= orElse (status status404)
|
||||||
|
<&> unboxSignedBox0 @(LWWRef e)
|
||||||
|
>>= orElse (status status404)
|
||||||
|
<&> lwwValue . snd
|
||||||
|
|
||||||
|
lift $ redirect [qc|/tree/{pretty rv}|]
|
||||||
|
|
||||||
get "/tree/:hash" do
|
get "/tree/:hash" do
|
||||||
what <- param @String "hash" <&> fromString
|
what <- param @String "hash" <&> fromString
|
||||||
|
|
||||||
|
@ -83,17 +106,14 @@ httpWorker (PeerConfig syn) pmeta e = do
|
||||||
|
|
||||||
callCC $ \exit -> do
|
callCC $ \exit -> do
|
||||||
|
|
||||||
mblob <- liftIO $ getBlock sto what
|
blob <- liftIO (getBlock sto what)
|
||||||
|
>>= orElse (status status404)
|
||||||
|
|
||||||
blob <- ContT $ maybe1 mblob (status status404)
|
mh <- orElse (status status404) (extractMetadataHash what blob)
|
||||||
|
|
||||||
mh <- ContT $ maybe1 (extractMetadataHash what blob) (status status404)
|
meta <- lift (getBlock sto mh) >>= orElse (status status404)
|
||||||
|
<&> LBS8.unpack
|
||||||
metabs <- lift (getBlock sto mh)
|
<&> fromRight mempty . parseTop
|
||||||
|
|
||||||
meta <- ContT (maybe1 metabs (status status404))
|
|
||||||
<&> LBS8.unpack
|
|
||||||
<&> fromRight mempty . parseTop
|
|
||||||
|
|
||||||
let tp = headDef "application/octet-stream"
|
let tp = headDef "application/octet-stream"
|
||||||
[ show (pretty w)
|
[ show (pretty w)
|
||||||
|
@ -105,7 +125,7 @@ httpWorker (PeerConfig syn) pmeta e = do
|
||||||
| ListVal [SymbolVal "file-name:", LitStrVal w] <- meta
|
| ListVal [SymbolVal "file-name:", LitStrVal w] <- meta
|
||||||
]
|
]
|
||||||
|
|
||||||
liftIO $ print $ pretty meta
|
-- liftIO $ print $ pretty meta
|
||||||
|
|
||||||
case fn of
|
case fn of
|
||||||
Just x | takeExtension x == ".html" -> pure ()
|
Just x | takeExtension x == ".html" -> pure ()
|
||||||
|
|
Loading…
Reference in New Issue