From 6768e16cfcc471674b0a65581cf1ea90ec2e6b98 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 11 Sep 2015 20:48:45 -0400 Subject: Basic functionality to list directories. --- src/Data.hs | 9 +++++++++ src/Main.hs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/PhotoStore.hs | 20 ++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/Data.hs create mode 100644 src/Main.hs create mode 100644 src/PhotoStore.hs (limited to 'src') diff --git a/src/Data.hs b/src/Data.hs new file mode 100644 index 0000000..d40c95b --- /dev/null +++ b/src/Data.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE DeriveGeneric #-} +module Data where + +import GHC.Generics + +data Album = Album + { name :: String + , pending :: Bool + } deriving (Eq, Show, Generic) diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..c40af76 --- /dev/null +++ b/src/Main.hs @@ -0,0 +1,54 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE TemplateHaskell #-} +module Main where + +import Data.Aeson +import GHC.Generics +import Network.Wai +import Network.Wai.Handler.Warp +import Servant +import HFlags +import System.Exit +import Control.Monad +import Data +import PhotoStore +import Control.Monad.IO.Class + +defineFlag "port" (8081 :: Int) "Port to serve on" +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 []) + +instance ToJSON Album + +type PhotoApi = "albums" :> Get '[JSON] [Album] + +config = Config + { pendingPath = flags_pending_path + , photosPath = flags_photos_path + } + +server :: Server PhotoApi +server = liftIO (getAlbums config) + +photoApi :: Proxy PhotoApi +photoApi = Proxy + +app :: Application +app = serve photoApi server + +port = 8081 +settings :: Settings +settings = setHost "*6" . setPort 8081 $ defaultSettings + +main :: IO () +main = do + $initHFlags "photos" + when (flags_pending_path == "") (die "--pending_path must be specified") + when (flags_photos_path == "") (die "--photos_path must be specified") + putStrLn $ "Starting server on port: " ++ (show port) + runSettings settings app diff --git a/src/PhotoStore.hs b/src/PhotoStore.hs new file mode 100644 index 0000000..57b2ccc --- /dev/null +++ b/src/PhotoStore.hs @@ -0,0 +1,20 @@ +module PhotoStore where + +import Data +import System.Directory + +data Config = Config + { pendingPath :: String + , photosPath :: String + } + +validAlbumName name = + name /= "." + && name /= ".." + +getAlbums :: Config -> IO [Album] +getAlbums config = do + pending <- getDirectoryContents (pendingPath config) + permanent <- getDirectoryContents (photosPath config) + return ([Album name True | name <- filter validAlbumName pending] ++ + [Album name False | name <- filter validAlbumName permanent]) -- cgit v1.2.3