Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 27, 2012
1 parent 7156cd3 commit 5eecb64
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dist
cabal-dev
tags
*.o
*.hi
*.chi
Expand Down
30 changes: 30 additions & 0 deletions active-cache-wai/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2012, yihuang

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 yihuang 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.
1 change: 1 addition & 0 deletions active-cache-wai/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This service
2 changes: 2 additions & 0 deletions active-cache-wai/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
41 changes: 41 additions & 0 deletions active-cache-wai/Simple.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Network.Wai
import Network.Wai.Handler.Warp (run)
import qualified Network.HTTP.Conduit as HTTP
import Network.HTTP.Types
import Data.Monoid
import qualified Blaze.ByteString.Builder as B
import qualified Data.ByteString.Lazy as L
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class (liftIO)

type SimpleState = L.ByteString

-- handle http requests
app :: MVar SimpleState -> Application
app state req = case pathInfo req of
[] -> do
d <- liftIO (takeMVar state)
return $ response d
_ -> return response404
where
response = ResponseBuilder status200 [] . B.fromLazyByteString
response404 = ResponseBuilder status404 [] mempty

-- update the cache periodically.
cacher :: String -> MVar SimpleState -> Int -> IO ()
cacher url state interval = forever $ do
ersp <- try (HTTP.simpleHttp url)
case ersp of
Right rsp -> void $ swapMVar state rsp
Left err -> print (err::SomeException)
threadDelay interval

main :: IO ()
main = do
let port = 3000
state <- newEmptyMVar :: IO (MVar SimpleState)
_ <- forkIO (cacher "http://10.10.10.3:9002/" state 1000)
putStrLn $ "http://localhost:"++show port
run port (app state)
53 changes: 53 additions & 0 deletions active-cache-wai/StructuredState.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{-# LANGUAGE DeriveGeneric #-}
import Network.Wai
import Network.Wai.Handler.Warp (run)
import qualified Network.HTTP.Conduit as HTTP
import Network.HTTP.Types
import GHC.Generics
import Data.Monoid
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import qualified Data.Vector as V
import qualified Blaze.ByteString.Builder as B
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class (liftIO)

newtype SimpleState = SimpleState Int
deriving (Generic)

instance ToJSON SimpleState
instance FromJSON SimpleState where
parseJSON (Array vec) = return $ SimpleState $ V.length vec
parseJSON o = typeMismatch "SimpleState" o

-- handle http requests
app :: MVar SimpleState -> Application
app state req = case pathInfo req of
[] -> do
d <- liftIO (takeMVar state)
return $ response (encode d)
_ -> return response404
where
response = ResponseBuilder status200 [] . B.fromLazyByteString
response404 = ResponseBuilder status404 [] mempty

-- update the cache periodically.
cacher :: String -> MVar SimpleState -> Int -> IO ()
cacher url state interval = forever $ do
ersp <- try (HTTP.simpleHttp url)
case ersp of
Right rsp -> maybe (putStrLn "decode json failed.")
(void . swapMVar state)
(decode rsp)
Left err -> print (err::SomeException)
threadDelay interval

main :: IO ()
main = do
let port = 3000
state <- newEmptyMVar :: IO (MVar SimpleState)
_ <- forkIO (cacher "http://10.10.10.3:9002/" state 1000)
putStrLn $ "http://localhost:"++show port
run port (app state)
23 changes: 23 additions & 0 deletions active-cache-wai/active-cache-wai.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Initial active-cache-wai.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: active-cache-wai
version: 0.1.0.0
synopsis: a cache service that update actively.
-- description:
license: BSD3
license-file: LICENSE
author: yihuang
maintainer: yi.codeplayer@gmail.com
-- copyright:
category: Web
build-type: Simple
cabal-version: >=1.8

executable active-cache-wai-Simple
main-is: Simple.hs
build-depends: base ==4.5.*, wai ==1.1.*, warp ==1.1.*, http-conduit ==1.2.*, http-types ==0.6.*, ghc-prim ==0.2.*, aeson ==0.6.*, vector ==0.9.*, blaze-builder ==0.3.*, transformers ==0.2.*, bytestring ==0.9.*

executable active-cache-wai-StructuredState
main-is: StructuredState.hs
build-depends: base ==4.5.*, wai ==1.1.*, warp ==1.1.*, http-conduit ==1.2.*, http-types ==0.6.*, ghc-prim ==0.2.*, aeson ==0.6.*, vector ==0.9.*, blaze-builder ==0.3.*, transformers ==0.2.*, bytestring ==0.9.*
30 changes: 30 additions & 0 deletions active-cache-wai/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
0a1
> {-# LANGUAGE DeriveGeneric #-}
4a6
> import GHC.Generics
5a8,10
> import Data.Aeson
> import Data.Aeson.Types (typeMismatch)
> import qualified Data.Vector as V
7d11
< import qualified Data.ByteString.Lazy as L
13c17,23
< type SimpleState = L.ByteString
---
> newtype SimpleState = SimpleState Int
> deriving (Generic)
>
> instance ToJSON SimpleState
> instance FromJSON SimpleState where
> parseJSON (Array vec) = return $ SimpleState $ V.length vec
> parseJSON o = typeMismatch "SimpleState" o
20c30
< return $ response d
---
> return $ response (encode d)
31c41,43
< Right rsp -> void $ swapMVar state rsp
---
> Right rsp -> maybe (putStrLn "decode json failed.")
> (void . swapMVar state)
> (decode rsp)

0 comments on commit 5eecb64

Please sign in to comment.