-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3167fb0
commit 12a7b83
Showing
6 changed files
with
135 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Pages.Pages where | ||
import Network.Wai (Response, Request (pathInfo), responseBuilder, responseFile) | ||
import IHP.HSX.QQ (hsx) | ||
import Text.Blaze.Html (Html) | ||
import Text.Regex (Regex, mkRegex, matchRegex) | ||
import Network.HTTP.Types (status404, status200) | ||
import Blaze.ByteString.Builder (copyByteString) | ||
import Data.ByteString.UTF8 (fromString) | ||
import Text.Blaze.Html.Renderer.String (renderHtml) | ||
import Layout (layout) | ||
import Helpers.Logger (info, warning) | ||
import System.Directory (doesFileExist) | ||
import Data.List (intercalate, find) | ||
import Api.Api (api) | ||
import Pages.Contact.Contact (contact) | ||
import Pages.Sources.Sources (sources) | ||
import Pages.Guestbook.Guestbook (guestbook) | ||
import Pages.Projects.Projects (projects) | ||
import Pages.Projects.Snake (leaderboard) | ||
import Pages.Admin.Admin (admin) | ||
import Index (index) | ||
import Data.Text (unpack) | ||
import Pages.Search.Search (search) | ||
|
||
page404 :: [String] -> Response | ||
page404 args = responseBuilder status404 [("Content-Type", "text/html")] $ copyByteString (fromString (renderHtml (layout [hsx| | ||
<h1>404 - Page not found</h1><br> | ||
params: {args} | ||
|]))) | ||
|
||
serve :: Html -> Response | ||
serve content = responseBuilder status200 [("Content-Type", "text/html")] $ copyByteString (fromString (renderHtml content)) | ||
|
||
serveFile :: String -> IO Response | ||
serveFile path = do | ||
info "Serving file" | ||
exists <- doesFileExist path | ||
if exists then do | ||
info "File exists" | ||
return $ responseFile status200 [] path Nothing | ||
else do | ||
warning "No file found!" | ||
return $ responseBuilder status404 [("Content-Type", "text/json")] $ copyByteString "{\"error\":\"Error: file not found!\"}" | ||
|
||
pages :: [(String, Request -> IO Response)] | ||
pages = [ | ||
("/static", \req -> do | ||
let (_:xs) = getArgs req | ||
serveFile $ intercalate "/" ("static":xs) | ||
), | ||
("/api", \req -> do | ||
let (_:args) = getArgs req | ||
(status, value) <- api args req | ||
return $ responseBuilder status [("Content-Type", "text/plain")] $ copyByteString (fromString value) | ||
), | ||
("/search", \req -> do | ||
let [_, query] = getArgs req | ||
return $ serve $ layout $ search pages $ mkRegex query | ||
), | ||
("/contact", \_ -> return $ serve $ layout contact), | ||
("/sources", \_ -> return $ serve $ layout sources), | ||
("/guestbook", \_ -> serve . layout <$> guestbook), | ||
("/projects", \req -> do | ||
let (_:project) = getArgs req | ||
return $ serve $ layout $ projects project | ||
), | ||
("/snake-leaderboard", \_ -> serve . layout <$> leaderboard), | ||
("/favicon.ico", \_ -> serveFile "static/favicon.ico"), | ||
("/admin", \req -> do | ||
let (_:xs) = getArgs req | ||
serve . layout <$> admin xs | ||
), | ||
("/", \_ -> serve . layout <$> index) | ||
] | ||
where | ||
getArgs request = map unpack $ pathInfo request | ||
|
||
findPage :: String -> (Request -> IO Response) | ||
findPage addr = case find (\(regex, _) -> case matchRegex (mkRegex regex) addr of | ||
Nothing -> False | ||
_ -> True) pages of -- TODO: make regex | ||
(Just x) -> snd x | ||
Nothing -> \req -> do | ||
let x = map unpack $ pathInfo req | ||
return $ page404 x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Pages.Search.Search where | ||
import Text.Blaze.Html (Html) | ||
import IHP.HSX.QQ (hsx) | ||
import Text.Regex (Regex, matchRegex) | ||
import Network.Wai (Request, Response) | ||
import Helpers.Section (section) | ||
|
||
findPage :: [(String, Request -> IO Response)] -> Regex -> [String] | ||
findPage ((target, _):xs) regex = case matchRegex regex target of | ||
Nothing -> findPage xs regex | ||
_ -> target : findPage xs regex | ||
findPage [] _ = [] | ||
|
||
search :: [(String, Request -> IO Response)] -> Regex -> Html | ||
search pages query = [hsx| | ||
<h1>Search Results:</h1> | ||
{section $ makeResults 0 $ findPage pages query} | ||
|] | ||
where | ||
makeResults :: Int -> [String] -> Html | ||
makeResults index (x:xs) = [hsx| | ||
<h2>{index} - {title}</h2> | ||
<a href={x}>{x}</a> | ||
<br> | ||
{makeResults (index+1) xs} | ||
|] | ||
where | ||
(_:title) = x | ||
makeResults _ [] = [hsx||] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters