Skip to content

Instantly share code, notes, and snippets.

@aeveltstra
Created November 3, 2023 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeveltstra/df76c36fa15ebb5b907c70b21171c0f0 to your computer and use it in GitHub Desktop.
Save aeveltstra/df76c36fa15ebb5b907c70b21171c0f0 to your computer and use it in GitHub Desktop.
Haskell Webserver
{-# LANGUAGE OverloadedStrings #-}
{- | Sample Haskell Web Application
- Based on WAI, implemented with the WARP web server.
- See https://www.yesodweb.com/book/web-application-interface
- See https://www.stackage.org/package/wai
-}
import Network.Wai
import Network.Wai.Handler.Warp (run)
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Application.Static
{- | Entry point of the application. Defines and logs on which
- port to listen, and starts the web server.
-}
main :: IO ()
main = do
-- Probably a good idea later on to read the port from input.
let port = 8181
-- Determine the root folder of the website:
-- if relative, starts at the webserver executable.
let webroot = "public"
putStrLn $ "Listening on port " ++ show port
-- Choice 1: run over http, no encryption.
run port $ logStdout $ staticApp $ defaultFileServerSettings webroot
@aeveltstra
Copy link
Author

Haskell WAI and WARP modules make creating static webservers very easy, as the above is all but a one-liner. That's it: that's the whole server. Useful for local development and testing.

@aeveltstra
Copy link
Author

Cabal file contents to get cabal to load program dependenciesL

cabal-version: >=1.10
-- Package description generated by 'cabal init'. For further documentation,
-- see http://haskell.org/cabal/users-guide/

name: webserver
version: 0.1.0.1
-- synopsis:
-- description:
-- bug-reports:
-- license:
license-file: LICENSE
author: A.E.Veltstra
maintainer: aev@sdf.org
copyright: CopyLeft
category: Web Applications
build-type: Simple
extra-source-files: README.md
, CHANGELOG.md

executable webserver
main-is: Main.hs
other-extensions: OverloadedStrings
build-depends: base >=4.16 && <4.17
, wai
, wai-app-static
, wai-extra
, wai-log
, warp
, warp-tls

-- hs-source-dirs:
default-language: Haskell2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment