Adds file descriptor and info caching
parent
0c1345b877
commit
faef893e09
|
@ -1,6 +1,6 @@
|
||||||
# hastatic
|
# hastatic
|
||||||
|
|
||||||
[![Docker Build Status](https://img.shields.io/docker/build/abhin4v/hastatic.svg?style=flat-square)](https://hub.docker.com/r/abhin4v/hastatic/) ![Docker Pulls](https://img.shields.io/docker/pulls/abhin4v/hastatic.svg?style=flat-square) ![MicroBadger Size](https://img.shields.io/microbadger/image-size/abhin4v/hastatic.svg?style=flat-square)
|
[![Docker Build Status](https://img.shields.io/docker/build/abhin4v/hastatic.svg?style=flat-square)](https://hub.docker.com/r/abhin4v/hastatic/) [![Docker Pulls](https://img.shields.io/docker/pulls/abhin4v/hastatic.svg?style=flat-square)](https://hub.docker.com/r/abhin4v/hastatic/) [![MicroBadger Size](https://img.shields.io/microbadger/image-size/abhin4v/hastatic.svg?style=flat-square)](https://hub.docker.com/r/abhin4v/hastatic/)
|
||||||
|
|
||||||
_hastatic_ is a tiny static web server for Docker.
|
_hastatic_ is a tiny static web server for Docker.
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ _hastatic_ is a tiny static web server for Docker.
|
||||||
- Supports custom index files for URLs ending with "/".
|
- Supports custom index files for URLs ending with "/".
|
||||||
- Takes care to not serve hidden files.
|
- Takes care to not serve hidden files.
|
||||||
- Adds caching headers automatically.
|
- Adds caching headers automatically.
|
||||||
|
- Caches file descriptors and info for better performance.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -51,4 +52,4 @@ The Docker image supports these environment variable for configuration:
|
||||||
|
|
||||||
## Internals
|
## Internals
|
||||||
|
|
||||||
_hastatic_ is written in Haskell, just 60 lines of it. It uses the excellent [Warp](https://hackage.haskell.org/package/warp) server underneath with the [warp-tls](https://hackage.haskell.org/package/warp-tls) package for TLS support.
|
_hastatic_ is written in Haskell, just 60 lines of it. It uses the excellent [Warp](https://hackage.haskell.org/package/warp) server underneath with the [warp-tls](https://hackage.haskell.org/package/warp-tls) package for HTTPS support.
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
--
|
--
|
||||||
-- see: https://github.com/sol/hpack
|
-- see: https://github.com/sol/hpack
|
||||||
--
|
--
|
||||||
-- hash: e7a5e0192bba9ed055c360b88189436d278d96883c36e66a4a17b70f134f4654
|
-- hash: 5fa75b100431a32f877dee2e76911b04d100a54f9562e0eb2f0500e9f6fded00
|
||||||
|
|
||||||
name: hastatic
|
name: hastatic
|
||||||
version: 0.9.0
|
version: 0.9.0
|
||||||
|
@ -26,6 +26,7 @@ executable hastatic
|
||||||
ghc-options: -threaded -rtsopts -with-rtsopts=-N -O2
|
ghc-options: -threaded -rtsopts -with-rtsopts=-N -O2
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, bytestring
|
||||||
, http-types
|
, http-types
|
||||||
, text
|
, text
|
||||||
, wai
|
, wai
|
||||||
|
|
|
@ -29,3 +29,4 @@ executables:
|
||||||
- http-types
|
- http-types
|
||||||
- wai-middleware-static
|
- wai-middleware-static
|
||||||
- text
|
- text
|
||||||
|
- bytestring
|
||||||
|
|
26
src/Main.hs
26
src/Main.hs
|
@ -1,17 +1,26 @@
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
import qualified Data.ByteString.Char8 as C8
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import qualified Data.List as List
|
import qualified Data.List as List
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import Data.Version (showVersion)
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
import qualified Network.Wai.Handler.WarpTLS as TLS
|
import qualified Network.Wai.Handler.WarpTLS as TLS
|
||||||
import Network.Wai.Middleware.Static
|
import Network.Wai.Middleware.Static
|
||||||
import Network.HTTP.Types (status404)
|
import Network.HTTP.Types (status404)
|
||||||
import Network.Wai.Handler.Warp (run, defaultSettings, setPort)
|
import Network.Wai.Handler.Warp ( runSettings
|
||||||
|
, defaultSettings
|
||||||
|
, setPort
|
||||||
|
, setFdCacheDuration
|
||||||
|
, setFileInfoCacheDuration
|
||||||
|
, setServerName
|
||||||
|
)
|
||||||
import System.Exit (die)
|
import System.Exit (die)
|
||||||
import System.Environment (lookupEnv)
|
import System.Environment (lookupEnv)
|
||||||
import Text.Read (readMaybe)
|
import Text.Read (readMaybe)
|
||||||
|
import Paths_hastatic (version)
|
||||||
|
|
||||||
data TLS = Okay TLS.TLSSettings | Error String | None
|
data TLS = Okay TLS.TLSSettings | Error String | None
|
||||||
|
|
||||||
|
@ -56,18 +65,23 @@ application excludedPaths = do
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
mPort <- lookupEnv "PORT"
|
mPort <- lookupEnv "PORT"
|
||||||
let port = fromMaybe 3000 (readMaybe =<< mPort)
|
let port = fromMaybe 3000 (readMaybe =<< mPort)
|
||||||
tlsSettings <- getTLSSettings
|
tlsSettings <- getTLSSettings
|
||||||
|
let settings = setPort port
|
||||||
|
. setFdCacheDuration 10
|
||||||
|
. setFileInfoCacheDuration 10
|
||||||
|
. setServerName ("hastatic-" <> C8.pack (showVersion version))
|
||||||
|
$ defaultSettings
|
||||||
|
|
||||||
case tlsSettings of
|
case tlsSettings of
|
||||||
Okay tls -> do
|
Okay tls -> do
|
||||||
app <- application [TLS.certFile tls, TLS.keyFile tls]
|
app <- application [TLS.certFile tls, TLS.keyFile tls]
|
||||||
putStrLn $ "Starting HTTPS server on port: " <> show port
|
putStrLn $ "Starting HTTPS server on port: " <> show port
|
||||||
TLS.runTLS tls (setPort port defaultSettings) app
|
TLS.runTLS tls settings app
|
||||||
None -> do
|
None -> do
|
||||||
app <- application []
|
app <- application []
|
||||||
putStrLn $ "Starting HTTP server on port: " <> show port
|
putStrLn $ "Starting HTTP server on port: " <> show port
|
||||||
run port app
|
runSettings settings app
|
||||||
Error msg ->
|
Error msg ->
|
||||||
die $ "Error starting server: " <> msg
|
die $ "Error starting server: " <> msg
|
||||||
|
|
Loading…
Reference in New Issue