MyNixOS website logo
Description

A super-simple web server framework.

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.

Webby

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

Build

Clone the repo and run stack build

Example

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

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

  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/appName
test-webby

$ 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)
Metadata

Version

1.1.1

License

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows