This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE OverloadedStrings #-} | |
| module Article where | |
| import Data.Text.Lazy | |
| import Data.Text.Lazy.Encoding | |
| import Data.Aeson | |
| import Control.Applicative | |
| -- Define the Article constructor | |
| -- e.g. Article 12 "some title" "some body text" | |
| data Article = Article Integer Text Text -- id title bodyText |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE OverloadedStrings #-} | |
| import Web.Scotty | |
| import Network.HTTP.Types | |
| main = scotty 3000 $ do | |
| get "/" $ do -- handle GET request on "/" URL | |
| text "This was a GET request!" -- send 'text/plain' response | |
| delete "/" $ do | |
| html "This was a DELETE request!" -- send 'text/html' response | |
| post "/" $ do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- set a header: | |
| post "/set-headers" $ do | |
| status status302 -- Respond with HTTP 302 status code | |
| setHeader "Location" "http://www.google.com.au" | |
| -- named parameters: | |
| get "/askfor/:word" $ do | |
| w <- param "word" | |
| html $ mconcat ["<h1>You asked for ", w, ", you got it!</h1>" ] | |
| -- unnamed parameters from a query string or a form: | |
| post "/submit" $ do -- e.g. http://server.com/submit?name=somename |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| main = scotty 3000 $ do | |
| -- get article (json) | |
| get "/article" $ do | |
| json $ Article 13 "caption" "content" -- Call Article constructor and encode the result as JSON | |
| -- post article (json) | |
| post "/article" $ do | |
| article <- jsonData :: ActionM Article -- Decode body of the POST request as an Article object | |
| json article -- Send the encoded object back as JSON |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM haskell:7.10 | |
| RUN cabal update | |
| # Add .cabal file | |
| ADD ./scotty-webapp-example.cabal /opt/server/scotty-webapp-example.cabal | |
| # Docker will cache this command as a layer, freeing us up to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function toArray(arrayLikeObject) { | |
| return Array.prototype.slice.call(arrayLikeObject); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var result = []; | |
| for (var i=0; i < 5; i++) { | |
| result.push(function () { return i }); // (1) | |
| } | |
| console.log(result[1]()); // 5 (not 1) | |
| console.log(result[3]()); // 5 (not 3) | |
| for (var i=0; i < 5; i++) { | |
| (function () { | |
| var i2 = i; // copy current i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:PermSize=512M -XX:MaxPermSize=1024M" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sleep(milliSeconds) { | |
| var startTime = new Date().getTime(); | |
| while (new Date().getTime() < startTime + milliSeconds); | |
| } | |
| sleep(10000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % cd /usr/local/Cellar/postgresql/9.4.5_2/bin | |
| # -d user can create databases | |
| # -P prompt for user password | |
| # -E encrypt user's password | |
| % createuser -d -P -E db_user |