diff --git a/services/proxy/package.yaml b/services/proxy/package.yaml index c5459360b30..8971abac3bb 100644 --- a/services/proxy/package.yaml +++ b/services/proxy/package.yaml @@ -20,6 +20,7 @@ library: - Proxy.Env - Proxy.Options - Proxy.Proxy + - Proxy.Run dependencies: - base >=4.6 && <5 - aeson >=1.0 diff --git a/services/proxy/src/Main.hs b/services/proxy/src/Main.hs index 241cd819c59..e9f90a0f47d 100644 --- a/services/proxy/src/Main.hs +++ b/services/proxy/src/Main.hs @@ -1,7 +1,7 @@ module Main (main) where import Imports -import Proxy.API +import Proxy.Run (run) import Proxy.Options import Util.Options diff --git a/services/proxy/src/Proxy/API.hs b/services/proxy/src/Proxy/API.hs index 6916da069e1..8cfd5124135 100644 --- a/services/proxy/src/Proxy/API.hs +++ b/services/proxy/src/Proxy/API.hs @@ -1,4 +1,4 @@ -module Proxy.API (Proxy.API.run) where +module Proxy.API (sitemap) where import Imports hiding (head) import Control.Monad.Catch @@ -7,19 +7,15 @@ import Control.Retry import Data.ByteString (breakSubstring) import Data.CaseInsensitive (CI) import Data.Metrics.Middleware hiding (path) -import Data.Metrics.WaiRoute (treeToPaths) import Network.HTTP.ReverseProxy import Network.HTTP.Types import Network.Wai -import Network.Wai.Handler.Warp import Network.Wai.Predicate hiding (err, Error, setStatus) import Network.Wai.Predicate.Request (getRequest) import Network.Wai.Routing hiding (path, route) import Network.Wai.Utilities -import Network.Wai.Utilities.Server hiding (serverPort) import Proxy.Env import Proxy.Proxy -import Proxy.Options import System.Logger.Class hiding (Error, info, render) import qualified Bilge.Request as Req @@ -33,17 +29,6 @@ import qualified Network.HTTP.Client as Client import qualified Network.Wai.Internal as I import qualified System.Logger as Logger -run :: Opts -> IO () -run o = do - m <- metrics - e <- createEnv m o - s <- newSettings $ defaultServer (o^.host) (o^.port) (e^.applog) m - let rtree = compile (sitemap e) - let measured = measureRequests m (treeToPaths rtree) - let app r k = runProxy e r (route rtree r k) - let start = measured . catchErrors (e^.applog) m $ app - runSettings s start `finally` destroyEnv e - sitemap :: Env -> Routes a Proxy () sitemap e = do diff --git a/services/proxy/src/Proxy/Run.hs b/services/proxy/src/Proxy/Run.hs new file mode 100644 index 00000000000..ba58f878304 --- /dev/null +++ b/services/proxy/src/Proxy/Run.hs @@ -0,0 +1,27 @@ +module Proxy.Run (run) where + +import Imports hiding (head) +import Control.Monad.Catch +import Control.Lens hiding ((.=)) +import Data.Metrics.Middleware hiding (path) +import Data.Metrics.Middleware.Prometheus (waiPrometheusMiddleware) +import Data.Metrics.WaiRoute (treeToPaths) +import Network.Wai.Utilities.Server hiding (serverPort) +import Network.Wai.Handler.Warp (runSettings) +import Proxy.Env +import Proxy.Proxy +import Proxy.Options +import Proxy.API (sitemap) + +run :: Opts -> IO () +run o = do + m <- metrics + e <- createEnv m o + s <- newSettings $ defaultServer (o^.host) (o^.port) (e^.applog) m + let rtree = compile (sitemap e) + let measured = measureRequests m (treeToPaths rtree) + let app r k = runProxy e r (route rtree r k) + let middleware = waiPrometheusMiddleware (sitemap e) + . measured + . catchErrors (e^.applog) m + runSettings s (middleware app) `finally` destroyEnv e