refactor template map to use Text over String
This commit is contained in:
		
							parent
							
								
									ba38e5e66e
								
							
						
					
					
						commit
						7a7a3b65f8
					
				
					 3 changed files with 22 additions and 17 deletions
				
			
		| 
						 | 
					@ -30,9 +30,12 @@ parseMetricsResponse MetricsResponse { results = r } = Map.fromList <$> mapM par
 | 
				
			||||||
        parseResult MRResult { metrics = [count], dimensions = [path] } = Right (removeTrailingSlash path, count)
 | 
					        parseResult MRResult { metrics = [count], dimensions = [path] } = Right (removeTrailingSlash path, count)
 | 
				
			||||||
        parseResult result = Left $ "Could not parse result: " ++ show result
 | 
					        parseResult result = Left $ "Could not parse result: " ++ show result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        removeTrailingSlash "" = ""
 | 
					        removeTrailingSlash "/" = "/"
 | 
				
			||||||
        removeTrailingSlash "/" = ""
 | 
					        removeTrailingSlash xs = removeTrailingSlash' xs
 | 
				
			||||||
        removeTrailingSlash (x:xs) = x : removeTrailingSlash xs
 | 
					
 | 
				
			||||||
 | 
					        removeTrailingSlash' "" = ""
 | 
				
			||||||
 | 
					        removeTrailingSlash' "/" = ""
 | 
				
			||||||
 | 
					        removeTrailingSlash' (x:xs) = x : removeTrailingSlash' xs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
queryPageviews :: C.Config -> IO (Either String (Map String Int))
 | 
					queryPageviews :: C.Config -> IO (Either String (Map String Int))
 | 
				
			||||||
queryPageviews config = do
 | 
					queryPageviews config = do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,27 +5,26 @@ module Server (
 | 
				
			||||||
) where
 | 
					) where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import Control.Applicative ((<|>))
 | 
					import Control.Applicative ((<|>))
 | 
				
			||||||
 | 
					import Control.Concurrent (MVar, readMVar, tryPutMVar)
 | 
				
			||||||
 | 
					import Data.Map (Map)
 | 
				
			||||||
 | 
					import qualified Data.Text as T
 | 
				
			||||||
 | 
					import Data.Text (Text)
 | 
				
			||||||
 | 
					import qualified Data.ByteString as BS
 | 
				
			||||||
 | 
					import Data.ByteString (ByteString)
 | 
				
			||||||
import Network.Wai (Application, Response, responseLBS, Request (pathInfo))
 | 
					import Network.Wai (Application, Response, responseLBS, Request (pathInfo))
 | 
				
			||||||
import Network.Wai.Handler.Warp (run)
 | 
					import Network.Wai.Handler.Warp (run)
 | 
				
			||||||
import qualified Network.HTTP.Types as H
 | 
					import qualified Network.HTTP.Types as H
 | 
				
			||||||
import qualified Data.Map as Map
 | 
					import qualified Data.Map as Map
 | 
				
			||||||
import Data.Map (Map)
 | 
					 | 
				
			||||||
import qualified Data.Text as T
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import TemplatePage (PageContext, PageTemplate, buildPage)
 | 
					import TemplatePage (PageContext, PageTemplate, buildPage)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import qualified Data.ByteString as BS
 | 
					app :: Map Text PageTemplate -> MVar PageContext -> MVar () -> Application
 | 
				
			||||||
import Data.ByteString (ByteString)
 | 
					 | 
				
			||||||
import Control.Concurrent (MVar, readMVar, tryPutMVar)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
app :: Map String PageTemplate -> MVar PageContext -> MVar () -> Application
 | 
					 | 
				
			||||||
app templates context updateNotify request respond = do
 | 
					app templates context updateNotify request respond = do
 | 
				
			||||||
   let path = T.unpack $ T.intercalate "/" $ filter (not . T.null) $ pathInfo request
 | 
					   let path = T.concat ["/", T.intercalate "/" $ filter (not . T.null) (pathInfo request)]
 | 
				
			||||||
       slashPath = if path /= "" then '/' : path else path
 | 
					       indexPath = T.concat [path, "/index.html"]
 | 
				
			||||||
       indexPath = slashPath ++ "/index.html"
 | 
					 | 
				
			||||||
       template = Map.lookup path templates <|> Map.lookup indexPath templates
 | 
					       template = Map.lookup path templates <|> Map.lookup indexPath templates
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   putStrLn $ "Page requested: " ++ slashPath
 | 
					   putStrLn $ "Page requested: " ++ T.unpack path
 | 
				
			||||||
   context' <- readMVar context
 | 
					   context' <- readMVar context
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   -- Unblock pageview cache if waiting for a request.
 | 
					   -- Unblock pageview cache if waiting for a request.
 | 
				
			||||||
| 
						 | 
					@ -40,5 +39,5 @@ respMaybe (Just text) = responseLBS H.status200 [("content-type", "text/html")]
 | 
				
			||||||
notFound :: Response
 | 
					notFound :: Response
 | 
				
			||||||
notFound = responseLBS H.status404 [(H.hContentType, "text/plain")] "404 - File not found"
 | 
					notFound = responseLBS H.status404 [(H.hContentType, "text/plain")] "404 - File not found"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
servePages :: Int -> Map String PageTemplate -> MVar PageContext -> MVar () -> IO ()
 | 
					servePages :: Int -> Map Text PageTemplate -> MVar PageContext -> MVar () -> IO ()
 | 
				
			||||||
servePages port templates context updateNotify = run port $ app templates context updateNotify
 | 
					servePages port templates context updateNotify = run port $ app templates context updateNotify
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,13 +4,16 @@ import Data.Bool (bool)
 | 
				
			||||||
import System.FilePath ((</>))
 | 
					import System.FilePath ((</>))
 | 
				
			||||||
import System.Directory (listDirectory, doesDirectoryExist)
 | 
					import System.Directory (listDirectory, doesDirectoryExist)
 | 
				
			||||||
import qualified Data.ByteString as BS
 | 
					import qualified Data.ByteString as BS
 | 
				
			||||||
 | 
					import qualified Data.Text as T
 | 
				
			||||||
 | 
					import Data.Text (Text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import TemplatePage (PageTemplate, parsePage, isUntemplated)
 | 
					import TemplatePage (PageTemplate, parsePage, isUntemplated)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
walkFiles :: FilePath -> IO [FilePath]
 | 
					walkFiles :: FilePath -> IO [FilePath]
 | 
				
			||||||
walkFiles path = foldr (mappend . forEntry . (path </>)) mempty =<< listDirectory path
 | 
					walkFiles path = foldr (mappend . forEntry . (path </>)) mempty =<< listDirectory path
 | 
				
			||||||
  where forEntry path' = bool (pure [path']) (walkFiles path') =<< doesDirectoryExist path'
 | 
					  where forEntry path' = bool (pure [path']) (walkFiles path') =<< doesDirectoryExist path'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
loadTemplates :: String -> FilePath -> IO [(String, PageTemplate)]
 | 
					loadTemplates :: String -> FilePath -> IO [(Text, PageTemplate)]
 | 
				
			||||||
loadTemplates baseUrl basePath = filter (not . isUntemplated . snd) <$> (mapM loadTemplate =<< walkFiles basePath)
 | 
					loadTemplates baseUrl basePath = filter (not . isUntemplated . snd) <$> (mapM loadTemplate =<< walkFiles basePath)
 | 
				
			||||||
  where pathToUrl path = baseUrl ++ drop (length basePath) path
 | 
					  where pathToUrl path = T.pack $ baseUrl ++ drop (length basePath) path
 | 
				
			||||||
        loadTemplate path = (\d -> (pathToUrl path, parsePage d)) <$> BS.readFile path
 | 
					        loadTemplate path = (\d -> (pathToUrl path, parsePage d)) <$> BS.readFile path
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue