diff --git a/config.sample.yaml b/config.sample.yaml index 48437b11cbe7c79bd4ff0d7f10532547b3534f34..deca5faa416db565c715052c3190bc52d43d39ab 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -127,6 +127,9 @@ server: notFoundHandler: tag: NotFoundPath contents: "/404" + cacheControl: + tag: CacheSeconds + contents: 604801 # bundle: # tag: S3Bundle # contents: @@ -135,6 +138,7 @@ server: # endpoint: https://minio # bucket: example # pollSeconds: 30 +# objectName: bundle.tar.gz bundle: tag: LocalBundle contents: diff --git a/src/Espresso/Config.hs b/src/Espresso/Config.hs index ac4c32029da0470f57a6440a5439ccccf530a889..3067fa93ee3f76e893dd8944c587e2ce2777edb4 100644 --- a/src/Espresso/Config.hs +++ b/src/Espresso/Config.hs @@ -45,6 +45,7 @@ data S3BundleConfig = S3BundleConfig , secretKey :: Text , endpoint :: Text , bucket :: Text + , objectName :: Text , pollSeconds :: Int } deriving (Generic, Show) diff --git a/src/Main.hs b/src/Main.hs index 127d9500d3fd77dcdb29601cc3bb6de2f6ef715f..d00f6c37c0aabf213f882c634170031d80f00a8a 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -44,6 +44,7 @@ import Data.Default ( def ) import Data.Generics.Labels ( ) import Data.Maybe ( fromMaybe ) import Data.Monoid ( (<>) ) +import Data.Text ( Text ) import qualified Data.Text as T import Data.String ( fromString ) @@ -125,19 +126,26 @@ main = do LocalBundle bundleConfig -> do path_ <- liftIO $ makeAbsolute (T.unpack $ bundleConfig ^. #path) + $(logInfo) + $ "Bundle: Using local path backend (" + <> (T.pack path_) + <> ")." + liftIO $ runServer config path_ S3Bundle bundleConfig -> do + $(logInfo) "Bundle: Using object storage backend." + cwd <- liftIO getCurrentDirectory let runDir = cwd </> "run/" let serveDir = runDir </> "serve/" - let bundlePath = runDir </> "bundle.tar.gz" + let bundlePath = runDir </> (T.unpack bundleObjectName) liftIO $ createDirectoryIfMissing True runDir $(logInfo) ("Bucket: " <> bucket) - minioResult <- downloadBundle cb3CI bucket bundlePath + minioResult <- downloadBundle cb3CI bucket bundlePath bundleObjectName case minioResult of Left e -> $(logError) $ "getObject failed." <> T.pack (show e) @@ -156,10 +164,12 @@ main = do etagMVar restartChan bundlePath + bundleObjectName (bundleConfig ^. #pollSeconds) where bucket = bundleConfig ^. #bucket + bundleObjectName = bundleConfig ^. #objectName endpointUrl = T.unpack $ bundleConfig ^. #endpoint cb3CI = setCreds (Credentials (bundleConfig ^. #accessKey) (bundleConfig ^. #secretKey)) @@ -170,12 +180,13 @@ downloadBundle => ConnectInfo -> Bucket -> FilePath + -> Text -> a (Either MinioErr GetObjectResponse) -downloadBundle connectInfo bucket bundlePath = do +downloadBundle connectInfo bucket bundlePath bundleObjectName = do $(logInfo) "Downloading latest bundle..." liftIO $ runMinio connectInfo $ do - response <- getObject bucket "bundle.tar.gz" defaultGetObjectOptions + response <- getObject bucket bundleObjectName defaultGetObjectOptions C.connect (gorObjectStream response) $ CB.sinkFileCautious bundlePath @@ -188,42 +199,47 @@ updater -> MVar ETag -> Chan Bool -> FilePath + -> Text -> Int -> m () -updater connectInfo bucket etagMVar restartChan bundlePath pollSeconds = do - threadDelay (1000000 * pollSeconds) +updater connectInfo bucket etagMVar restartChan bundlePath bundleObjectName pollSeconds + = do + threadDelay (1000000 * pollSeconds) - $(logInfo) "Checking for bundle updates..." + $(logInfo) "Checking for bundle updates..." - minioResult <- liftIO $ runMinio connectInfo $ statObject - bucket - "bundle.tar.gz" - defaultGetObjectOptions + minioResult <- liftIO $ runMinio connectInfo $ statObject + bucket + bundleObjectName + defaultGetObjectOptions - case minioResult of - Left e -> $(logError) $ "statObject failed." <> T.pack (show e) - Right response -> do - currentETag <- readMVar etagMVar - let newETag = oiETag response + case minioResult of + Left e -> $(logError) $ "statObject failed." <> T.pack (show e) + Right response -> do + currentETag <- readMVar etagMVar + let newETag = oiETag response - if newETag == currentETag - then $(logInfo) "No changes" - else do - $(logInfo) $ "Got new ETag: " <> oiETag response + if newETag == currentETag + then $(logInfo) "No changes" + else do + $(logInfo) $ "Got new ETag: " <> oiETag response - minioResult_ <- downloadBundle connectInfo bucket bundlePath + minioResult_ <- downloadBundle connectInfo + bucket + bundlePath + bundleObjectName - case minioResult_ of - Left e -> $(logError) $ "getObject failed." <> T.pack (show e) - Right response -> do - $(logInfo) "Sucessfully downloaded new bundle." + case minioResult_ of + Left e -> $(logError) $ "getObject failed." <> T.pack (show e) + Right response -> do + $(logInfo) "Sucessfully downloaded new bundle." - -- Update eTag. - _ <- takeMVar etagMVar - putMVar etagMVar newETag + -- Update eTag. + _ <- takeMVar etagMVar + putMVar etagMVar newETag - -- Schedule restart of web server. - writeChan restartChan True + -- Schedule restart of web server. + writeChan restartChan True foreverServer :: (MonadIO m, MonadLogger m, MonadBaseControl IO m) @@ -267,6 +283,7 @@ prepareServeDir serveDir = do runServer :: Config -> FilePath -> IO () runServer config serveDir = do let serverConfig = config ^. #server + stage_ <- lookupEnvOrDefault "SITE_STAGE" (serverConfig ^. #stage) let serverEnvironmentConfig = case stage_ of @@ -275,4 +292,3 @@ runServer config serveDir = do Production -> serverConfig ^. #prod serve' $ (serverEnvironmentConfig & #path .~ (Just serveDir)) - where serverConfig = config ^. #server