Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
k-bx committed Sep 11, 2015
0 parents commit 8ab7736
Show file tree
Hide file tree
Showing 21 changed files with 558 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.stack-work
TAGS
30 changes: 30 additions & 0 deletions owlcloud-albums/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Kostiantyn Rybnikov (c) 2015

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Kostiantyn Rybnikov nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions owlcloud-albums/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
25 changes: 25 additions & 0 deletions owlcloud-albums/owlcloud-albums.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: owlcloud-albums
version: 0.1.0.0
synopsis: Simple project template from stack
description: Please see README.md
homepage: http://github.com/k-bx/owlcloud-albums#readme
license: BSD3
license-file: LICENSE
author: Kostiantyn Rybnikov
maintainer: [email protected]
copyright: Kostiantyn Rybnikov <[email protected]>
category: Web
build-type: Simple
cabal-version: >=1.10

executable owlcloud-albums
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, either
, owlcloud-lib
, servant
, servant-server
, wai
, warp
26 changes: 26 additions & 0 deletions owlcloud-albums/src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Import
import Network.Wai
import Network.Wai.Handler.Warp
import OwlCloud
import Servant

server :: Server AlbumsAPI
server = albums

albums :: Maybe SigninToken -> Maybe SortBy -> EitherT ServantErr IO [Album]
albums mt sortBy = checkingValidity mt $ do
state <- liftIO $ atomically $ readTVar db
return (albumsList state)

albumsAPI :: Proxy AlbumsAPI
albumsAPI = Proxy

app :: Application
app = serve albumsAPI server

main :: IO ()
main = run 8083 app
30 changes: 30 additions & 0 deletions owlcloud-front/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Kostiantyn Rybnikov (c) 2015

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Kostiantyn Rybnikov nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions owlcloud-front/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
31 changes: 31 additions & 0 deletions owlcloud-front/owlcloud-front.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: owlcloud-front
version: 0.1.0.0
synopsis: Simple project template from stack
description: Please see README.md
homepage: http://github.com/k-bx/microservices#readme
license: BSD3
license-file: LICENSE
author: Kostiantyn Rybnikov
maintainer: [email protected]
copyright: Kostiantyn Rybnikov &lt;[email protected]&gt;
category: Web
build-type: Simple
cabal-version: >=1.10

executable owlcloud-front
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: aeson
, base >= 4.7 && < 5
, http-client
, http-types
, lens
, owlcloud-lib
, servant
, servant-server
, string-class
, text
, wai
, warp
, wreq
49 changes: 49 additions & 0 deletions owlcloud-front/src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

-- | This module is a public API implementation

module Main where

import Control.Arrow hiding (app)
import Control.Lens
import Data.Maybe (fromMaybe)
import Data.Monoid
import Data.String.Class (fromStrictByteString, toString)
import Data.Text (Text)
import qualified Data.Text as T
import Network.HTTP.Client hiding (Request, queryString,
requestBody, requestHeaders)
import Network.HTTP.Types (status404)
import Network.Wai
import Network.Wai.Handler.Warp hiding (Manager)
import qualified Network.Wreq as W

app :: Manager -> Application
app mgr req respond =
case pathInfo req of
("api":"users":_) -> microservice "http://localhost:8082/"
("api":"albooms":_) -> microservice "http://localhost:8083/"
_ -> respond (responseLBS status404 [] ",,,(o,o),,,\n ';:`-':;' \n -\"-\"- \n")
where
microservice = microserviceProxy mgr req respond

getReqParams :: Request -> [(Text, Text)]
getReqParams req = map (fromStrictByteString *** fromStrictByteString . fromMaybe "") (queryString req)

microserviceProxy :: Manager -> Request -> (Network.Wai.Response -> IO b) -> Text -> IO b
microserviceProxy mgr req respond basePath = do
let opts = W.defaults & W.manager .~ Right mgr
& W.headers .~ requestHeaders req
& W.params .~ getReqParams req
url = basePath <> T.intercalate "/" (pathInfo req)
r <- case requestMethod req of
"GET" -> W.getWith opts (toString url)
"POST" -> requestBody req >>= W.postWith opts (toString url)
respond (responseLBS (r ^. W.responseStatus) (r ^. W.responseHeaders) (r ^. W.responseBody))

main :: IO ()
main = do
mgr <- newManager defaultManagerSettings
run 8081 (app mgr)
30 changes: 30 additions & 0 deletions owlcloud-lib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Kostiantyn Rybnikov (c) 2015

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Kostiantyn Rybnikov nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions owlcloud-lib/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
36 changes: 36 additions & 0 deletions owlcloud-lib/owlcloud-lib.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: owlcloud-lib
version: 0.1.0.0
synopsis: Simple project template from stack
description: Please see README.md
homepage: http://github.com/k-bx/owlcloud-lib#readme
license: BSD3
license-file: LICENSE
author: Kostiantyn Rybnikov
maintainer: [email protected]
copyright: Kostiantyn Rybnikov &lt;[email protected]&gt;
category: Web
build-type: Simple
cabal-version: >=1.10

library
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: aeson
, base >= 4.7 && < 5
, containers
, either
, servant
, servant-server
, stm
, text
, time
, transformers
, uuid
, wai
, warp
, servant-client
exposed-modules: Import
, OwlCloud
, OwlCloud.Common
, OwlCloud.Types
7 changes: 7 additions & 0 deletions owlcloud-lib/src/Import.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Import
( module X
) where

import Control.Concurrent.STM as X
import Control.Monad.IO.Class as X
import Control.Monad.Trans.Either as X
7 changes: 7 additions & 0 deletions owlcloud-lib/src/OwlCloud.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OwlCloud
( module OwlCloud.Types
, module OwlCloud.Common
) where

import OwlCloud.Types
import OwlCloud.Common
66 changes: 66 additions & 0 deletions owlcloud-lib/src/OwlCloud/Common.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE OverloadedStrings #-}

module OwlCloud.Common where

import Control.Monad (liftM)
import Data.Proxy
import Data.Set (Set)
import qualified Data.Set as Set
import Import
import OwlCloud.Types
import Servant
import Servant.Client
import System.IO.Unsafe (unsafeInterleaveIO, unsafePerformIO)

-- | Database

data State = State
{ validTokens :: Set SigninToken
, albumsList :: [Album] }

db :: TVar State
db = unsafePerformIO (unsafeInterleaveIO (newTVarIO (State Set.empty initialAlbums)))
where
initialAlbums = [Album [Photo "Scating" "http://i.imgur.com/PuhhmQi.jpg"
,Photo "Taking shower" "http://i.imgur.com/v5kqUIM.jpg"]
,Album [Photo "About to fly" "http://i.imgur.com/3hRAGWJ.png"
,Photo "Selfie" "http://i.imgur.com/ArZrhR6.jpg"]]
{-# NOINLINE db #-}

-- | Request-ready microservices API

-- Users API

apiUsersOwlIn :<|> apiUsersOwlOut :<|> apiUsersTokenValidity =
client (Proxy::Proxy UsersAPI) (BaseUrl Http "localhost" 8082)

-- Albums API

apiAlbumsList =
client (Proxy::Proxy AlbumsAPI) (BaseUrl Http "localhost" 8083)

-- | Utils

fly :: (Show b, MonadIO m)
=> EitherT ServantError m b
-> EitherT ServantErr m b
fly apiReq =
either logAndFail return =<< EitherT (liftM Right (runEitherT apiReq))
where
logAndFail e = do
liftIO (putStrLn ("Got internal-api error: " ++ show e))
left internalError
internalError = ServantErr 500 "Internal Server Error" "" []

checkingValidity :: Maybe SigninToken
-> EitherT ServantErr IO a
-> EitherT ServantErr IO a
checkingValidity mt f =
maybe
(left (ServantErr 400 "Please, provide an authorization token" "" []))
(\t -> fly (apiUsersTokenValidity t) >>= handleValidity >> f)
mt
where
handleValidity (TokenValidity True) = return ()
handleValidity (TokenValidity False) =
left (ServantErr 400 "Your authorization token is invalid" "" [])
Loading

0 comments on commit 8ab7736

Please sign in to comment.