1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-24 05:45:11 +03:00

Provide Express server (#193)

This commit is contained in:
Jens Krause 2017-08-25 21:54:24 +02:00
parent 5cb54f4a55
commit ef5d835c2a
No known key found for this signature in database
GPG Key ID: 3B2FAFBCEFA5906D
11 changed files with 2265 additions and 0 deletions

11
front-ps/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
node_modules/
bower_components/
output/
public/server.js
public/category-overview.js
public/category-detail.js
.psci_modules
npm-debug.log
.psc-ide-port
yarn-error.log
.yarnclean

61
front-ps/README.md Normal file
View File

@ -0,0 +1,61 @@
# [WIP] Guide's front-end
## Requirements
* [`Node`](https://nodejs.org/en/)
* [`yarn`](https://yarnpkg.com/lang/en/) (or [npm](https://www.npmjs.com/))
* [`Pulp`](https://github.com/purescript-contrib/pulp)
## Installation
1. Get all sources:
```sh
git clone git@github.com:aelve/guide.git
cd guide
git checkout front-ps-next
```
2. Get data of `guide-database`
```sh
./official.sh
```
3. Build back-end API
```
./b
```
4. Install front-end dependencies
```sh
yarn
```
5. Build Express server
```sh
yarn build:server
```
6. Build page `category-overview`
```sh
yarn build:category-overview
```
7. Build page `category-detail`
```sh
yarn build:category-detail
```
## Run
1. Run back-end
```sh
stack exec guide
```
2. Run front-end
```sh
yarn start
```
Open [`http://localhost:3333`](http://localhost:3333).

18
front-ps/bower.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "aelve-guide-frontend",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
],
"dependencies": {
"purescript-prelude": "^3.1.0",
"purescript-console": "^3.0.0",
"purescript-express": "^0.5.2",
"purescript-node-process": "^5.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
}
}

35
front-ps/package.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "aelve-guide-frontend",
"version": "0.0.1",
"description": "",
"main": "public/server.js",
"keywords": [],
"scripts": {
"postinstall": "bower cache clean && bower install",
"clean": "rimraf dist && rimraf public/*js && rimraf output",
"build:category-overview": "rimraf output/Main && pulp build --src-path ./pages/category-overview -O > ./public/category-overview.js",
"build:category-detail": "rimraf output/Main && pulp build --src-path ./pages/category-detail -O > ./public/category-detail.js",
"build:server": "rimraf output/Main && mkdir dist && pulp build --src-path ./server -O > ./dist/server.js",
"run:server": "PORT=3333 node dist/server.js",
"start": "npm run run:server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/aelve/guide/.git"
},
"author": "",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/aelve/guide/issues"
},
"devDependencies": {
"bower": "^1.8.0",
"pulp": "12.0.1",
"rimraf": "^2.6.1"
},
"dependencies": {
"ejs": "^2.5.7",
"express": "^4.15.4"
}
}

View File

@ -0,0 +1,9 @@
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log "Hello category-detail!"

View File

@ -0,0 +1,9 @@
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log "Hello category-overview!"

View File

@ -0,0 +1,2 @@
</body>
</html>

View File

@ -0,0 +1,8 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title><%=title%></title>
<!-- TODO (sectore) create and add css file -->
<!-- <link href="/<%=contentId%>.css" rel="stylesheet"> -->
</head>
<body>

View File

@ -0,0 +1,6 @@
<% include header %>
<h1>name: <%=catName%> id: <%=catDetailId%> </h1>
<div id="guide"></div>
<script type="text/javascript" src="/<%=contentId%>.js"></script>
<% include footer %>

91
front-ps/server/Main.purs Normal file
View File

@ -0,0 +1,91 @@
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Exception (Error, message)
import Data.Int (fromString)
import Data.Maybe (fromMaybe)
import Node.Express.App (App, get, listenHttp, setProp, use, useOnError)
import Node.Express.Handler (Handler, HandlerM, next)
import Node.Express.Middleware.Static (static)
import Node.Express.Request (getOriginalUrl, getRouteParam)
import Node.Express.Response (redirect, render, sendJson, setStatus)
import Node.Express.Types (EXPRESS)
import Node.HTTP (Server)
import Node.Process (PROCESS, lookupEnv)
errorHandler :: forall e. Error -> HandlerM (express :: EXPRESS | e) Unit
errorHandler err = do
setStatus 400
sendJson {error: message err}
logger :: forall e. Handler (console :: CONSOLE | e)
logger = do
url <- getOriginalUrl
liftEff $ log ("url: " <> url)
next
indexHandler :: forall e. HandlerM (express :: EXPRESS | e ) Unit
indexHandler =
redirect $ "/category/" <> defaultCategoryName
defaultCategoryName :: String
defaultCategoryName = "haskell"
newtype PageConfig = PageConfig
{ contentId :: String
, title :: String
, catName :: String --
, catDetailId :: String
}
renderPage :: forall eff . PageConfig -> Handler eff
renderPage config =
-- TODO (sectore) Render a single Pux app for each page
render "layout" config
categoryOverviewHandler :: forall e. HandlerM (express :: EXPRESS | e) Unit
categoryOverviewHandler = do
catName <- fromMaybe defaultCategoryName <$> getRouteParam "name"
renderPage $ PageConfig { contentId: "category-overview"
, title: "Aelve - Guide: Category " <> catName
, catName
, catDetailId: "0"
}
categoryDetailHandler :: forall e. HandlerM (express :: EXPRESS | e) Unit
categoryDetailHandler = do
catName <- fromMaybe defaultCategoryName <$> getRouteParam "name"
catDetailId <- fromMaybe "0" <$> getRouteParam "id"
renderPage $ PageConfig { contentId: "category-detail"
, title: "Aelve - Guide: Category Detail" <> catDetailId
, catName
, catDetailId
}
appSetup :: forall e. App (console :: CONSOLE | e)
appSetup = do
liftEff $ log "app setup"
setProp "view engine" "ejs"
setProp "views" "public/views"
use logger
use $ static "public"
get "/" indexHandler
get "/category/:name/" categoryOverviewHandler
get "/category/:name/detail/:id" categoryDetailHandler
useOnError errorHandler
main :: forall e. Eff (express :: EXPRESS, process :: PROCESS, console :: CONSOLE | e) Server
main = do
port <- (fromMaybe defaultPort <<< fromString <<< fromMaybe (show defaultPort)) <$> lookupEnv "PORT"
listenHttp appSetup port (callback port)
where
defaultPort = 8080
callback port' = \_ -> log $ "Listening on " <> show port'

2015
front-ps/yarn.lock Normal file

File diff suppressed because it is too large Load Diff