Commit 6f44b4a2 authored by Yuanle Song's avatar Yuanle Song
Browse files

use warp to host static files;

bugfix: worker should close file when done
parent 44b76f7d
Loading
Loading
Loading
Loading
+16 −9
Original line number Diff line number Diff line
@@ -5,18 +5,21 @@ import Data.String (fromString)
import System.Environment (lookupEnv)
import Data.Monoid ((<>))
import Control.Concurrent.Chan
import System.Directory (setCurrentDirectory)
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT

import Web.Scotty
import Network.Wai.Handler.Warp (defaultSettings, setHost, setPort)
import Network.Wai.Handler.Warp
import Formatting
import Log
import Log.Backend.StandardOutput
-- import Network.Wai.Application.Static
import Network.Wai.Middleware.Static
import qualified Database.Redis as R

import Config
import App (mkApp)
import App (mkWaiApp)
import Worker (startWorkers)

-- TODO use a proper config lib.
@@ -46,10 +49,14 @@ main = withSimpleStdOutLogger $ \logger -> do
  startWorkers runtimeConfig
  runLogT "Main" logger $
    logInfo_ $ sformat ("will listen on " % string % ":" % int) (host config) (port config)
  let opts = Options { verbose=0
                     , settings=warpSettings
                     }
          where
            warpSettings = setPort (port config)
                           (setHost (fromString $ host config) defaultSettings)
  scottyOpts opts $ mkApp runtimeConfig
  let warpSettings = ( setFdCacheDuration 10
                     . setFileInfoCacheDuration 10
                     . setPort (port config)
                     . setHost (fromString $ host config)) defaultSettings
  rdApi <- mkWaiApp runtimeConfig
  -- let staticApp = staticApp $ defaultFileServerSettings $ webRoot config
  -- runSettings warpSettings rdApi
  setCurrentDirectory (webRoot config)    -- static app only support serving
                                          -- from PWD
  let app = static rdApi
  runSettings warpSettings app
+45 −5
Original line number Diff line number Diff line
@@ -157,11 +157,6 @@ https://github.com/scotty-web/scotty/tree/master/examples
- 

* later                                                               :entry:
** 2018-05-05 hosting static file via warp?
- try it. it makes development easier.
- make sure Range header is supported.
- 

** 2018-05-05 add logging support
- hslogger
  http://hackage.haskell.org/package/hslogger-1.2.10/docs/System-Log-Logger.html
@@ -295,7 +290,52 @@ that way you don't need to tell rd-server the web root dir.

* current                                                             :entry:
** 
** 2018-05-06 write client side tool to actually do the download.
- make this work:
  env WEB_ROOT=/home/sylecn/persist/cache stack exec rd-api

  rd http://localhost:8082/ideaIC-2018.1.tar.gz
- 

* done                                                                :entry:
** 2018-05-05 hosting static file via warp?
- try it. it makes development easier.
- make sure Range header is supported.
- 2018-05-06 wai-app-static
  try it.

  settingsFdCacheDuration default is 0 

- how to mount app at /rd/ and /static/?
- use WAI middleware.
  this can serve / and /rd/. even better.

  wai-middleware-static
  doesn't support file listing?
  only support the current directory.

  try it.
  cd d/t2
  curl http://localhost:8082/sdkman.sh
  it works. and it supports Range header.
  curl -vvv -o t1.out -r0-1 http://localhost:8082/sdkman.sh
  curl -vvv -o t2.out -r0-9 http://localhost:8082/sdkman.sh
  it works.

  is file listing supported?
  curl http://localhost:8082/
  404, so no.

- the wai-middleware-static is only 274 lines.
  https://github.com/scotty-web/wai-middleware-static/blob/master/Network/Wai/Middleware/Static.hs

- wai-app-static is only 288 lines.
  https://github.com/yesodweb/wai/blob/master/wai-app-static/Network/Wai/Application/Static.hs

- to mount app at different url, it should be easy to just write a middleware.
- let me just use wai-middleware-static without dir listing for now.

** 2018-05-06 do I need to close handle when done?
** 2018-05-06 integrate hlint
stack install hlint

+3 −0
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ dependencies:
  - base >= 4.7 && < 5
  - wai
  - warp
  # - wai-app-static
  - wai-middleware-static
  - directory
  - scotty
  - cryptohash
  - hedis
+2 −2
Original line number Diff line number Diff line
module Worker (startWorkers) where

import Control.Concurrent.Chan
import System.IO (IOMode(ReadMode))
import System.IO (IOMode(ReadMode), hClose)
import GHC.IO.Handle (Handle, hTell, hSeek, SeekMode(AbsoluteSeek))
import GHC.IO.Handle.FD (openBinaryFile)
import Control.Monad (when, replicateM_)
@@ -38,7 +38,7 @@ fileWorker runtimeConfig = do
    Right _ -> do
      putStrLn $ "set file status to " <> show resultStatus <> " for " <> filepath
      putStrLn $ "file handling done for " <> filepath
  -- GET /rd/file should enqueue if status is error.
  hClose handle
  return ()
    where
      -- | calculate sha1 for a single block. return True on success.