diff options
Diffstat (limited to 'Web')
| -rw-r--r-- | Web/Request.hs | 4 | ||||
| -rw-r--r-- | Web/Response.hs | 13 | ||||
| -rw-r--r-- | Web/Router.hs | 4 | ||||
| -rw-r--r-- | Web/Views.hs | 11 |
4 files changed, 22 insertions, 10 deletions
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 "<strong>404 Not Found</strong>" +notFoundResponse = HtmlResponse 404 (T.pack "<strong>404 Not Found</strong>") 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 "<strong>index</strong>" + return $ HtmlResponse 200 (T.pack "<strong>index</strong>") helloGet req = - return $ HtmlResponse 200 "<i>hello</i>" + return $ HtmlResponse 200 (T.pack "<i>hello</i>") +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 |