{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE QuasiQuotes #-} module WhatcdResolver where import Control.Monad.Logger qualified as Logger import Control.Monad.Logger.CallStack import Control.Monad.Reader import Data.Aeson qualified as Json import Data.Aeson.BetterErrors qualified as Json import Data.Aeson.KeyMap qualified as KeyMap import Data.Error.Tree import Data.List.NonEmpty qualified as NonEmpty import Data.Map.Strict qualified as Map import Data.Pool (Pool) import Data.Pool qualified as Pool import Data.Text qualified as Text import Database.PostgreSQL.Simple (Only (..)) import Database.PostgreSQL.Simple qualified as Postgres import Database.PostgreSQL.Simple.SqlQQ (sql) import Database.PostgreSQL.Simple.Types (PGArray (PGArray)) import Database.PostgreSQL.Simple.Types qualified as Postgres import Database.Postgres.Temp qualified as TmpPg import FieldParser qualified as Field import GHC.Records (HasField (..)) import IHP.HSX.QQ (hsx) import Json qualified import Json.Enc (Enc) import Json.Enc qualified as Enc import Label import Network.HTTP.Conduit qualified as Http import Network.HTTP.Simple qualified as Http import Network.HTTP.Types import Network.HTTP.Types qualified as Http import Network.Wai qualified as Wai import Network.Wai.Handler.Warp qualified as Warp import PossehlAnalyticsPrelude import Postgres.Decoder qualified as Dec import Postgres.MonadPostgres import Pretty import RunCommand import System.Directory qualified as Dir import System.Directory qualified as Xdg import System.FilePath ((>)) import System.IO qualified as IO import Text.Blaze.Html (Html, (!)) import Text.Blaze.Html.Renderer.Utf8 qualified as Html import Text.Blaze.Html5 qualified as Html import Text.Blaze.Html5.Attributes qualified as Attr import UnliftIO htmlUi :: App () htmlUi = do withRunInIO $ \runInIO -> Warp.run 8080 $ \req resp -> do let h = resp . Wai.responseLBS Http.ok200 [] case req & Wai.pathInfo of [] -> h =<< runInIO mainHtml ["snips", "song"] -> h snipsSong _ -> h =<< runInIO mainHtml where tableData = ( [ "Group ID", "Torrent ID", "Artist", "Name", "Weight", "Torrent" ], \t -> [ Enc.int t.groupId, Enc.int t.torrentId, Enc.text t.torrentGroupJson.artist, Enc.text t.torrentGroupJson.groupName, Enc.int t.seedingWeight, Enc.value t.torrentJson ] ) mkTable :: ([Text], t -> [Enc]) -> [t] -> Html mkTable f ts = do let headers = Html.thead (fst f <&> Html.toHtml @Text <&> Html.th & mconcat) let keys = fst f <&> Text.toLower <&> Text.replace " " "_" let json = Enc.list (\t -> Enc.object (zip keys (t & snd f))) ts let tableDataScript = Html.script ! Attr.type_ "application/json" ! Attr.id "table-data" $ (json & Enc.encToBytesUtf8 & bytesToTextUtf8Unsafe & Html.text) [hsx| {tableDataScript}