packages feed

webby 1.1.0 → 1.1.1

raw patch · 4 files changed

+81/−60 lines, 4 files

Files

ChangeLog.md view
@@ -1,8 +1,8 @@ # Change log -## Unreleased - To be v1.1.0+## v1.1.1 -* Add the `image` function to return mime-typed responses.+- Ensure support for GHC 9.4  ## Older releases 
README.md view
@@ -9,64 +9,84 @@ # Example  ``` haskell+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE NoImplicitPrelude #-}+ module Main where +import qualified Data.Text as T+import Network.HTTP.Types (status500)+import qualified Network.Wai as W import qualified Network.Wai.Handler.Warp as W-import qualified Network.Wai              as W-import           Network.HTTP.Types       (status500)--import           UnliftIO                 (liftIO)-import qualified UnliftIO.Exception       as E--import           Relude                   hiding (get, put)-import           Relude.Print             (putTextLn)-import qualified Data.Text                as T--import           Webby+import Relude hiding (get, put)+import Relude.Print (putTextLn)+import UnliftIO (liftIO)+import qualified UnliftIO.Exception as E+import Webby  -- An example exception handler web-applications can install with webby appExceptionHandler :: T.Text -> (E.SomeException -> WebbyM appEnv ()) appExceptionHandler appName = \(exception :: E.SomeException) -> do-    setStatus status500-    let msg = appName <> " failed with " <> show exception-    putTextLn msg-    text msg+  setStatus status500+  let msg = appName <> " failed with " <> show exception+  putTextLn msg+  text msg +newtype AppEnv = AppEnv Text+  deriving (Eq, Show)++-- To demonstrate that appEnv is avaliable via MonadReader interface without any+-- additional boiler-plate+class HasAppName env where+  getAppName :: env -> Text++instance HasAppName AppEnv where+  getAppName (AppEnv name) = name+ main :: IO () main = do-    -- Define the API routes handled by your web-application-    let routes = [ get "/api/a" (text "a\n")-                 , get "/api/b" (text "b\n")-                 , post "/api/capture/:id"-                   (do idVal :: Int <- getCapture "id"-                       text $ (T.pack (show idVal) `T.append` "\n")-                   )-                 , get "/api/isOdd/:val"-                   (do val :: Integer <- getCapture "val"-                       -- if val is odd return the number else we short-circuit the handler-                       unless (odd val) finish-                       text $ show val-                    )-                 , get "/api/showEnv" (do env <- getAppEnv-                                          text env-                                      )-                 , get "/aaah" (liftIO $ E.throwString "oops!")-                 ]--        -- Set the routes definition and exception handler for your-        -- web-application-        webbyConfig = setExceptionHandler (appExceptionHandler "MyApp") $-                      setRoutes routes $-                      defaultWebbyServerConfig+  -- Define the API routes handled by your web-application+  let routes =+        [ get "/api/a" (text "a\n"),+          get "/api/b" (text "b\n"),+          post+            "/api/capture/:id"+            ( do+                idVal :: Int <- getCapture "id"+                text $ (T.pack (show idVal) `T.append` "\n")+            ),+          get+            "/api/isOdd/:val"+            ( do+                val :: Integer <- getCapture "val"+                -- if val is odd return the number else we short-circuit the handler+                unless (odd val) finish+                text $ show val+            ),+          get+            "/api/appName"+            ( do+                appName <- asks getAppName --appEnv is only an `ask` away+                text appName+            ),+          get "/aaah" (liftIO $ E.throwString "oops!")+        ]+      -- Set the routes definition and exception handler for your+      -- web-application+      webbyConfig =+        setExceptionHandler (appExceptionHandler "MyApp") $+          setRoutes routes $+            defaultWebbyServerConfig+      -- Application environment in this example is a simple Text literal.+      -- Usually, application environment would contain database connections+      -- etc.+      appEnv = AppEnv "test-webby" -        -- Application environment in this example is a simple Text literal.-        -- Usually, application environment would contain database connections-        -- etc.-        appEnv = "MyEnv" :: T.Text+  webbyApp <- mkWebbyApp appEnv webbyConfig+  putStrLn "Starting webserver..."+  W.runEnv 7000 webbyApp -    webbyApp <- mkWebbyApp appEnv webbyConfig-    putStrLn "Starting webserver..."-    W.runEnv 7000 webbyApp ```  You can try the example above, by cloning the repo and running the@@ -81,12 +101,16 @@ ``` shell $ curl http://localhost:7000/api/a a+ $ curl http://localhost:7000/api/b b+ $ curl -XPOST http://localhost:7000/api/capture/32 32-$ curl http://localhost:7000/api/showEnv-MyEnv++$ curl http://localhost:7000/api/appName+test-webby+ $ curl http://localhost:7000/aaah MyApp failed with Control.Exception.Safe.throwString called with: 
examples/Basic.hs view
@@ -22,18 +22,14 @@ newtype AppEnv = AppEnv Text   deriving (Eq, Show) --- To demonstrate that appEnv is avaliable via MonadReader interface and no--- additional boiler-plate is required+-- To demonstrate that appEnv is available via MonadReader interface without+-- additional boiler-plate class HasAppName env where   getAppName :: env -> Text  instance HasAppName AppEnv where   getAppName (AppEnv name) = name -fetchAppName :: (HasAppName env, MonadReader env m, MonadIO m) => m Text-fetchAppName = do-  asks getAppName- main :: IO () main = do   -- Define the API routes handled by your web-application@@ -55,9 +51,9 @@                 text $ show val             ),           get-            "/api/showAppName"+            "/api/appName"             ( do-                appName <- fetchAppName+                appName <- asks getAppName --appEnv is only an `ask` away                 text appName             ),           get "/aaah" (liftIO $ E.throwString "oops!")
webby.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name:           webby-version:        1.1.0+version:        1.1.1 synopsis:       A super-simple web server framework description:    A super-simple, easy to use web server framework inspired by                 Scotty. The goals of the project are: (1) Be easy to use (2) Allow@@ -23,7 +23,8 @@                 GHC == 8.8.4                 GHC == 8.10.7                 GHC == 9.0.2-                GHC == 9.2.4+                GHC == 9.2.7+                GHC == 9.4.4  source-repository head   type: git