scotty-utils (empty) → 0.1.0.0
raw patch · 3 files changed
+113/−0 lines, 3 filesdep +aesondep +aeson-resultdep +base
Dependencies added: aeson, aeson-result, base, http-types, scotty, text
Files
- LICENSE +30/−0
- scotty-utils.cabal +30/−0
- src/Web/Scotty/Utils.hs +53/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Li Meng Jun (c) 2017++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 Li Meng Jun 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.
+ scotty-utils.cabal view
@@ -0,0 +1,30 @@+name: scotty-utils+version: 0.1.0.0+synopsis: Scotty utils library+description: Scotty utils library.+homepage: https://github.com/Lupino/yuntan-common/tree/master/scotty-utils#readme+license: BSD3+license-file: LICENSE+author: Li Meng Jun+maintainer: lmjubuntu@gmail.com+copyright: MIT+category: Web, Scotty, Utils+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10+++library+ hs-source-dirs: src+ exposed-modules: Web.Scotty.Utils+ build-depends: base >= 4.7 && < 5+ , http-types+ , aeson+ , scotty+ , text+ , aeson-result+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/Lupino/yuntan-common
+ src/Web/Scotty/Utils.hs view
@@ -0,0 +1,53 @@+{-# LANGUAGE OverloadedStrings #-}++module Web.Scotty.Utils+ ( maybeNotFound+ , eitherNotFound+ , eitherBadRequest+ , err+ , ok+ , errNotFound+ , errBadRequest+ , okListResult+ , safeParam+ ) where++import Network.HTTP.Types (Status, status400, status404)+import Web.Scotty.Trans (ActionT, Parsable, ScottyError, json,+ param, rescue, status)++import qualified Data.Aeson.Result as R (Err, List, err, fromList, fromOk, ok)++import Data.Aeson (ToJSON)+import Data.Text (Text)+import qualified Data.Text.Lazy as LT (Text)++maybeNotFound :: (ToJSON a, ScottyError e, Monad m) => String -> Maybe a -> ActionT e m ()+maybeNotFound _ (Just a) = json a+maybeNotFound obj Nothing = err status404 $ obj ++ " not found."++eitherBadRequest :: (ToJSON a, ScottyError e, Monad m) => Either R.Err a -> ActionT e m ()+eitherBadRequest (Right a) = json a+eitherBadRequest (Left e) = status status400 >> json e++eitherNotFound :: (ToJSON a, ScottyError e, Monad m) => Either R.Err a -> ActionT e m ()+eitherNotFound (Right a) = json a+eitherNotFound (Left e) = status status404 >> json e++err :: (ScottyError e, Monad m) => Status -> String -> ActionT e m ()+err st msg = status st >> json (R.err msg)++ok :: (ToJSON a, ScottyError e, Monad m) => Text -> a -> ActionT e m ()+ok key = json . R.fromOk key . R.ok++errNotFound :: (ScottyError e, Monad m) => String -> ActionT e m ()+errNotFound = err status404++errBadRequest :: (ScottyError e, Monad m) => String -> ActionT e m ()+errBadRequest = err status400++okListResult :: (ToJSON a, ScottyError e, Monad m) => Text -> R.List a -> ActionT e m ()+okListResult key = json . R.fromList key++safeParam ::(Parsable a, ScottyError e, Monad m) => LT.Text -> a -> ActionT e m a+safeParam key def = param key `rescue` (\_ -> return def)