Julian Ospald 1 year ago
parent
commit
3a7c74a67a
No known key found for this signature in database
4 changed files with 80 additions and 3 deletions
  1. 22
    0
      .gitignore
  2. 8
    2
      scotty-example.cabal
  3. 8
    0
      src/Helpers.hs
  4. 42
    1
      src/Main.hs

+ 22
- 0
.gitignore View File

@@ -0,0 +1,22 @@
1
+dist
2
+dist-*
3
+cabal-dev
4
+*.o
5
+*.hi
6
+*.chi
7
+*.chs.h
8
+*.dyn_o
9
+*.dyn_hi
10
+.hpc
11
+.hsenv
12
+.cabal-sandbox/
13
+cabal.sandbox.config
14
+*.prof
15
+*.aux
16
+*.hp
17
+*.eventlog
18
+.stack-work/
19
+cabal.project.local
20
+cabal.project.local~
21
+.HTF/
22
+.ghc.environment.*

+ 8
- 2
scotty-example.cabal View File

@@ -17,9 +17,15 @@ cabal-version:       >=1.10
17 17
 
18 18
 executable scotty-example
19 19
   main-is:             Main.hs
20
-  -- other-modules:
20
+  other-modules:       Helpers
21 21
   -- other-extensions:
22 22
   build-depends:       base >=4.9 && <4.10,
23
-                       scotty
23
+                       bytestring,
24
+                       scotty,
25
+                       http-types,
26
+                       text,
27
+                       wai,
28
+                       wai-extra,
29
+                       utf8-string
24 30
   hs-source-dirs:      src
25 31
   default-language:    Haskell2010

+ 8
- 0
src/Helpers.hs View File

@@ -0,0 +1,8 @@
1
+module Helpers where
2
+
3
+import Data.String
4
+
5
+
6
+fS :: IsString a => String -> a 
7
+fS = fromString
8
+

+ 42
- 1
src/Main.hs View File

@@ -1,4 +1,45 @@
1 1
 module Main where
2 2
 
3
+import Web.Scotty
4
+
5
+import Data.Monoid (mconcat)
6
+import Helpers
7
+import Network.HTTP.Types.Status
8
+import qualified Data.Text.Lazy as T
9
+import Network.Wai
10
+import Network.Wai.Middleware.RequestLogger
11
+import Network.Wai.Middleware.HttpAuth
12
+import qualified Data.ByteString as BS
13
+import qualified Data.ByteString.UTF8 as U8
14
+
15
+
3 16
 main :: IO ()
4
-main = putStrLn "Hello, Haskell!"
17
+main = scotty 3000 $ do
18
+  middleware auth
19
+  middleware logging
20
+  get (fS "/foo") foo
21
+  get (fS "/:word") word_pattern
22
+
23
+
24
+word_pattern :: ActionM ()
25
+word_pattern = do
26
+    beam <- param (fS "word")
27
+    html $ mconcat [fS "<h1>Scotty, ", beam, fS " me up!</h1>"]
28
+
29
+
30
+foo :: ActionM ()
31
+foo = do
32
+  status ok200
33
+  text $ T.pack "Blah"
34
+  return ()
35
+
36
+
37
+logging :: Middleware
38
+logging = logStdout
39
+
40
+
41
+auth :: Middleware
42
+auth = basicAuth (\u p -> return $
43
+                       u == U8.fromString "michael" &&
44
+                       p == U8.fromString "mypass")
45
+                 (fS "My Realm")