|
| 1 | +#!/usr/bin/env stack |
| 2 | +{- stack |
| 3 | + --resolver lts-6.11 |
| 4 | + --install-ghc |
| 5 | + runghc |
| 6 | + --package shakespeare |
| 7 | + --package wai-app-static |
| 8 | + --package wai-extra |
| 9 | + --package warp |
| 10 | + -} |
| 11 | + |
| 12 | +-- The code above is used for Haskell Stack's script interpreter |
| 13 | +-- feature. For more information, see: |
| 14 | +-- https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter |
| 15 | +-- |
| 16 | +-- Note how we explicitly list an LTS Haskell snapshot |
| 17 | +-- (https://www.stackage.org/lts-6.11) to ensure reproducibility. We |
| 18 | +-- then state which packages need to be present to run this code. |
| 19 | + |
| 20 | +-- Enable the OverloadedStrings extension, a commonly used feature. |
| 21 | +{-# LANGUAGE OverloadedStrings #-} |
| 22 | + |
| 23 | +-- We use the QuasiQuotes to embed Hamlet HTML templates inside |
| 24 | +-- our source file. |
| 25 | +{-# LANGUAGE QuasiQuotes #-} |
| 26 | + |
| 27 | +-- Import the various modules that we'll use in our code. |
| 28 | +import qualified Data.ByteString.Char8 as S8 |
| 29 | +import qualified Data.ByteString.Lazy as L |
| 30 | +import Data.Functor.Identity |
| 31 | +import Network.HTTP.Types |
| 32 | +import Network.Wai |
| 33 | +import Network.Wai.Application.Static |
| 34 | +import Network.Wai.Handler.Warp |
| 35 | +import Network.Wai.Parse |
| 36 | +import System.Environment |
| 37 | +import System.FilePath |
| 38 | +import Text.Blaze.Html.Renderer.Utf8 |
| 39 | +import Text.Hamlet |
| 40 | + |
| 41 | +-- | Entrypoint to our application |
| 42 | +main :: IO () |
| 43 | +main = do |
| 44 | + -- For ease of setup, we want to have a "sanity" command line |
| 45 | + -- argument. We'll see how this is used in the Dockerfile |
| 46 | + -- later. Desired behavior: |
| 47 | + -- |
| 48 | + -- * If we have the argument "sanity", immediately exit |
| 49 | + -- * If we have no arguments, run the server |
| 50 | + -- * Otherwise, error out |
| 51 | + args <- getArgs |
| 52 | + case args of |
| 53 | + ["sanity"] -> putStrLn "Sanity check passed, ready to roll!" |
| 54 | + [] -> do |
| 55 | + putStrLn "Launching DataHandler." |
| 56 | + -- Run our application (defined below) on port 5000 |
| 57 | + run 5000 app |
| 58 | + _ -> error $ "Unknown arguments: " ++ show args |
| 59 | + |
| 60 | +-- | Our main application |
| 61 | +app :: Application |
| 62 | +app req send = |
| 63 | + -- Route the request based on the path requested |
| 64 | + case pathInfo req of |
| 65 | + -- "/": send the HTML homepage contents |
| 66 | + [] -> send $ responseBuilder |
| 67 | + status200 |
| 68 | + [("Content-Type", "text/html; charset=utf-8")] |
| 69 | + (renderHtmlBuilder homepage) |
| 70 | + |
| 71 | + -- "/browse/...": use the file server to allow directory |
| 72 | + -- listings and downloading files |
| 73 | + ("browse":rest) -> |
| 74 | + -- We create a modified request that strips off the |
| 75 | + -- "browse" component of the path, so that the file server |
| 76 | + -- does not need to look inside a /browse/ directory |
| 77 | + let req' = req { pathInfo = rest } |
| 78 | + in fileServer req' send |
| 79 | + |
| 80 | + -- "/upload": handle a file upload |
| 81 | + ["upload"] -> upload req send |
| 82 | + |
| 83 | + -- anything else: 404 |
| 84 | + _ -> send $ responseLBS |
| 85 | + status404 |
| 86 | + [("Content-Type", "text/plain; charset=utf-8")] |
| 87 | + "Not found" |
| 88 | + |
| 89 | +-- | Create an HTML page which links to the /browse URL, and allows |
| 90 | +-- for a file upload |
| 91 | +homepage :: Html |
| 92 | +homepage = [shamlet| |
| 93 | +$doctype 5 |
| 94 | +<html> |
| 95 | + <head> |
| 96 | + <title>File server |
| 97 | + <body> |
| 98 | + <h1>File server |
| 99 | + <p> |
| 100 | + <a href=/browse/>Browse available files |
| 101 | + |
| 102 | + <form method=POST action=/upload enctype=multipart/form-data> |
| 103 | + <p>Upload a new file |
| 104 | + <input type=file name=file> |
| 105 | + <input type=submit> |
| 106 | +|] |
| 107 | + |
| 108 | +-- | Use the standard file server settings to serve files from the |
| 109 | +-- current directory |
| 110 | +fileServer :: Application |
| 111 | +fileServer = staticApp (defaultFileServerSettings ".") |
| 112 | + |
| 113 | +-- | Handle file uploads, storing the file in the current directory |
| 114 | +upload :: Application |
| 115 | +upload req send = do |
| 116 | + -- Parse the request body. We'll ignore parameters and just look |
| 117 | + -- at the files |
| 118 | + (_params, files) <- parseRequestBody lbsBackEnd req |
| 119 | + |
| 120 | + -- Look for the file parameter called "file" |
| 121 | + case lookup "file" files of |
| 122 | + -- Not found, so return a 400 response |
| 123 | + Nothing -> send $ responseLBS |
| 124 | + status400 |
| 125 | + [("Content-Type", "text/plain; charset=utf-8")] |
| 126 | + "No file parameter found" |
| 127 | + -- Got it! |
| 128 | + Just file -> do |
| 129 | + let |
| 130 | + -- Determine the name of the file to write out |
| 131 | + name = takeFileName $ S8.unpack $ fileName file |
| 132 | + -- and grab the content |
| 133 | + content = fileContent file |
| 134 | + -- Write it out |
| 135 | + L.writeFile name content |
| 136 | + |
| 137 | + -- Send a 303 response to redirect back to the homepage |
| 138 | + send $ responseLBS |
| 139 | + status303 |
| 140 | + [ ("Content-Type", "text/plain: charset=utf-8") |
| 141 | + , ("Location", "/") |
| 142 | + ] |
| 143 | + "Upload successful!" |
0 commit comments