From 5f737f2ebbd0fdaf5013b8de95e59ab0bd9098f2 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 9 Oct 2015 17:26:57 -0400 Subject: Include code to pass a request header to servant. --- run.sh | 1 + src/Main.hs | 9 +++++++-- web/index.html | 15 +++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) mode change 100644 => 100755 run.sh diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 index dd066c2..4a0eade --- a/run.sh +++ b/run.sh @@ -1 +1,2 @@ +#!/bin/bash stack exec photos -- --pending_path=/tmp --photos_path=/tmp diff --git a/src/Main.hs b/src/Main.hs index ba1acd4..3eed3cc 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -20,10 +20,10 @@ import Control.Monad.IO.Class import Control.Monad.Trans.Either defineFlag "port" (8081 :: Int) "Port to serve on" -defineFlag "host" ("*6" :: String) "Host to serve on (*6 for ipv6 mode)" +defineFlag "host" ("*6" :: String) "Host to serve on (*6 for ipv6 mode)" defineFlag "pending_path" ("" :: String) "Path to pending albums" defineFlag "photos_path" ("" :: String) "Path to permanent albums" -$(return []) +$(return []) -- Somehow forces the flags to be set. instance ToJSON Album instance FromJSON Album @@ -38,6 +38,8 @@ instance ToJSON RenameError type PhotoApi = "albums" :> Get '[JSON] [Album] :<|> "rename" :> ReqBody '[JSON] RenameRequest :> Post '[JSON] (Either RenameError ()) + :<|> "test" :> Header "X-Token" String :> Get '[JSON] () +-- Introduce request header containing auth information. config = Config { pendingPath = flags_pending_path @@ -47,6 +49,7 @@ config = Config server :: Server PhotoApi server = albums :<|> rename + :<|> test where albums = liftIO (getAlbums config) @@ -54,6 +57,8 @@ server = albums rename (RenameRequest from to) = liftIO $ runEitherT (renameAlbum config from to) + test token = return () + photoApi :: Proxy PhotoApi photoApi = Proxy diff --git a/web/index.html b/web/index.html index f5ee27c..d1819b0 100644 --- a/web/index.html +++ b/web/index.html @@ -109,10 +109,17 @@ $(document).ready(function() { fetchAlbums(); - var request = { - from: { name: 'x', pending: true }, - to: { name: 'y', pending: false } - }; + $.ajax({ + url: '/api/test', + type: 'GET', + headers: {"X-Token": "foo"}, + success: function(data) { + console.log("Success. got response " + data); + }, + error: function(data) { + console.log("Success. got response " + data); + } + }); }); -- cgit v1.2.3