hipsql-api (empty) → 0.0.0.0
raw patch · 6 files changed
+188/−0 lines, 6 filesdep +aesondep +basedep +bytestringsetup-changed
Dependencies added: aeson, base, bytestring, servant
Files
- CHANGELOG.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- hipsql-api.cabal +41/−0
- library/Hipsql/API.hs +50/−0
- library/Hipsql/API/Internal.hs +60/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Changelog for hipsql-api++## 0.0.0.0++* Initial release
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright SimSpace (c) 2020++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 SimSpace 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hipsql-api.cabal view
@@ -0,0 +1,41 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: f1b5fc556493646dc34671dfd75d40b86715250587e57f49023aacd7b113eea6++name: hipsql-api+version: 0.0.0.0+description: Please see the README on GitHub at <https://github.com/simspace/hipsql#readme>+homepage: https://github.com/simspace/hipsql#readme+bug-reports: https://github.com/simspace/hipsql/issues+author: Cary Robbins+maintainer: carymrobbins@gmail.com+copyright: 2021 SimSpace+license: BSD3+license-file: LICENSE+build-type: Simple+extra-source-files:+ CHANGELOG.md++source-repository head+ type: git+ location: https://github.com/simspace/hipsql++library+ exposed-modules:+ Hipsql.API+ Hipsql.API.Internal+ other-modules:+ Paths_hipsql_api+ hs-source-dirs:+ library+ ghc-options: -Wall+ build-depends:+ aeson >=1.4.7.1 && <1.5+ , base >=4.7 && <5+ , bytestring >=0.10.10.0 && <0.11+ , servant >=0.16 && <0.17+ default-language: Haskell2010
+ library/Hipsql/API.hs view
@@ -0,0 +1,50 @@+-- | Public API for @hipsql@ server and client implementations using @servant@.+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE TypeOperators #-}+module Hipsql.API where++import Data.Proxy (Proxy(Proxy))+import GHC.Generics (Generic)+import Hipsql.API.Internal (Version(Version), mkVersion)+import Servant.API (type (:>), Get, JSON, OctetStream, Post, ReqBody, Summary)+import Servant.API.Generic (type (:-), ToServantApi)+import qualified Data.ByteString.Lazy as Lazy+import qualified Data.List as List+import qualified Paths_hipsql_api++-- | The servant API type for @hipsql@.+type HipsqlAPI = ToServantApi HipsqlRoutes++-- | The API routes for @hipsql@.+data HipsqlRoutes route = HipsqlRoutes+ { getVersion :: route :-+ Summary "Gets the current server version"+ :> "version"+ :> Get '[JSON] Version++ , eval :: route :-+ Summary "Evaluate a psql expression"+ :> "eval"+ :> ReqBody '[OctetStream] Lazy.ByteString+ :> Post '[OctetStream] Lazy.ByteString+ } deriving stock (Generic)++-- | A @hipsql@ API proxy value.+theHipsqlAPI :: Proxy HipsqlAPI+theHipsqlAPI = Proxy++-- | The current @hipsql-api@ version; servers and clients should use this+-- to report which version of the API they are compiled against.+theHipsqlApiVersion :: Version+theHipsqlApiVersion = mkVersion Paths_hipsql_api.version++-- | Render a 'Version' to a human-readable 'String'.+renderVersion :: Version -> String+renderVersion (Version xs) = List.intercalate "." $ map show xs++-- | Check if two 'Version's are compatible wth one another.+isCompatibleWith :: Version -> Version -> Bool+isCompatibleWith (Version xs) (Version ys) =+ take 2 xs == take 2 ys
+ library/Hipsql/API/Internal.hs view
@@ -0,0 +1,60 @@+-- | Internal module which implements the @hipsql@ HTTP API using @servant@.+{-# LANGUAGE BlockArguments #-}+{-# LANGUAGE DeriveAnyClass #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE LambdaCase #-}+module Hipsql.API.Internal+ ( -- * Disclaimer+ -- $disclaimer++ -- ** Internals+ module Hipsql.API.Internal+ ) where++import Data.Aeson (FromJSON, ToJSON)+import GHC.Generics (Generic)+import System.Environment (lookupEnv)+import Text.Read (readMaybe)+import qualified Data.Version++-- | Lookup the HTTP port to use for a hipsql HTTP server or client+-- by checking the @HIPSQL_PORT@ environment variable. Defaults+-- to 'defaultHipsqlPort' if unset.+lookupHipsqlPort :: IO (Either String Int)+lookupHipsqlPort =+ lookupEnvInt "HIPSQL_PORT" `withDefault` Right defaultHipsqlPort++-- | By default, hipsql should use the port @55805@.+defaultHipsqlPort :: Int+defaultHipsqlPort = 55805++-- | Lookup an environment variable and parse it as an 'Int'.+lookupEnvInt :: String -> IO (Maybe (Either String Int))+lookupEnvInt k =+ flip fmap (lookupEnv k) \mv ->+ flip fmap mv \v ->+ case readMaybe v of+ Just i -> Right i+ Nothing -> Left $ "Invalid int for " <> k <> ": " <> v++-- | Used in conjunction with 'lookupEnvInt' to set a default value.+withDefault :: IO (Maybe a) -> a -> IO a+withDefault action defaultValue = flip fmap action \case+ Nothing -> defaultValue+ Just a -> a++-- | Used to show version information for a hipsql server or client.+newtype Version = Version+ { version :: [Int]+ } deriving stock (Eq, Generic)+ deriving anyclass (FromJSON, ToJSON)++-- | Constructs a 'Version' from a @Paths_hipsql_*.version@ value.+mkVersion :: Data.Version.Version -> Version+mkVersion = Version . Data.Version.versionBranch++-- $disclaimer+--+-- Changes to this module will not be reflected in the library's version+-- updates.