webby: A super-simple web server framework

[ apache, library, web ] [ Propose Tags ]

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 graceful exception handling (3) Parse request parameters easily and in a typed manner.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.1, 0.1.2, 0.1.3, 0.2.0, 0.3.0, 0.3.1, 0.4.0
Dependencies aeson (>=1.2 && <1.5), base-noprelude (>=4.7 && <5), binary (==0.8.*), bytestring (==0.10.*), formatting (==6.3.*), http-api-data (>=0.3 && <0.5), http-types (==0.12.*), relude, resourcet (==1.2.*), text (==1.2.*), unliftio (>=0.2.7 && <0.3), unordered-containers (>=0.2.9 && <0.3), wai (==3.2.*) [details]
License Apache-2.0
Author
Maintainer aditya.mmy@gmail.com
Category Web
Home page https://github.com/donatello/webby
Bug tracker https://github.com/donatello/webby/issues
Source repo head: git clone https://github.com/donatello/webby
Uploaded by AdityaManthramurthy at Wed Oct 2 21:22:12 UTC 2019
Distributions NixOS:0.4.0
Downloads 839 total (148 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2019-10-02 [all 1 reports]

Modules

[Index] [Quick Jump]

Flags

NameDescriptionDefaultType
devDisabledManual

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for webby-0.4.0

[back to package description]

Webby

An easy to use Haskell web-server inspired by Scotty.

Build

Clone the repo and run stack build

Example

module Main where

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

-- 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

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

        -- 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

You can try the example above, by cloning the repo and running the example:

$ examples/Basic.hs

In another shell, let's curl the server:

$ 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/aaah
MyApp failed with Control.Exception.Safe.throwString called with:

oops!
Called from:
  throwString (examples/Basic.hs:55:42 in main:Main)