Skip to content

Commit

Permalink
decided to switch to a simpler http API interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nevrome committed Jan 6, 2025
1 parent 60e54d9 commit 650fc2b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
29 changes: 24 additions & 5 deletions src/Poseidon/CLI/Serve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ type ArchiveStore a = [(ArchiveName, a)] -- a generic lookup table from an archi
getArchiveNames :: ArchiveStore a -> [String]
getArchiveNames = map fst

Check warning on line 75 in src/Poseidon/CLI/Serve.hs

View check run for this annotation

Codecov / codecov/patch

src/Poseidon/CLI/Serve.hs#L75

Added line #L75 was not covered by tests

getArchiveByName :: ArchiveName -> ArchiveStore a -> Maybe a
getArchiveByName name store =
case filter (\(n, _) -> n == name) store of
[] -> Nothing
[(_,a)] -> Just a
_ -> Nothing

Check warning on line 82 in src/Poseidon/CLI/Serve.hs

View check run for this annotation

Codecov / codecov/patch

src/Poseidon/CLI/Serve.hs#L78-L82

Added lines #L78 - L82 were not covered by tests

runServerMainThread :: ServeOptions -> PoseidonIO ()
runServerMainThread opts = do
-- the MVar is used as a signal from the server to the calling thread that it is ready.
Expand Down Expand Up @@ -165,13 +172,25 @@ runServer (ServeOptions archBaseDirs maybeZipPath port ignoreChecksums certFiles

-- landing page
get "/" $ do
pacs <- getItemFromArchiveStore archiveStore
currentArchiveName <- param "archive" `rescue` const (return $ head archiveNames)
mainPage currentArchiveName archiveNames pacs
logRequest logA
mainPage archiveNames

Check warning on line 176 in src/Poseidon/CLI/Serve.hs

View check run for this annotation

Codecov / codecov/patch

src/Poseidon/CLI/Serve.hs#L175-L176

Added lines #L175 - L176 were not covered by tests
-- archive pages
get "/:archive_name" $ do
logRequest logA
archiveName <- param "archive_name"
let maybePacs = getArchiveByName archiveName archiveStore
pacs <- case maybePacs of
Nothing -> raise $ "Archive " <> pack archiveName <> "does not exist"
Just p -> return p
archivePage archiveName pacs

Check warning on line 185 in src/Poseidon/CLI/Serve.hs

View check run for this annotation

Codecov / codecov/patch

src/Poseidon/CLI/Serve.hs#L179-L185

Added lines #L179 - L185 were not covered by tests
-- per package pages
get "/package/:package_name" $ do
get "/:archive_name/:package_name" $ do
logRequest logA
pacs <- getItemFromArchiveStore archiveStore
archiveName <- param "archive_name"
let maybePacs = getArchiveByName archiveName archiveStore
pacs <- case maybePacs of
Nothing -> raise $ "Archive " <> pack archiveName <> "does not exist"
Just p -> return p
packageName <- param "package_name"
maybeVersionString <- (Just <$> param "package_version") `rescue` (\_ -> return Nothing)
maybeVersion <- case maybeVersionString of
Expand Down
46 changes: 25 additions & 21 deletions src/Poseidon/ServerHTML.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}

module Poseidon.ServerHTML (mainPage, packagePage) where
module Poseidon.ServerHTML (mainPage, archivePage, packagePage) where

import Poseidon.Package
import Poseidon.EntityTypes
Expand All @@ -25,31 +25,35 @@ jscript = [text|

|]

mainPage :: String -> [String] -> [PoseidonPackage] -> S.ActionM ()
mainPage currentArchiveName archiveNames pacs = S.html $ renderMarkup $ do
mainPage :: [String] -> S.ActionM ()
mainPage archiveNames = S.html $ renderMarkup $ do
H.html $ do
H.head $ do
H.script ! A.type_ "text/javascript" $ H.text jscript
H.body $ do
H.form ! A.action "/" ! A.method "get" $ do
H.select ! A.name "archive" ! A.onchange "this.form.submit()" $ do
mapM_ (\archiveName ->
let isSelected = if archiveName == currentArchiveName
then H.option ! A.selected "selected"
else H.option
in isSelected $ H.string archiveName
) archiveNames
H.h1 "Poseidon packages"
H.h1 "Poseidon public archives"
H.ul $ mapM_ (\archiveName -> H.li $ H.div $ do
H.a ! A.href ("/" <> H.toValue archiveName) $
H.toMarkup archiveName
) archiveNames

Check warning on line 38 in src/Poseidon/ServerHTML.hs

View check run for this annotation

Codecov / codecov/patch

src/Poseidon/ServerHTML.hs#L29-L38

Added lines #L29 - L38 were not covered by tests

archivePage :: String -> [PoseidonPackage] -> S.ActionM ()
archivePage archiveName pacs = S.html $ renderMarkup $ do
H.html $ do
H.head $ do
H.script ! A.type_ "text/javascript" $ H.text jscript
H.body $ do
H.h1 $ H.toMarkup archiveName
H.ul $ mapM_ (\pac -> H.li $ H.div $ do
let pacName = getPacName pac
pacVersion = getPacVersion pac
pacNameAndVersion = renderNameWithVersion $ posPacNameAndVersion pac
H.a ! A.href ("/package/" <> H.toValue pacName <> "?package_version=" <> renderMaybeVersion pacVersion) $
H.toMarkup pacNameAndVersion
H.toMarkup (" | " :: String)
H.a ! A.href ("/zip_file/" <> H.toValue pacName <> "?package_version=" <> renderMaybeVersion pacVersion) $
H.toMarkup ("Download" :: String)
) pacs
let pacName = getPacName pac
pacVersion = getPacVersion pac
pacNameAndVersion = renderNameWithVersion $ posPacNameAndVersion pac
H.a ! A.href ("/" <> H.toValue archiveName <> "/" <> H.toValue pacName <> "?package_version=" <> renderMaybeVersion pacVersion) $
H.toMarkup pacNameAndVersion
H.toMarkup (" | " :: String)
H.a ! A.href ("/zip_file/" <> H.toValue pacName <> "?package_version=" <> renderMaybeVersion pacVersion) $
H.toMarkup ("Download" :: String)
) pacs

Check warning on line 56 in src/Poseidon/ServerHTML.hs

View check run for this annotation

Codecov / codecov/patch

src/Poseidon/ServerHTML.hs#L41-L56

Added lines #L41 - L56 were not covered by tests

packagePage :: PoseidonPackage -> S.ActionM ()
packagePage pac = S.html $ renderMarkup $ do
Expand Down

0 comments on commit 650fc2b

Please sign in to comment.