Commit c6e5a1e7 authored by Yuanle Song's avatar Yuanle Song
Browse files

allow rd-api run without redis

it will act like a static file server
parent f0213b38
Loading
Loading
Loading
Loading
+19 −11
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ module Main (main) where
import Data.String (fromString)
import System.Environment (lookupEnv)
import Data.Monoid ((<>))
import Control.Monad (mzero, when)
import Control.Monad (when)
import Control.Monad.IO.Class (liftIO)
import System.Directory (setCurrentDirectory)

@@ -11,7 +11,6 @@ import Network.Wai.Handler.Warp
import Formatting
import Network.Wai.Middleware.Static
import Options.Applicative
import System.Log.FastLogger
import Control.Error
import System.Exit (die)
import qualified Database.Redis as R
@@ -42,18 +41,27 @@ runApiServer rdConfig = do
            R.connectHost=redisHost config
          , R.connectPort=R.PortNumber (fromIntegral (redisPort config))
          }
  conn <- case connEi of
  connMaybe <- case connEi of
    Left e -> do
      liftIO $ pushLogStrLn (rcLoggerSet rc0) $ toLogStr $ sformat
      liftIO $ do
        logl rc0 $ sformat
          ("Connect to redis at " % string % ":" % int % " failed: " % stext)
          (redisHost config) (redisPort config) e
      mzero
        logl rc0 $ sformat "No redis, GET /rd/ api disabled, acting as static file server"
      return Nothing
    Right conn ->
      return conn
  let rc = rc0 { rcConfig=config
      return $ Just conn
  let rc = case connMaybe of
             Nothing -> rc0 { rcConfig=config
                            , rcHasRedis=False }
             Just conn -> rc0 { rcConfig=config
                              , rcHasRedis=True
                              , rcRedisConn=conn }
  liftIO $ do
    if rcHasRedis rc then
        startWorkers rc
    else
        logl rc $ sformat "No redis, not starting workers"
    logl rc $ sformat ("webRoot is " % string) (webRoot config)
    logl rc $ sformat ("will listen on " % string % ":" % int) (host config) (port config)
    let warpSettings = ( setFdCacheDuration 10
+13 −8
Original line number Diff line number Diff line
@@ -304,14 +304,6 @@ execParser can be extended to handle envvar.

- check whether there is support for envvar in a pkg.

** 2018-05-08 rd-api, allow serve static file without redis-server.
--disable-rd-api
if set, do not provides /rd/ api. just act like a static file server.
this does not require redis at all.

if not set, require redis connection at start up time. show error explicitly
if can't connect to redis.

** 2018-05-07 loopUntilAllBlocksReady, how to track progress?
use a thread pool to download blocks, print overall progress when some parts
done or some time elapsed.
@@ -416,6 +408,19 @@ I remember there are tools that can simulate packet loss.
policy in ovs can do it.

* done                                                                :entry:
** 2018-05-08 rd-api, allow serve static file without redis-server.
--disable-rd-api
if set, do not provides /rd/ api. just act like a static file server.
this does not require redis at all.

if not set, require redis connection at start up time. show error explicitly
if can't connect to redis.

- 2018-05-10 just return error if there is no redis connection.
  add a runtimeConfig that is a Maybe R.Connection.

  don't start workers if no redis connection.

** 2018-05-08 humanReadableSize: round up when convert byte to MiB. rd shows 377 MiB
or just show as floating point with 1 digit after point.

+1 −1
Original line number Diff line number Diff line
name:                reliable-download
version:             1.0.1.0
version:             1.0.2.0
synopsis:            provide reliable download service via HTTP
description:         reliable-download web application and cli tool
homepage:            "https://github.com/sylecn/reliable-download#readme"
+3 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import Data.Monoid ((<>))
import Data.Text.Encoding (decodeUtf8)
import Control.Concurrent.Chan
import System.IO.Error (catchIOError)
import Control.Monad (when)
import Control.Monad (when, unless)
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT

@@ -73,6 +73,8 @@ processNewFileAsyncMaybe rc fbp = do
-- | GET /rd/.* handler
getRdHandler :: RDRuntimeConfig -> ExceptT T.Text ActionM ()
getRdHandler rc = do
  unless (rcHasRedis rc) $
      throwE "No redis connection, GET /rd/ disabled"
  path <- lift $ param "1"

  let filepath = webRoot (rcConfig rc) </> T.unpack path
+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ data RDConfig = RDConfig {
data RDRuntimeConfig = RDRuntimeConfig {
      rcConfig :: RDConfig
    , rcRedisConn :: R.Connection
    , rcHasRedis :: Bool
    , rcFileChan :: Chan FillBlockParam
    , rcLoggerSet :: LoggerSet
    , rcLoggerTimeCache :: IO FormattedTime }
@@ -47,6 +48,7 @@ defaultRDRuntimeConfig = do
  return RDRuntimeConfig {
               rcConfig=defaultRDConfig
             , rcRedisConn=conn
             , rcHasRedis=True
             , rcFileChan=fileChan
             , rcLoggerSet=loggerSet
             , rcLoggerTimeCache=loggerTimeCache }