Compare commits

...

2 commits

Author SHA1 Message Date
19980acc0c WIP: Pagination 2022-07-13 00:04:35 -04:00
cc1ae8e395 Update Dockerfile to use the Haskell server 2022-07-12 23:23:46 -04:00
6 changed files with 53 additions and 35 deletions

View file

@ -1,3 +1,4 @@
.git
.DS_Store
.idea
*.log
@ -13,3 +14,7 @@ www/elm-stuff
www/node_modules
www/package-lock.json
www/public/elm.js
.stack-work/
test.db
tutor.db

View file

@ -1,33 +1,28 @@
FROM python:3.9.6-alpine3.14 as base
FROM haskell:9.0.2-slim as base
WORKDIR /app
FROM base as dependencies
FROM base as builder
RUN mkdir /build
WORKDIR /build
COPY stack.yaml package.yaml stack.yaml.lock /build/
RUN stack build --system-ghc --only-dependencies
ENV PIP_DEFAULT_TIMEOUT=100 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
POETRY_VERSION=1.1.4
FROM base as build
RUN apk add --no-cache gcc libffi-dev musl-dev openssl-dev rust cargo
RUN pip install "poetry==$POETRY_VERSION"
RUN python -m venv /venv
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin
COPY . .
RUN poetry build && /venv/bin/pip install dist/*.whl
COPY --from=dependencies /root/.stack /root/.stack
COPY . /build/
WORKDIR /build
RUN stack build --system-ghc
RUN mv "$(stack path --local-install-root --system-ghc)/bin" /build/bin
FROM base as frontend
RUN apk add --no-cache curl
COPY www /www
RUN curl -sL --output elm.gz https://github.com/elm/compiler/releases/download/0.19.1/binary-for-linux-64-bit.gz \
&& gunzip elm.gz \
&& chmod +x elm \
RUN curl -sL --output /bin/elm.gz https://github.com/elm/compiler/releases/download/0.19.1/binary-for-linux-64-bit.gz \
&& gunzip /bin/elm.gz \
&& chmod +x /bin/elm \
&& cd /www \
&& /app/elm make /www/src/App.elm --output /www/public/elm.js
&& /bin/elm make /www/src/App.elm --output /www/public/elm.js
FROM base as final
@ -37,9 +32,6 @@ ENV TUTOR_PORT=8888 \
TUTOR_DATABASE=/tutor.db \
TUTOR_STATIC=/www
RUN apk add sqlite
COPY --from=builder /venv /venv
COPY --from=build /build/bin /app
COPY --from=frontend /www/public /www
COPY docker-entrypoint.sh tables.sql ./
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["server"]
ENTRYPOINT ["/app/tutor-exe"]

View file

@ -27,9 +27,11 @@ import GHC.Generics (Generic)
import qualified Paths_tutor as PT
import Servant
import Servant.OpenApi (HasOpenApi (toOpenApi))
import qualified Tutor.Pagination as Pagination
import Tutor.Card
import Tutor.Config
import Tutor.Database
import Data.Maybe (fromMaybe)
data Status = Status
{ apiVersion :: T.Text,
@ -176,6 +178,8 @@ type SearchCards =
:> QueryParam' '[Description "Query string"] "q" T.Text
:> QueryParam' '[Description "Sorting method"] "sort_by" T.Text
:> QueryParam' '[Description "Search across collection or all cards"] "in_collection" T.Text
:> QueryParam' '[Description "Pagination limit"] "limit" Int
:> QueryParam' '[Description "Pagination offset"] "offset" Int
:> Get '[JSON] [Card]
type TutorAPI =
@ -206,9 +210,11 @@ openapi =
tutorServer :: Config -> Server TutorAPI
tutorServer config = status config :<|> searchCards (tutorDatabase config)
where
searchCards :: FilePath -> Maybe T.Text -> Maybe T.Text -> Maybe T.Text -> Handler [Card]
searchCards dbFile q sortBy inCollection =
liftIO $ Tutor.Database.searchCards dbFile q sortBy inCollection
searchCards :: FilePath -> Maybe T.Text -> Maybe T.Text -> Maybe T.Text -> Maybe Int -> Maybe Int -> Handler [Card]
searchCards dbFile q sortBy inCollection limit offset =
liftIO $ Pagination.items <$> Tutor.Database.searchCards dbFile q sortBy inCollection pagination
where pagination = Pagination.Pagination (fromMaybe 10 limit) (fromMaybe 0 offset)
docsServer :: Server DocsAPI
docsServer = return openapi

View file

@ -14,12 +14,13 @@ import Database.SQLite.Simple.Internal
import Database.SQLite.Simple.Ok
import Database.SQLite.Simple.QQ
import Tutor.Card
import qualified Tutor.Pagination as Pagination
import qualified Tutor.Search as Search
import Tutor.SearchParser
import Tutor.Select
( Select (..),
join,
limit,
limitOffset,
orderBy,
parameter,
selectSimple,
@ -198,14 +199,16 @@ instance FromRow Card where
}
}
searchCards :: FilePath -> Maybe T.Text -> Maybe T.Text -> Maybe T.Text -> IO [Card]
searchCards dbFile q sortBy inCollection =
searchCards :: FilePath -> Maybe T.Text -> Maybe T.Text -> Maybe T.Text -> Pagination.Pagination -> IO (Pagination.Page Card)
searchCards dbFile q sortBy inCollection pagination =
withConnection dbFile $ \conn ->
queryNamed
asPage <$> queryNamed
conn
(toQuery query)
(selectParameters query)
where
asPage results =
Pagination.Page Nothing Nothing results
baseQuery =
selectSimple
[ "cards.scryfall_id",
@ -234,7 +237,7 @@ searchCards dbFile q sortBy inCollection =
ELSE cards.color_identity END ASC|]
<> orderBy "cards.name ASC"
<> parameter ":last_update_key" ("last_update" :: T.Text)
<> limit 10
<> limitOffset (Pagination.limit pagination) (Pagination.offset pagination)
query =
mconcat $
catMaybes

12
src/Tutor/Pagination.hs Normal file
View file

@ -0,0 +1,12 @@
module Tutor.Pagination (Pagination(..), Page(..)) where
data Pagination = Pagination
{ limit :: Int
, offset :: Int
}
data Page a = Page
{ previous :: Maybe Pagination
, next :: Maybe Pagination
, items :: [a]
}

View file

@ -96,7 +96,7 @@ toQuery select =
[] -> Nothing
xs -> Just $ "ORDER BY " <> sqlJoin ", " xs
sqlLimitOffset = case (selectLimit select, selectOffset select) of
(Just l, Just o) -> Just "LIMIT :__limit, :__offset"
(Just l, Just o) -> Just "LIMIT :__limit OFFSET :__offset"
(Just l, Nothing) -> Just "LIMIT :__limit"
_ -> Nothing