Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Split up Types.hs and logically organize modules #1793

Merged
merged 39 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
befb126
move ConnectionStatus to Main
monacoremo Apr 6, 2021
c38b5fe
split ContentTypes from Types
monacoremo Apr 6, 2021
c65ca0c
split DbStructureTypes from Types
monacoremo Apr 6, 2021
48e58d9
split Preferences from Types
monacoremo Apr 6, 2021
a552358
move LogLevel to Config from Types
monacoremo Apr 6, 2021
158bc69
move SCstatus from Types to Main
monacoremo Apr 6, 2021
f4aca18
moved SqlFragment and Proxy from Types
monacoremo Apr 6, 2021
8d315c6
moved PgVersion
monacoremo Apr 6, 2021
47f1797
move SourceColumn
monacoremo Apr 6, 2021
f1678de
split out JSPath
monacoremo Apr 6, 2021
b6df497
split out Headers
monacoremo Apr 6, 2021
5ca9488
refactor remaining types
monacoremo Apr 6, 2021
4232470
move remaining Types to Queries
monacoremo Apr 6, 2021
5cefbcd
style
monacoremo Apr 6, 2021
3979841
tweak imports in Private
monacoremo Apr 6, 2021
8fe655c
ApiRequests imports
monacoremo Apr 6, 2021
eba52e4
App imports
monacoremo Apr 6, 2021
132f002
DbStructureTypes exports
monacoremo Apr 6, 2021
ca0c3e8
Error imports
monacoremo Apr 6, 2021
cad4a7f
JSPath exports
monacoremo Apr 6, 2021
55f9ad6
Middleware imports
monacoremo Apr 6, 2021
0784eb6
Parsers imports
monacoremo Apr 6, 2021
87718b9
QueryTypes exports
monacoremo Apr 6, 2021
fa62b78
QueryBuilder imports
monacoremo Apr 6, 2021
77a6c72
Statements imports
monacoremo Apr 6, 2021
ba08243
move setConfigLocal
monacoremo Apr 6, 2021
7ead887
ContentTypes -> ContentType
monacoremo Apr 8, 2021
8a72249
DbStructure
monacoremo Apr 8, 2021
30c3013
move ProxyUri
monacoremo Apr 9, 2021
4717592
move Preferences and SqlFragment, dissolve Private.Common
monacoremo Apr 9, 2021
03a1250
move JSPath and Proxy to Config
monacoremo Apr 9, 2021
6e6dfc5
fix tests
monacoremo Apr 10, 2021
fc1350c
move PgVersion to DbStructure
monacoremo Apr 10, 2021
90b91c7
fix main
monacoremo Apr 10, 2021
41e55a0
sketch part of potential target structure
monacoremo Apr 11, 2021
13d3e86
Headers -> GucHeader
monacoremo Apr 11, 2021
92038a7
move setConfigLocal
monacoremo Apr 11, 2021
f89bebf
fix rebase derp
monacoremo Apr 11, 2021
2def566
fix removed emptySnippetOnFalse
monacoremo Apr 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,39 @@ import System.CPUTime (getCPUTime)
import System.IO (BufferMode (..), hSetBuffering)
import Text.Printf (hPrintf)

import PostgREST.App (postgrest)
import PostgREST.App (postgrest)
import PostgREST.Config
import PostgREST.DbStructure (getDbStructure, getPgVersion)
import PostgREST.Error (PgError (PgError), checkIsFatal,
errorPayload)
import PostgREST.Statements (dbSettingsStatement)
import PostgREST.Types (ConnectionStatus (..), DbStructure,
PgVersion (..), SCacheStatus (..),
minimumPgVersion)
import Protolude hiding (hPutStrLn, head, toS)
import Protolude.Conv (toS)
import PostgREST.DbStructure (DbStructure, getDbStructure,
getPgVersion)
import PostgREST.DbStructure.PgVersion (PgVersion (..),
minimumPgVersion)
import PostgREST.Error (PgError (PgError),
checkIsFatal, errorPayload)
import PostgREST.Query.Statements (dbSettingsStatement)

import Protolude hiding (hPutStrLn, head, toS)
import Protolude.Conv (toS)


#ifndef mingw32_HOST_OS
import System.Posix.Signals
import UnixSocket
#endif


-- | Current database connection status data ConnectionStatus
data ConnectionStatus
= NotConnected
| Connected PgVersion
| FatalConnectionError Text
deriving (Eq)

-- | Schema cache status
data SCacheStatus
= SCLoaded
| SCOnRetry
| SCFatalFail

-- | This is where everything starts.
main :: IO ()
main = do
Expand Down
28 changes: 18 additions & 10 deletions postgrest.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,32 @@ library
default-extensions: OverloadedStrings
NoImplicitPrelude
hs-source-dirs: src
exposed-modules: PostgREST.ApiRequest
PostgREST.App
exposed-modules: PostgREST.App
PostgREST.Auth
PostgREST.Config
PostgREST.DbRequestBuilder
PostgREST.Config.JSPath
PostgREST.Config.Proxy
PostgREST.ContentType
PostgREST.DbStructure
PostgREST.DbStructure.Identifiers
PostgREST.DbStructure.PgVersion
PostgREST.DbStructure.Proc
PostgREST.DbStructure.Relation
PostgREST.DbStructure.Table
PostgREST.Error
PostgREST.GucHeader
PostgREST.Middleware
PostgREST.OpenAPI
PostgREST.Parsers
PostgREST.QueryBuilder
PostgREST.Statements
PostgREST.Query.QueryBuilder
PostgREST.Query.SqlFragment
PostgREST.Query.Statements
PostgREST.RangeQuery
PostgREST.Types
PostgREST.Request.ApiRequest
PostgREST.Request.DbRequestBuilder
PostgREST.Request.Parsers
PostgREST.Request.Preferences
PostgREST.Request.Types
other-modules: Paths_postgrest
PostgREST.Private.Common
PostgREST.Private.ProxyUri
PostgREST.Private.QueryFragment
build-depends: base >= 4.9 && < 4.15
, HTTP >= 4000.3.7 && < 4000.4
, Ranged-sets >= 0.3 && < 0.5
Expand Down
70 changes: 45 additions & 25 deletions src/PostgREST/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,43 @@ import qualified Network.HTTP.Types.Status as HTTP
import qualified Network.HTTP.Types.URI as HTTP
import qualified Network.Wai as Wai

import qualified PostgREST.ApiRequest as ApiRequest
import qualified PostgREST.Auth as Auth
import qualified PostgREST.DbRequestBuilder as ReqBuilder
import qualified PostgREST.DbStructure as DbStructure
import qualified PostgREST.Error as Error
import qualified PostgREST.Middleware as Middleware
import qualified PostgREST.OpenAPI as OpenAPI
import qualified PostgREST.QueryBuilder as QueryBuilder
import qualified PostgREST.RangeQuery as RangeQuery
import qualified PostgREST.Statements as Statements

import PostgREST.ApiRequest (Action (..), ApiRequest (..),
InvokeMethod (..), Target (..))
import PostgREST.Config (AppConfig (..))
import PostgREST.Error (Error)

import PostgREST.Types
import qualified PostgREST.Auth as Auth
import qualified PostgREST.DbStructure as DbStructure
import qualified PostgREST.Error as Error
import qualified PostgREST.Middleware as Middleware
import qualified PostgREST.OpenAPI as OpenAPI
import qualified PostgREST.Query.QueryBuilder as QueryBuilder
import qualified PostgREST.Query.Statements as Statements
import qualified PostgREST.RangeQuery as RangeQuery
import qualified PostgREST.Request.ApiRequest as ApiRequest
import qualified PostgREST.Request.DbRequestBuilder as ReqBuilder

import PostgREST.Config (AppConfig (..),
LogLevel (..))
import PostgREST.ContentType (ContentType (..))
import PostgREST.DbStructure (DbStructure (..),
tablePKCols)
import PostgREST.DbStructure.Identifiers (FieldName,
QualifiedIdentifier (..),
Schema)
import PostgREST.DbStructure.Proc (ProcDescription (..),
ProcVolatility (..))
import PostgREST.DbStructure.Table (Table (..))
import PostgREST.Error (Error)
import PostgREST.GucHeader (GucHeader,
addHeadersIfNotIncluded,
unwrapGucHeader)
import PostgREST.Request.ApiRequest (Action (..),
ApiRequest (..),
InvokeMethod (..),
Target (..))
import PostgREST.Request.Preferences (PreferCount (..),
PreferParameters (..),
PreferRepresentation (..))
import PostgREST.Request.Types (ReadRequest, fstFieldNames)

import qualified PostgREST.ContentType as ContentType
import qualified PostgREST.DbStructure.Proc as Proc

import Protolude hiding (Handler, toS)
import Protolude.Conv (toS)
Expand Down Expand Up @@ -123,7 +143,7 @@ postgrestResponse conf@AppConfig{..} maybeDbStructure pool time req = do
Just ct ->
return ct
Nothing ->
throwError . Error.ContentTypeError $ map toMime iAccepts
throwError . Error.ContentTypeError $ map ContentType.toMime iAccepts

let
handleReq apiReq =
Expand Down Expand Up @@ -352,9 +372,9 @@ handleInvoke invMethod proc context@RequestContext{..} = do
identifier =
QualifiedIdentifier
(pdSchema proc)
(fromMaybe (pdName proc) $ procTableName proc)
(fromMaybe (pdName proc) $ Proc.procTableName proc)

returnsSingle (ApiRequest.TargetProc target _) = procReturnsSingle target
returnsSingle (ApiRequest.TargetProc target _) = Proc.procReturnsSingle target
returnsSingle _ = False

req <- readRequest identifier context
Expand All @@ -367,7 +387,7 @@ handleInvoke invMethod proc context@RequestContext{..} = do
(returnsSingle iTarget)
(QueryBuilder.requestToCallProcQuery
(QualifiedIdentifier (pdSchema proc) (pdName proc))
(specifiedProcArgs iColumns proc)
(Proc.specifiedProcArgs iColumns proc)
iPayload
(returnsScalar iTarget)
iPreferParameters
Expand Down Expand Up @@ -405,7 +425,7 @@ handleOpenApi headersOnly tSchema (RequestContext conf@AppConfig{..} dbStructure

return $
Wai.responseLBS HTTP.status200
(toHeader CTOpenAPI : maybeToList (profileHeader apiRequest))
(ContentType.toHeader CTOpenAPI : maybeToList (profileHeader apiRequest))
(if headersOnly then mempty else toS body)

txMode :: ApiRequest -> SQL.Mode
Expand Down Expand Up @@ -491,7 +511,7 @@ shouldCount preferCount =
preferCount == Just ExactCount || preferCount == Just EstimatedCount

returnsScalar :: ApiRequest.Target -> Bool
returnsScalar (TargetProc proc _) = procReturnsScalar proc
returnsScalar (TargetProc proc _) = Proc.procReturnsScalar proc
returnsScalar _ = False

readRequest :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m ReadRequest
Expand All @@ -503,7 +523,7 @@ readRequest QualifiedIdentifier{..} (RequestContext AppConfig{..} dbStructure ap

contentTypeHeaders :: RequestContext -> [HTTP.Header]
contentTypeHeaders RequestContext{..} =
toHeader ctxContentType : maybeToList (profileHeader ctxApiRequest)
ContentType.toHeader ctxContentType : maybeToList (profileHeader ctxApiRequest)

requestContentTypes :: AppConfig -> ApiRequest -> [ContentType]
requestContentTypes conf ApiRequest{..} =
Expand Down Expand Up @@ -542,7 +562,7 @@ binaryField RequestContext{..} readReq

rawContentTypes :: AppConfig -> [ContentType]
rawContentTypes AppConfig{..} =
(decodeContentType <$> configRawMediaTypes) `union` [CTOctetStream, CTTextPlain]
(ContentType.decodeContentType <$> configRawMediaTypes) `union` [CTOctetStream, CTTextPlain]

profileHeader :: ApiRequest -> Maybe HTTP.Header
profileHeader ApiRequest{..} =
Expand Down
9 changes: 6 additions & 3 deletions src/PostgREST/Auth.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ In the test suite there is an example of simple login function that can be used
very simple authentication system inside the PostgreSQL database.
-}
{-# LANGUAGE RecordWildCards #-}
module PostgREST.Auth (containsRole, jwtClaims, JWTClaims) where
module PostgREST.Auth
( containsRole
, jwtClaims
, JWTClaims
) where

import qualified Crypto.JWT as JWT
import qualified Data.Aeson as JSON
Expand All @@ -23,9 +27,8 @@ import Control.Monad.Except (liftEither)
import Data.Either.Combinators (mapLeft)
import Data.Time.Clock (UTCTime)

import PostgREST.Config (AppConfig (..))
import PostgREST.Config (AppConfig (..), JSPath, JSPathExp (..))
import PostgREST.Error (Error (..))
import PostgREST.Types (JSPath, JSPathExp (..))

import Protolude

Expand Down
Loading