-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
dist | ||
cabal-dev | ||
tags | ||
*.o | ||
*.hi | ||
*.chi | ||
|
This file contains 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
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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This service |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains 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
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) |
This file contains 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
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) |
This file contains 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
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.* |
This file contains 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
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) |