10 Commits

Author SHA1 Message Date
c45ea0ecc8 wip 2018-12-20 00:33:32 +02:00
6767813879 wip 2018-12-20 00:29:25 +02:00
8818b6f495 Separate backend and frontend 2018-12-19 23:18:13 +02:00
e459a318bd Disable cache nix root 2018-11-12 22:34:58 +02:00
92e34fdfcc Use the nix-build command instead 2018-11-12 21:53:47 +02:00
26af45713c Travis build 2018-11-12 21:46:42 +02:00
5727ea5574 Docs support 2018-11-12 21:32:42 +02:00
7928aa1cb6 More tests 2018-10-26 23:59:06 +03:00
fb29a6e694 Automatic testing for schema 2018-10-26 23:47:14 +03:00
5961a99d77 Simple placeholder test 2018-10-26 22:15:11 +03:00
26 changed files with 677 additions and 204 deletions

12
.travis.yml Normal file
View File

@ -0,0 +1,12 @@
language: nix
os:
- linux
before_script:
- mkdir -m 0755 -p /nix/var/nix/{profiles,gcroots}/per-user/$USER
- mkdir -p ~/.config/nixpkgs
script:
- nix-build ./release.nix --option trusted-public-keys "masser-ebook-manager.cachix.org-1:mtFSkQ2MO5MvjUpulZoFKjKUIa8g8CTcdPVuJaPKS1w= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" --option binary-caches "https://masser-ebook-manager.cachix.org https://cache.nixos.org" -A ghc.backend -A ghc.frontend
- nix-build ./release.nix --option trusted-public-keys "masser-ebook-manager.cachix.org-1:mtFSkQ2MO5MvjUpulZoFKjKUIa8g8CTcdPVuJaPKS1w= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" --option binary-caches "https://masser-ebook-manager.cachix.org https://cache.nixos.org" -A ghcjs.frontend

View File

@ -23,6 +23,7 @@ executable backend
, API.Catalogue , API.Catalogue
, API.Channels , API.Channels
, API.Users , API.Users
, Configuration
, Database , Database
, Database.Book , Database.Book
, Database.Channel , Database.Channel
@ -68,6 +69,7 @@ executable backend
, servant , servant
, servant-auth , servant-auth
, servant-auth-server , servant-auth-server
, servant-auth-docs
, servant-docs , servant-docs
, servant-lucid , servant-lucid
, servant-multipart , servant-multipart
@ -86,3 +88,62 @@ executable backend
, NoImplicitPrelude , NoImplicitPrelude
, OverloadedStrings , OverloadedStrings
, RecordWildCards , RecordWildCards
test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs: src
build-depends: base >=4.10
, exceptions
, monad-control
, common
, aeson
, asn1-data
, asn1-types
, bytestring
, classy-prelude
, cryptonite
, dhall
, directory
, foreign-store
, generic-lens
, http-api-data
, http-media
, jose
, lens
, lucid
, memory
, monad-logger
, mtl
, pandoc
, pandoc-types
, pem
, process
, resource-pool
, selda
, selda-postgresql
, servant
, servant-auth
, servant-auth-server
, servant-docs
, servant-auth-docs
, servant-lucid
, servant-multipart
, servant-server
, text
, transformers
, wai
, warp
, x509
, x509-store
, xml-conduit
, xml-hamlet
, validity
, genvalidity-hspec
, genvalidity-property
, genvalidity-text
, hspec
default-extensions: DeriveGeneric
, NoImplicitPrelude
, OverloadedStrings
, RecordWildCards

View File

@ -1,14 +1,14 @@
{-# Language DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# Language TypeFamilies #-} {-# LANGUAGE DeriveGeneric #-}
{-# Language TypeOperators #-} {-# LANGUAGE FlexibleInstances #-}
{-# Language NoImplicitPrelude #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# Language MultiParamTypeClasses #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# Language OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# Language TemplateHaskell #-} {-# LANGUAGE QuasiQuotes #-}
{-# Language QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-}
{-# Language RecordWildCards #-} {-# LANGUAGE TemplateHaskell #-}
{-# Language DeriveGeneric #-} {-# LANGUAGE TypeFamilies #-}
{-# Language FlexibleInstances #-} {-# LANGUAGE TypeOperators #-}
module API (API, handler) where module API (API, handler) where
@ -18,23 +18,21 @@ import Types
import View import View
import qualified API.Users as Users
import qualified API.Channels as Channels
import qualified API.Books as Books import qualified API.Books as Books
import qualified API.Catalogue as Catalogue import qualified API.Catalogue as Catalogue
import qualified API.Channels as Channels
import qualified API.Users as Users
data Index = Index data Index = Index
type API = Get '[HTML] (AppView Index) type API = Users.API
:<|> Users.API
:<|> "api" :> "current" :> Channels.API :<|> "api" :> "current" :> Channels.API
:<|> "api" :> "current" :> Books.API :<|> "api" :> "current" :> Books.API
:<|> "api" :> "1" :> Catalogue.VersionedAPI 1 :<|> "api" :> "1" :> Catalogue.VersionedAPI 1
:<|> "api" :> "current" :> Catalogue.VersionedAPI 1 :<|> "api" :> "current" :> Catalogue.VersionedAPI 1
handler :: ServerT API AppM handler :: ServerT API AppM
handler = indexHandler handler = Users.handler
:<|> Users.handler
:<|> Channels.handler :<|> Channels.handler
:<|> Books.handler :<|> Books.handler
:<|> Catalogue.handler :<|> Catalogue.handler

View File

@ -1,54 +1,59 @@
{-# Language DuplicateRecordFields #-} {-# LANGUAGE DataKinds #-}
{-# Language DataKinds #-} {-# LANGUAGE DeriveGeneric #-}
{-# Language TypeFamilies #-} {-# LANGUAGE DuplicateRecordFields #-}
{-# Language TypeOperators #-} {-# LANGUAGE FlexibleInstances #-}
{-# Language NoImplicitPrelude #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# Language MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# Language OverloadedStrings #-} {-# LANGUAGE NamedFieldPuns #-}
{-# Language TemplateHaskell #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# Language QuasiQuotes #-} {-# LANGUAGE OverloadedStrings #-}
{-# Language RecordWildCards #-} {-# LANGUAGE QuasiQuotes #-}
{-# Language DeriveGeneric #-} {-# LANGUAGE RecordWildCards #-}
{-# Language FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-}
{-# Language TypeApplications #-} {-# LANGUAGE TypeApplications #-}
{-# Language DataKinds #-} {-# LANGUAGE TypeFamilies #-}
{-# Language NamedFieldPuns #-} {-# LANGUAGE TypeOperators #-}
module API.Books where module API.Books where
import ClassyPrelude import ClassyPrelude
import Control.Lens import Control.Lens
import Control.Monad.Catch (throwM, MonadThrow) import Control.Monad.Catch (MonadThrow, throwM)
import Control.Monad.Trans.Maybe
import Crypto.Hash (digestFromByteString)
import Data.Aeson import Data.Aeson
import Data.ByteArray (convert)
import Data.Generics.Product import Data.Generics.Product
import Database import Database
import Database.Book import Database.Book
import Database.Channel import Database.Channel
import Database.Tag import Database.Tag
import qualified Datastore as DS
import Servant hiding (contentType) import Servant hiding (contentType)
import Servant.Auth as SA import Servant.Auth as SA
import qualified Servant.Docs as Docs
import Server.Auth import Server.Auth
import Types import Types
import Control.Monad.Trans.Maybe
import qualified Datastore as DS
import Data.ByteArray (convert)
import Crypto.Hash (digestFromByteString)
data JsonBook = JsonBook { identifier :: BookID data JsonBook = JsonBook { identifier :: BookID
, contentType :: Text , contentType :: Text
, title :: Text , title :: Text
, description :: Maybe Text , description :: Maybe Text
, channels :: [Text] , channels :: [Text]
, tags :: [Text] } , tags :: [Text] }
deriving (Generic, Show) deriving (Generic, Show, Eq)
instance Docs.ToSample JsonBook where
toSamples _ = [("Book", JsonBook 13 "epub" "title" (Just "Description") [] [])]
instance Docs.ToSample PostBook where
toSamples _ = [("Book", PostBook "epub" "title" (Just "Description") [] [])]
data PostBook = PostBook { contentType :: Text data PostBook = PostBook { contentType :: Text
, title :: Text , title :: Text
, description :: Maybe Text , description :: Maybe Text
, channels :: [Text] , channels :: [Text]
, tags :: [Text] } , tags :: [Text] }
deriving (Generic, Show) deriving (Generic, Show, Eq)
instance ToJSON JsonBook instance ToJSON JsonBook
@ -58,13 +63,21 @@ instance FromJSON PostBook
type API = Auth '[SA.BasicAuth, SA.JWT] SafeUser :> BaseAPI type API = Auth '[SA.BasicAuth, SA.JWT] SafeUser :> BaseAPI
instance Docs.ToCapture (Capture "book_id" BookID) where
toCapture _ = Docs.DocCapture "book_id" "The book id"
type BaseAPI = "books" :> Get '[JSON] [JsonBook] type BaseAPI = "books" :> Get '[JSON] [JsonBook]
:<|> "books" :> ReqBody '[JSON] PostBook :> Post '[JSON] JsonBook :<|> "books" :> ReqBody '[JSON] PostBook :> Post '[JSON] JsonBook
:<|> "books" :> Capture "book_id" BookID :> "meta" :> ReqBody '[JSON] JsonBook :> Put '[JSON] JsonBook :<|> "books" :> Capture "book_id" BookID :> "meta" :> ReqBody '[JSON] JsonBook :> Put '[JSON] JsonBook
:<|> "books" :> Capture "book_id" BookID :> ReqBody '[OctetStream] ByteString :> Put '[JSON] NoContent :<|> "books" :> Capture "book_id" BookID :> ReqBody '[OctetStream] FileContent :> Put '[JSON] NoContent
:<|> GetBook :<|> GetBook
type GetBook = "books" :> Capture "book_id" BookID :> Get '[OctetStream] ByteString newtype FileContent = FileContent { getFileContent :: ByteString } deriving (MimeUnrender OctetStream, MimeRender OctetStream )
instance Docs.ToSample FileContent where
toSamples _ = [("File contents", FileContent "bytes here and there")]
type GetBook = "books" :> Capture "book_id" BookID :> Get '[OctetStream] FileContent
handler :: ServerT API AppM handler :: ServerT API AppM
handler user = listBooksHandler user handler user = listBooksHandler user
@ -73,12 +86,12 @@ handler user = listBooksHandler user
:<|> putBookContentHandler user :<|> putBookContentHandler user
:<|> getBookContentHandler user :<|> getBookContentHandler user
getBookContentHandler :: AuthResult SafeUser -> BookID -> AppM ByteString getBookContentHandler :: AuthResult SafeUser -> BookID -> AppM FileContent
getBookContentHandler auth bookId = requireBookOwner auth bookId $ \SafeUser{username} -> do getBookContentHandler auth bookId = requireBookOwner auth bookId $ \SafeUser{username} -> do
content <- runMaybeT $ do content <- runMaybeT $ do
Book{contentHash=mHash} <- MaybeT $ runDB (getBook bookId username) Book{contentHash=mHash} <- MaybeT $ runDB (getBook bookId username)
contentHash <- MaybeT $ return (mHash >>= digestFromByteString . unHex) contentHash <- MaybeT $ return (mHash >>= digestFromByteString . unHex)
MaybeT $ DS.get contentHash FileContent <$> MaybeT (DS.get contentHash)
maybe (throwM err404) return content maybe (throwM err404) return content
requireBookOwner :: AuthResult SafeUser -> BookID -> (SafeUser -> AppM a) -> AppM a requireBookOwner :: AuthResult SafeUser -> BookID -> (SafeUser -> AppM a) -> AppM a
@ -87,8 +100,9 @@ requireBookOwner auth bookId f = flip requireLoggedIn auth $ \u@SafeUser{usernam
unless exists $ throwM err404 unless exists $ throwM err404
runDB (isBookOwner bookId username) >>= \o -> if o then f u else throwM err403 runDB (isBookOwner bookId username) >>= \o -> if o then f u else throwM err403
putBookContentHandler :: AuthResult SafeUser -> BookID -> ByteString -> AppM NoContent putBookContentHandler :: AuthResult SafeUser -> BookID -> FileContent -> AppM NoContent
putBookContentHandler auth bookId content = requireBookOwner auth bookId $ \SafeUser{username} -> do putBookContentHandler auth bookId fc = requireBookOwner auth bookId $ \SafeUser{username} -> do
let content = getFileContent fc
key <- HashDigest . convert <$> DS.put content key <- HashDigest . convert <$> DS.put content
runDB (setContent bookId username key) runDB (setContent bookId username key)
return NoContent return NoContent

View File

@ -24,8 +24,10 @@ import qualified Database.Channel as Channel
import GHC.TypeLits import GHC.TypeLits
import Servant hiding (contentType) import Servant hiding (contentType)
import Servant.Auth as SA import Servant.Auth as SA
import qualified Servant.Docs as Docs
import Servant.XML import Servant.XML
import Server.Auth import Server.Auth
import System.IO.Unsafe (unsafePerformIO)
import Types import Types
-- This is my first try on going to versioned apis, things might change -- This is my first try on going to versioned apis, things might change
@ -40,7 +42,7 @@ newtype Rel = Rel { unRel :: Text } deriving (IsString, Show)
data Pagination = Pagination { previous :: Maybe Rel data Pagination = Pagination { previous :: Maybe Rel
, next :: Maybe Rel } , next :: Maybe Rel }
deriving (Show) deriving (Show, Generic)
newtype SubSection = SubSection Rel deriving (Show) newtype SubSection = SubSection Rel deriving (Show)
newtype Acquisition = Acquisition Rel deriving (Show) newtype Acquisition = Acquisition Rel deriving (Show)
@ -64,6 +66,20 @@ deriving instance Show (Entry 1)
deriving instance Generic (Catalog 1) deriving instance Generic (Catalog 1)
deriving instance Generic (Entry 1) deriving instance Generic (Entry 1)
instance Docs.ToSample (Entry 1) where
toSamples _ = [("Entry", EntryV1 "title" "identifier" docsTime "content" (Left (SubSection (Rel "sub"))))]
instance Docs.ToSample UTCTime where
toSamples _ = [("time", docsTime)]
instance Docs.ToSample Rel where
toSamples _ = [("Relative link", Rel "next")]
instance Docs.ToSample Pagination
instance Docs.ToSample (Catalog 1) -- where
-- toSamples _ = [("catalog", CatalogV1 docsTime (Rel "prev") (Rel "next") (Pagination (Just "previous") (Just "next")) [])]
docsTime :: UTCTime
docsTime = unsafePerformIO getCurrentTime
instance ToNode SubSection where instance ToNode SubSection where
toNode (SubSection rel) = [xml|<link type="application/atom+xml;profile=opds-catalog;kind=acquisition" rel="subsection" href="#{unRel rel}">|] toNode (SubSection rel) = [xml|<link type="application/atom+xml;profile=opds-catalog;kind=acquisition" rel="subsection" href="#{unRel rel}">|]

View File

@ -1,23 +1,22 @@
{-# Language DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# Language TypeFamilies #-} {-# LANGUAGE DeriveGeneric #-}
{-# Language TypeOperators #-} {-# LANGUAGE DuplicateRecordFields #-}
{-# Language NoImplicitPrelude #-} {-# LANGUAGE FlexibleInstances #-}
{-# Language MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# Language OverloadedStrings #-} {-# LANGUAGE NamedFieldPuns #-}
{-# Language TemplateHaskell #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# Language QuasiQuotes #-} {-# LANGUAGE OverloadedStrings #-}
{-# Language RecordWildCards #-} {-# LANGUAGE QuasiQuotes #-}
{-# Language DeriveGeneric #-} {-# LANGUAGE RecordWildCards #-}
{-# Language FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-}
{-# Language TypeApplications #-} {-# LANGUAGE TypeApplications #-}
{-# Language DataKinds #-} {-# LANGUAGE TypeFamilies #-}
{-# Language DuplicateRecordFields #-} {-# LANGUAGE TypeOperators #-}
{-# Language NamedFieldPuns #-}
module API.Channels (API, handler, JsonChannel(..)) where module API.Channels (API, handler, JsonChannel(..)) where
import ClassyPrelude import ClassyPrelude
import Control.Lens import Control.Lens
import Control.Monad.Catch (throwM, MonadThrow) import Control.Monad.Catch (MonadThrow, throwM)
import Control.Monad.Logger import Control.Monad.Logger
import Data.Aeson import Data.Aeson
import Data.Generics.Product import Data.Generics.Product
@ -25,6 +24,7 @@ import Database
import Database.Channel import Database.Channel
import Servant import Servant
import Servant.Auth as SA import Servant.Auth as SA
import qualified Servant.Docs as Docs
import Server.Auth import Server.Auth
import Types import Types
@ -36,6 +36,12 @@ data UpdateChannel = UpdateChannel { identifier :: ChannelID
, visibility :: Visibility } , visibility :: Visibility }
deriving (Show, Generic) deriving (Show, Generic)
instance Docs.ToSample JsonChannel where
toSamples _ = [("Channel", JsonChannel "channel" Private)]
instance Docs.ToSample UpdateChannel where
toSamples _ = [("Channel", UpdateChannel 13 "channel" Private)]
instance ToJSON JsonChannel instance ToJSON JsonChannel
instance FromJSON JsonChannel instance FromJSON JsonChannel
instance ToJSON UpdateChannel instance ToJSON UpdateChannel
@ -43,6 +49,9 @@ instance FromJSON UpdateChannel
type API = Auth '[SA.BasicAuth, SA.Cookie, SA.JWT] SafeUser :> BaseAPI type API = Auth '[SA.BasicAuth, SA.Cookie, SA.JWT] SafeUser :> BaseAPI
instance Docs.ToCapture (Capture "channel_id" ChannelID) where
toCapture _ = Docs.DocCapture "channel_id" "The channel id"
type BaseAPI = "channels" :> ReqBody '[JSON] JsonChannel :> Post '[JSON] UpdateChannel type BaseAPI = "channels" :> ReqBody '[JSON] JsonChannel :> Post '[JSON] UpdateChannel
:<|> "channels" :> Capture "channel_id" ChannelID :> ReqBody '[JSON] UpdateChannel :> Put '[JSON] UpdateChannel :<|> "channels" :> Capture "channel_id" ChannelID :> ReqBody '[JSON] UpdateChannel :> Put '[JSON] UpdateChannel
:<|> "channels" :> Get '[JSON] [JsonChannel] :<|> "channels" :> Get '[JSON] [JsonChannel]

View File

@ -16,6 +16,7 @@ import Database.User
import Servant import Servant
import Servant.Auth as SA import Servant.Auth as SA
import Servant.Auth.Server as SAS import Servant.Auth.Server as SAS
import qualified Servant.Docs as Docs
import Server.Auth import Server.Auth
import Types import Types
import Web.FormUrlEncoded import Web.FormUrlEncoded
@ -27,12 +28,17 @@ data RegisterForm = RegisterForm { username :: Username
, passwordAgain :: PlainPassword } , passwordAgain :: PlainPassword }
deriving (Generic, Show) deriving (Generic, Show)
instance Docs.ToSample RegisterForm
data LoginStatus = LoginStatus ( Maybe SafeUser ) deriving Generic data LoginStatus = LoginStatus ( Maybe SafeUser ) deriving Generic
data RegisterStatus = RegisterStatus deriving Generic data RegisterStatus = RegisterStatus deriving Generic
instance Docs.ToSample RegisterStatus
instance ToJSON LoginStatus instance ToJSON LoginStatus
instance FromJSON LoginStatus instance FromJSON LoginStatus
instance Docs.ToSample LoginStatus
instance FromJSON RegisterForm instance FromJSON RegisterForm
instance ToJSON RegisterForm instance ToJSON RegisterForm

View File

@ -23,3 +23,4 @@ data Config = Config { database :: Pg
instance Interpret Pg instance Interpret Pg
instance Interpret Store instance Interpret Store
instance Interpret Config instance Interpret Config

View File

@ -1,26 +1,35 @@
{-# Language NoImplicitPrelude #-} {-# LANGUAGE DeriveGeneric #-}
{-# Language DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-}
{-# Language OverloadedStrings #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# Language DuplicateRecordFields #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# Language GeneralizedNewtypeDeriving #-} {-# LANGUAGE OverloadedStrings #-}
module Database.Schema where module Database.Schema where
import ClassyPrelude import ClassyPrelude
import Database.Selda.Generic import Data.Aeson
import Database.Selda import Database.Selda
import Database.Selda.Backend import Database.Selda.Backend
import Database.Selda.Generic
import Data.Aeson import qualified Servant.Docs as Docs
import Web.HttpApiData import Web.HttpApiData
-- | User type -- | User type
newtype PlainPassword = PlainPassword Text deriving (Show, ToJSON, FromJSON, ToHttpApiData, FromHttpApiData, Eq) newtype PlainPassword = PlainPassword Text deriving (Show, ToJSON, FromJSON, ToHttpApiData, FromHttpApiData, Eq, Generic)
newtype HashedPassword = HashedPassword {unHashed :: ByteString} newtype HashedPassword = HashedPassword {unHashed :: ByteString}
data NoPassword = NoPassword data NoPassword = NoPassword
newtype Email = Email { unEmail :: Text } deriving (Show, ToJSON, FromJSON, ToHttpApiData, FromHttpApiData) newtype Email = Email { unEmail :: Text } deriving (Show, ToJSON, FromJSON, ToHttpApiData, FromHttpApiData, Generic, Eq, IsString)
newtype Username = Username { unUsername :: Text } deriving (Show, ToJSON, FromJSON, ToHttpApiData, FromHttpApiData) newtype Username = Username { unUsername :: Text } deriving (Show, ToJSON, FromJSON, ToHttpApiData, FromHttpApiData, Eq, Generic, IsString)
instance Docs.ToSample Username where
toSamples _ = [("Username", Username "user123")]
instance Docs.ToSample Email where
toSamples _ = [("Email", Email "first.last@example.com")]
instance Docs.ToSample PlainPassword where
toSamples _ = [("Password", PlainPassword "password123")]
instance SqlType HashedPassword where instance SqlType HashedPassword where
mkLit = LCustom . LBlob . unHashed mkLit = LCustom . LBlob . unHashed
@ -42,9 +51,9 @@ instance SqlType Username where
newtype UserID = UserID {unUserID :: Int} deriving (Show) newtype UserID = UserID {unUserID :: Int} deriving (Show)
newtype BookID = BookID {unBookID :: Int} deriving (Show, ToJSON, FromJSON, FromHttpApiData, Eq, Ord, ToHttpApiData) newtype BookID = BookID {unBookID :: Int} deriving (Show, ToJSON, FromJSON, FromHttpApiData, Eq, Ord, ToHttpApiData, Generic, Num)
newtype ChannelID = ChannelID {unChannelID :: Int} deriving (Show, ToHttpApiData, FromHttpApiData, ToJSON, FromJSON) newtype ChannelID = ChannelID {unChannelID :: Int} deriving (Show, ToHttpApiData, FromHttpApiData, ToJSON, FromJSON, Eq, Generic, Num)
newtype TagID = TagID {unTagID :: Int} deriving (Show) newtype TagID = TagID {unTagID :: Int} deriving (Show)
@ -77,7 +86,7 @@ data User pass = User { identifier :: UserID
, password :: pass } , password :: pass }
deriving (Show, Generic) deriving (Show, Generic)
data Role = UserRole | AdminRole deriving (Show, Read, Enum, Bounded, Typeable, Generic) data Role = UserRole | AdminRole deriving (Show, Read, Enum, Bounded, Typeable, Generic, Eq)
instance ToJSON Role instance ToJSON Role
instance FromJSON Role instance FromJSON Role
@ -125,7 +134,7 @@ data Tag = Tag { identifier :: TagID
deriving (Show, Generic) deriving (Show, Generic)
data Visibility = Public | Private | Followers data Visibility = Public | Private | Followers
deriving (Show, Read, Generic) deriving (Show, Read, Generic, Eq)
instance ToJSON Visibility instance ToJSON Visibility
instance FromJSON Visibility instance FromJSON Visibility

View File

@ -1,37 +1,45 @@
{-# Language DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# Language TypeFamilies #-} {-# LANGUAGE DeriveGeneric #-}
{-# Language TypeOperators #-} {-# LANGUAGE FlexibleInstances #-}
{-# Language NoImplicitPrelude #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# Language MultiParamTypeClasses #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# Language OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# Language TemplateHaskell #-} {-# LANGUAGE QuasiQuotes #-}
{-# Language QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-}
{-# Language RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# Language DeriveGeneric #-} {-# LANGUAGE TemplateHaskell #-}
{-# Language FlexibleInstances #-} {-# LANGUAGE TypeApplications #-}
{-# Language TypeApplications #-} {-# LANGUAGE TypeFamilies #-}
{-# Language ScopedTypeVariables #-} {-# LANGUAGE TypeOperators #-}
module Server where module Server where
import qualified API as API import qualified API as API
import Server.Auth (authCheck)
import Servant
import Types
import ClassyPrelude hiding (Handler) import ClassyPrelude hiding (Handler)
import Control.Monad.Logger
import Control.Monad.Except
import Servant.Auth.Server as SAS
import Control.Lens import Control.Lens
import Control.Monad.Except
import Control.Monad.Logger
import Data.Generics.Product import Data.Generics.Product
import Servant
import Servant.Auth.Docs ()
import Servant.Auth.Server as SAS
import qualified Servant.Docs as Docs
import Servant.HTML.Lucid (HTML)
import Server.Auth (SafeUser) import Server.Auth (SafeUser)
import Server.Auth (authCheck)
import Types
type API = API.API :<|> "static" :> Raw type API = API.API
:<|> "help" :> Get '[PlainText, HTML] String
:<|> "static" :> Raw
type Ctx = '[BasicAuthData -> IO (AuthResult SafeUser), CookieSettings, JWTSettings] type Ctx = '[BasicAuthData -> IO (AuthResult SafeUser), CookieSettings, JWTSettings]
server :: App -> Application server :: App -> Application
server app = serveWithContext api cfg (hoistServerWithContext (Proxy @ API.API) (Proxy @ Ctx) server' API.handler :<|> serveDirectoryFileServer "static") server app = serveWithContext api cfg (hoistServerWithContext (Proxy @ API.API) (Proxy @ Ctx) server' API.handler :<|> serveDocs :<|> serveDirectoryFileServer "static")
where where
apiDocs :: Docs.API
apiDocs = Docs.docs (Proxy @API.API)
serveDocs = pure $ Docs.markdown apiDocs
myKey = view (field @"jwk") app myKey = view (field @"jwk") app
jwtCfg = defaultJWTSettings myKey jwtCfg = defaultJWTSettings myKey
authCfg = authCheck app authCfg = authCheck app

View File

@ -1,11 +1,11 @@
{-# Language DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# Language TypeFamilies #-} {-# LANGUAGE DuplicateRecordFields #-}
{-# Language OverloadedStrings #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# Language NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-}
{-# Language TypeOperators #-} {-# LANGUAGE TemplateHaskell #-}
{-# Language DuplicateRecordFields #-} {-# LANGUAGE TypeApplications #-}
{-# Language TypeApplications #-} {-# LANGUAGE TypeFamilies #-}
{-# Language TemplateHaskell #-} {-# LANGUAGE TypeOperators #-}
module Server.Auth module Server.Auth
( SafeUser(..) ( SafeUser(..)
, authCheck , authCheck
@ -15,8 +15,8 @@ module Server.Auth
import ClassyPrelude import ClassyPrelude
import Control.Lens (view) import Control.Lens (view)
import Control.Monad.Catch (MonadThrow, throwM)
import Control.Monad.Logger import Control.Monad.Logger
import Control.Monad.Catch (throwM, MonadThrow)
import Data.Aeson import Data.Aeson
import Data.Generics.Product import Data.Generics.Product
import Database import Database
@ -24,6 +24,7 @@ import Database.Schema
import Database.User import Database.User
import Servant (err401) import Servant (err401)
import Servant.Auth.Server as SAS import Servant.Auth.Server as SAS
import qualified Servant.Docs as Docs
import Types import Types
-- generic-lens can convert similar types to this -- generic-lens can convert similar types to this
@ -36,6 +37,9 @@ data SafeUser = SafeUser { email :: Email
, role :: Role } , role :: Role }
deriving (Show, Generic) deriving (Show, Generic)
instance Docs.ToSample SafeUser where
toSamples _ = [("User", SafeUser "user@example.com" "user" UserRole )]
instance ToJSON SafeUser where instance ToJSON SafeUser where
instance FromJSON SafeUser where instance FromJSON SafeUser where
instance ToJWT SafeUser where instance ToJWT SafeUser where

65
backend/src/Spec.hs Normal file
View File

@ -0,0 +1,65 @@
{-# Language TypeApplications #-}
module Main where
import API.Books
import qualified Data.Aeson as A
import Data.Char (isPrint)
import Data.GenValidity.Text ()
import qualified Data.Text as T
import Database.Schema
import Prelude
import Test.Hspec
import Test.Validity
instance GenUnchecked PlainPassword
instance GenValid PlainPassword
instance GenInvalid PlainPassword
instance Validity PlainPassword
instance GenUnchecked Email
instance GenValid Email
instance GenInvalid Email
instance Validity Email
instance GenUnchecked Username
instance GenValid Username
instance GenInvalid Username
instance Validity Username
instance GenUnchecked BookID
instance GenValid BookID
instance GenInvalid BookID
instance Validity BookID
instance GenUnchecked ChannelID
instance GenValid ChannelID
instance GenInvalid ChannelID
instance Validity ChannelID
instance GenUnchecked Role
instance GenValid Role
instance GenInvalid Role
instance Validity Role
instance GenUnchecked Visibility
instance GenValid Visibility
instance GenInvalid Visibility
instance Validity Visibility
instance GenUnchecked JsonBook
instance GenValid JsonBook
instance GenInvalid JsonBook
instance Validity JsonBook
instance GenUnchecked PostBook
instance GenValid PostBook
instance GenInvalid PostBook
instance Validity PostBook
spec :: Spec
spec = do
describe "JSON encoding" $ do
it "Works for PlainPassword" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @PlainPassword) A.decode
it "Works for Email" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @Email) A.decode
it "Username" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @Username) A.decode
it "Works for BookID" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @BookID) A.decode
it "Works for ChannelID" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @ChannelID) A.decode
it "Works for Role" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @Role) A.decode
it "Works for Visibility" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @Visibility) A.decode
it "Works for JsonBook" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @JsonBook) A.decode
it "Works for PostBook" $ inverseFunctionsIfSecondSucceedsOnValid (A.encode @PostBook) A.decode
main :: IO ()
main = hspec spec

View File

@ -13,13 +13,10 @@ extra-source-files: ChangeLog.md
cabal-version: >=1.10 cabal-version: >=1.10
library library
exposed-modules: Configuration exposed-modules: Data.Versioned
, Data.Versioned
-- other-extensions: -- other-extensions:
build-depends: base >=4.10 build-depends: base >=4.10
, classy-prelude , classy-prelude
, dhall
, foreign-store
, generic-lens , generic-lens
, lens , lens
, mtl , mtl
@ -31,3 +28,20 @@ library
, OverloadedStrings , OverloadedStrings
, RecordWildCards , RecordWildCards
default-language: Haskell2010 default-language: Haskell2010
-- test-suite spec
-- type: exitcode-stdio-1.0
-- main-is: Spec.hs
-- hs-source-dirs: src
-- build-depends: base >=4.10
-- , classy-prelude
-- , foreign-store
-- , generic-lens
-- , lens
-- , mtl
-- , text
-- , transformers
-- , validity
-- , genvalidity-hspec
-- , genvalidity-property
-- , hspec

9
common/src/Spec.hs Normal file
View File

@ -0,0 +1,9 @@
module Main where
import Test.Hspec
spec :: Spec
spec = describe "test" $ it "verifies tests work" $ True == True
main :: IO ()
main = hspec spec

View File

@ -1,12 +1,59 @@
{ nixpkgs, haskellPackages }: { nixpkgs, haskellPackages }:
let
miso = nixpkgs.fetchFromGitHub {
owner = "dmjio";
repo = "miso";
rev = "630e823dd40a434b73124e12b229a79d9fefb01d";
sha256 = "046gdp3ah2lsipfcy89rh20mn08xbhcgrj549v8zzy69j33xjm2l";
};
miso-jsaddle = super: if haskellPackages.ghc.isGhcjs or false then (super.callPackage (miso + "/miso-ghcjs.nix") {}) else (super.callPackage (miso + "/miso-ghc-jsaddle.nix") {});
dontCheck = nixpkgs.haskell.lib.dontCheck;
in
(import ./project.nix nixpkgs) { (import ./project.nix nixpkgs) {
haskellPackages = haskellPackages;
packages = { packages = {
common = ./common; common = ./common;
backend = ./backend; backend = ./backend;
frontend = ./frontend;
}; };
overrides = self: super: { overrides = self: super: {
generic-lens = nixpkgs.haskell.lib.dontCheck super.generic-lens; generic-lens = nixpkgs.haskell.lib.dontCheck super.generic-lens;
miso = miso-jsaddle super;
# doctest = null; # Not compilable with ghcjs
# comonad = dontCheck super.comonad;
# Glob = dontCheck super.Glob;
# SHA = dontCheck super.SHA;
# iproute = dontCheck super.iproute;
# semigroupoids = dontCheck super.semigroupoids;
# wai-app-static = dontCheck super.wai-app-static;
# attoparsec = dontCheck super.attoparsec;
# http-date = dontCheck super.http-date;
# lens = dontCheck super.lens;
# unix-time = dontCheck super.unix-time;
# http-types = dontCheck super.http-types;
# servant = dontCheck super.servant;
# servant-server = dontCheck super.servant-server;
# servant-auth-docs = dontCheck super.servant-auth-docs;
# lens-aeson = dontCheck super.lens-aeson;
# word8 = dontCheck super.word8;
# http2 = dontCheck super.http2;
# wai-extra = dontCheck super.wai-extra;
# pgp-wordlist = dontCheck super.pgp-wordlist;
# prettyprinter = dontCheck super.prettyprinter;
# unliftio = dontCheck super.unliftio;
# prettyprinter-ansi-terminal = dontCheck super.prettyprinter-ansi-terminal;
# distributive = dontCheck super.distributive;
# genvalidity-property = dontCheck super.genvalidity-property;
# genvalidity-hspec = dontCheck super.genvalidity-hspec;
# genvalidity = dontCheck super.genvalidity;
# megaparsec = dontCheck super.megaparsec;
# ncurses = null;
# haskeline = super.callHackage "haskeline" "0.7.4.2" {};
# terminfo = super.callHackage "terminfo" "0.4.1.1" {};
}; };
tools = with haskellPackages; [ tools = with haskellPackages; [
ghcid ghcid

5
frontend/ChangeLog.md Normal file
View File

@ -0,0 +1,5 @@
# Revision history for frontend
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

30
frontend/LICENSE Normal file
View File

@ -0,0 +1,30 @@
Copyright (c) 2018, Mats Rauhala
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Mats Rauhala nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2
frontend/Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

28
frontend/frontend.cabal Normal file
View File

@ -0,0 +1,28 @@
-- Initial frontend.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: frontend
version: 0.1.0.0
-- synopsis:
-- description:
license: BSD3
license-file: LICENSE
author: Mats Rauhala
maintainer: mats.rauhala@iki.fi
-- copyright:
category: Web
build-type: Simple
extra-source-files: ChangeLog.md
cabal-version: >=1.10
executable frontend
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.11 && <4.12
, miso
, jsaddle-warp
, mtl
, common
hs-source-dirs: src
default-language: Haskell2010

41
frontend/src/Main.hs Normal file
View File

@ -0,0 +1,41 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import Control.Monad.Trans (liftIO)
import Language.Javascript.JSaddle.Warp
import Miso
import Miso.String
data Action = Add
| Subtract
| SayHello
| NoOp
newtype Model = Model Int deriving (Eq, Num, ToMisoString)
updateModel :: Action -> Model -> Effect Action Model
updateModel Add m = noEff (m + 1)
updateModel Subtract m = noEff (m - 1)
updateModel SayHello m = m <# (liftIO (putStrLn "Hello world") >> pure NoOp)
updateModel NoOp m = noEff m
viewModel :: Model -> View Action
viewModel x =
div_ [] [ button_ [ onClick Add ] [ text "+" ]
, text (ms x)
, button_ [ onClick Subtract ] [ text "-" ]
]
main :: IO ()
main = run 8081 $ startApp App{..}
where
model = Model 0
initialAction = SayHello
update = updateModel
view = viewModel
subs = []
events = defaultEvents
mountPoint = Nothing

16
ghcjs.patch Normal file
View File

@ -0,0 +1,16 @@
diff --git a/lib/boot/shims/src/string.js b/lib/boot/shims/src/string.js
index d82f75a..f16e55a 100644
--- a/lib/boot/shims/src/string.js
+++ b/lib/boot/shims/src/string.js
@@ -780,11 +780,7 @@ function h$throwJSException(e) {
// adding the Exception dictionary
var strVal = e.toString() + '\n' + Array.prototype.join.call(e.stack, '\n');
var someE = MK_SOMEEXCEPTION(HS_JSEXCEPTION_EXCEPTION,
-#ifdef GHCJS_PROF
- MK_JSEXCEPTION(MK_JSVAL(e), h$toHsString(strVal), h$CCS_SYSTEM))
-#else
MK_JSEXCEPTION(MK_JSVAL(e), h$toHsString(strVal))
-#endif
);
return h$throw(someE, true);
}

14
jsaddle-warp-ghcjs.nix Normal file
View File

@ -0,0 +1,14 @@
{ mkDerivation, base, fetchgit, stdenv }:
mkDerivation {
pname = "jsaddle-warp";
version = "0.9.5.0";
src = fetchgit {
url = "https://github.com/ghcjs/jsaddle.git";
rev = "34fe7d61b3f387b81aa748294ac8d993243f53b4";
sha256 = "0qdh5qdk23vcp1yp910zgw2hs4zpbx9ig25xgaax0iwj2m1ifh5x";
};
postUnpack = "sourceRoot+=/jsaddle-warp; echo source root reset to $sourceRoot";
libraryHaskellDepends = [ base ];
description = "Interface for JavaScript that works with GHCJS and GHC";
license = stdenv.lib.licenses.mit;
}

View File

@ -0,0 +1 @@
create unique index tag_owner on tags (tag, owner);

View File

@ -1,7 +1,7 @@
{ {
"url": "https://github.com/nixos/nixpkgs.git", "url": "https://github.com/nixos/nixpkgs.git",
"rev": "e0d250e5cf6d179e1ccc775472d89718f61fcfd1", "rev": "4507926b80c6b8f73053775ffee17f6781c7e7c8",
"date": "2018-01-08T11:52:28+01:00", "date": "2018-01-08T11:52:28+01:00",
"sha256": "1iqpjz4czcpghbv924a5h4jvfmj6c8q6sl3b1z7blz3mi740aivs", "sha256": "068v9xh7d8klk62p2qwr76fyfqfh1bp08xc12x138g5q6pg6yfzb",
"fetchSubmodules": true "fetchSubmodules": true
} }

View File

@ -6,7 +6,8 @@ let
in in
{ packages { haskellPackages
, packages
, overrides ? _ : _ : {} , overrides ? _ : _ : {}
, tools ? [] , tools ? []
}: }:
@ -17,12 +18,17 @@ let
(self: super: mapAttrs (name: path: self.callCabal2nix name path {}) packages) (self: super: mapAttrs (name: path: self.callCabal2nix name path {}) packages)
overrides overrides
]; ];
haskellPackages = nixpkgs.haskellPackages.override { overrides = overrides'; }; haskellPackages' = haskellPackages.extend overrides';
packages' = mapAttrs (name: _: haskellPackages."${name}") packages; packages' = mapAttrs (name: _: haskellPackages'."${name}") packages;
mkShell = name: pkg: mkShell = name: pkg:
let let
n = "${name}-shell"; n = "${name}-shell";
deps = haskellPackages.ghcWithHoogle (pkgs: pkg.buildInputs ++ pkg.propagatedBuildInputs); deps = with haskellPackages'; [
ghcid
cabal-install
hasktags
(haskellPackages'.ghcWithHoogle (pkgs: pkg.buildInputs ++ pkg.propagatedBuildInputs))
];
in in
{ {
name = "${n}"; name = "${n}";

View File

@ -2,14 +2,71 @@
let let
jsaddle = nixpkgs.fetchFromGitHub {
owner = "ghcjs";
repo = "jsaddle";
rev = "34fe7d61b3f387b81aa748294ac8d993243f53b4";
sha256 = "0qdh5qdk23vcp1yp910zgw2hs4zpbx9ig25xgaax0iwj2m1ifh5x";
};
config = {
packageOverrides = pkgs: with pkgs.haskell.lib; with pkgs.lib; {
haskell = pkgs.haskell // {
packages = pkgs.haskell.packages // {
ghccustom = pkgs.haskell.packages.ghc843.override {
overrides = self: super: {
jsaddle-warp = dontCheck (super.callPackage (jsaddle + "/jsaddle-warp") {});
# jsaddle-warp = super.callPackage ./jsaddle-warp-ghcjs.nix {};
jsaddle = dontCheck (super.callPackage (jsaddle + "/jsaddle") {});
};
};
ghcjscustom = pkgs.haskell.packages.ghcjs84.override {
overrides = self: super: {
doctest = null;
comonad = dontCheck (super.comonad);
classy-prelude = dontCheck (super.classy-prelude);
unliftio = dontCheck (super.unliftio);
semigroupoids = dontCheck (super.semigroupoids);
lens = dontCheck (super.lens);
directory-tree = dontCheck (super.directory-tree);
http-types = dontCheck (super.http-types);
tasty-quickcheck = dontCheck (super.tasty-quickcheck);
scientific = dontCheck (super.scientific);
servant = dontCheck (super.servant);
jsaddle-warp = super.callPackage ./jsaddle-warp-ghcjs.nix {};
ghc = overrideDerivation (super.ghc.override {
ghcjsSrc = pkgs.fetchgit {
url = "https://github.com/ghcjs/ghcjs.git";
rev = "dc190b1bb2453cfa484124e9f335ee3cad1492f7";
sha256 = "0dh52gj0f3700zfyrhisy44b6y9p1bsawwrmd5pllpdyw21zd9lw";
fetchSubmodules = true;
};
}) (drv: { patches = (drv.patches or []) ++ [ ./ghcjs.patch ]; });
};
};
};
};
};
};
pinnedVersion = nixpkgs.lib.importJSON ./nixpkgs-version.json; pinnedVersion = nixpkgs.lib.importJSON ./nixpkgs-version.json;
pinnedPkgs = import (nixpkgs.fetchFromGitHub { pinnedPkgs = import (nixpkgs.fetchFromGitHub {
owner = "NixOS"; owner = "NixOS";
repo = "nixpkgs"; repo = "nixpkgs";
inherit (pinnedVersion) rev sha256; inherit (pinnedVersion) rev sha256;
}) {}; }) { inherit config; };
ghc = import ./default.nix { nixpkgs = pinnedPkgs; haskellPackages = pinnedPkgs.haskell.packages.ghccustom; };
ghcjs = import ./default.nix { nixpkgs = pinnedPkgs; haskellPackages = pinnedPkgs.haskell.packages.ghcjscustom; };
inherit (pinnedPkgs) pkgs; inherit (pinnedPkgs) pkgs;
in in
import ./default.nix { nixpkgs = pinnedPkgs; haskellPackages = pinnedPkgs.haskellPackages; } {
inherit ghc ghcjs;
deps = pkgs.buildEnv {
name = "deps";
paths = [
(pkgs.haskell.packages.ghccustom.ghcWithPackages (_: ghc.backend.buildInputs ++ ghc.backend.propagatedBuildInputs))
(pkgs.haskell.packages.ghcjscustom.ghcWithPackages (_: ghcjs.frontend.buildInputs ++ ghcjs.frontend.propagatedBuildInputs))
];
buildInputs = [ ];
};
}