From 463665e8a91e8b0611c3e8413842182c67d3ea3d Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 27 Nov 2020 18:06:40 +0400 Subject: Changed String to Text everywhere --- Main.hs | 8 +++++--- Web/Request.hs | 4 +++- Web/Response.hs | 13 ++++++++----- Web/Router.hs | 4 +++- Web/Views.hs | 11 ++++++++--- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Main.hs b/Main.hs index 86bc37e..aefc95c 100644 --- a/Main.hs +++ b/Main.hs @@ -1,5 +1,7 @@ module Main where +import qualified Data.Text as T + import Web.Response import Web.Request import Web.Router @@ -7,9 +9,9 @@ import Web.Views import Web.Utils import Web.Http -table = [ Route indexGet "/" GET - , Route helloGet "/hello" GET ] +table = [ Route indexGet (T.pack "/") GET + , Route helloGet (T.pack "/hello") GET ] main = do - response <- resolve table (Request [] "/hello" GET) + response <- resolve table (Request [] (T.pack "/hello") GET) print $ response \ No newline at end of file diff --git a/Web/Request.hs b/Web/Request.hs index 38f91b5..71dc421 100644 --- a/Web/Request.hs +++ b/Web/Request.hs @@ -1,9 +1,11 @@ module Web.Request where +import Data.Text (Text) + import Web.Http -- Request query url method -data Request = Request [QueryPair] String Method +data Request = Request [QueryPair] Text Method deriving Show getQuery (Request query _ _) = query diff --git a/Web/Response.hs b/Web/Response.hs index b08efb9..39f6887 100644 --- a/Web/Response.hs +++ b/Web/Response.hs @@ -1,17 +1,20 @@ module Web.Response where +import qualified Data.Text as T +import Data.Text (Text) + data Response - = HtmlResponse Int String -- Код возврата, содержимое HTML - | TextResponse Int String String -- Код возврата, Content-Type, содержимое HTML + = HtmlResponse Int Text -- Код возврата, содержимое HTML + | TextResponse Int Text Text -- Код возврата, Content-Type, содержимое HTML deriving Show -notFoundResponse = HtmlResponse 404 "404 Not Found" +notFoundResponse = HtmlResponse 404 (T.pack "404 Not Found") getStatusCode (HtmlResponse code _) = code getStatusCode (TextResponse code _ _) = code -getContentType (HtmlResponse _ _) = "text/html" +getContentType (HtmlResponse _ _) = (T.pack "text/html") getContentType (TextResponse _ contentType _) = contentType getContent (HtmlResponse _ content) = content -getContent (TextResponse _ _ content) = content \ No newline at end of file +getContent (TextResponse _ _ content) = content diff --git a/Web/Router.hs b/Web/Router.hs index af5bd96..234f30a 100644 --- a/Web/Router.hs +++ b/Web/Router.hs @@ -1,11 +1,13 @@ module Web.Router where +import Data.Text (Text) + import Web.Response import Web.Request import Web.Http -- Route callback url method -data Route = Route (Request -> IO Response) String Method +data Route = Route (Request -> IO Response) Text Method resolve :: [Route] -> Request -> IO Response resolve [] _ = return notFoundResponse diff --git a/Web/Views.hs b/Web/Views.hs index b6ae013..6619d09 100644 --- a/Web/Views.hs +++ b/Web/Views.hs @@ -1,20 +1,25 @@ module Web.Views where import System.IO +import qualified Data.Text as T +import Data.Text (Text) import Web.Request import Web.Response indexGet (Request query url method) = - return $ HtmlResponse 200 "index" + return $ HtmlResponse 200 (T.pack "index") helloGet req = - return $ HtmlResponse 200 "hello" + return $ HtmlResponse 200 (T.pack "hello") +renderTemplate :: String -> IO Response renderTemplate name = do template <- readTemplate name return $ HtmlResponse 200 template +readTemplate :: String -> IO Text readTemplate name = do handle <- openFile ("templates/" ++ name) ReadMode - hGetContents handle \ No newline at end of file + contents <- hGetContents handle + return $ T.pack contents -- cgit v1.2.3