Skip to content

Commit 822f89d

Browse files
committed
Initial commit
0 parents  commit 822f89d

File tree

10 files changed

+4211
-0
lines changed

10 files changed

+4211
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc*
7+
/.purs*
8+
/.psa*

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_js:
2+
- "node"
3+
language: node_js
4+
install:
5+
- npm i
6+
script:
7+
- npm test

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Shinya Takahashi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# purescript-bucketchain-history-api-fallback
2+
3+
[![Latest release](http://img.shields.io/github/release/Bucketchain/purescript-bucketchain-history-api-fallback.svg)](https://github.com/Bucketchain/purescript-bucketchain-history-api-fallback/releases)
4+
5+
A History API fallback middleware of [Bucketchain](https://github.com/Bucketchain/purescript-bucketchain).
6+
7+
## Installation
8+
9+
```
10+
bower install purescript-bucketchain-history-api-fallback
11+
```
12+
13+
## Documentation
14+
15+
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-bucketchain-history-api-fallback).
16+
17+
## LICENSE
18+
19+
MIT

bower.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "purescript-bucketchain-history-api-fallback",
3+
"license": "MIT",
4+
"homepage": "https://github.com/Bucketchain/purescript-bucketchain-history-api-fallback",
5+
"authors": [
6+
"Shinya Takahashi <[email protected]>"
7+
],
8+
"description": "A History API fallback middleware of Bucketchain.",
9+
"keywords": [
10+
"purescript",
11+
"webserver",
12+
"history-api"
13+
],
14+
"repository": {
15+
"type": "git",
16+
"url": "git://github.com/Bucketchain/purescript-bucketchain-history-api-fallback.git"
17+
},
18+
"moduleType": [
19+
"node"
20+
],
21+
"ignore": [
22+
"**/.*",
23+
"node_modules",
24+
"bower_components",
25+
"output"
26+
],
27+
"dependencies": {
28+
"purescript-bucketchain": "^0.2.5"
29+
},
30+
"devDependencies": {
31+
"purescript-assert": "^4.0.0"
32+
}
33+
}

example/Main.purs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module Main where
2+
3+
import Prelude
4+
5+
import Bucketchain (createServer, listen)
6+
import Bucketchain.HistoryAPIFallback (withHistoryAPIFallback)
7+
import Bucketchain.Http (requestMethod, requestURL, setHeader, setStatusCode)
8+
import Bucketchain.Middleware (Middleware)
9+
import Bucketchain.ResponseBody (body)
10+
import Control.Monad.Reader (ask)
11+
import Data.Maybe (Maybe(..))
12+
import Effect (Effect)
13+
import Effect.Class (liftEffect)
14+
import Node.HTTP (ListenOptions, Server)
15+
16+
main :: Effect Unit
17+
main = server >>= listen opts
18+
19+
server :: Effect Server
20+
server = createServer $ withHistoryAPIFallback
21+
<<< middleware1
22+
<<< middleware2
23+
<<< middleware3
24+
<<< middleware4
25+
26+
opts :: ListenOptions
27+
opts =
28+
{ hostname: "127.0.0.1"
29+
, port: 3000
30+
, backlog: Nothing
31+
}
32+
33+
middleware1 :: Middleware
34+
middleware1 next = do
35+
http <- ask
36+
if requestMethod http == "GET" && requestURL http == "/index.html"
37+
then liftEffect do
38+
setHeader http "Content-Type" "text/plain; charset=utf-8"
39+
setStatusCode http 200
40+
Just <$> body "Hello World :)"
41+
else next
42+
43+
middleware2 :: Middleware
44+
middleware2 next = do
45+
http <- ask
46+
if requestMethod http == "GET" && requestURL http == "/items"
47+
then liftEffect do
48+
setHeader http "Content-Type" "application/json; charset=utf-8"
49+
setStatusCode http 200
50+
Just <$> body "{\"code\": 12001}"
51+
else next
52+
53+
middleware3 :: Middleware
54+
middleware3 next = do
55+
http <- ask
56+
if requestMethod http == "GET" && requestURL http == "/ping.txt"
57+
then liftEffect do
58+
setHeader http "Content-Type" "text/plain; charset=utf-8"
59+
setStatusCode http 200
60+
Just <$> body "Pong!"
61+
else next
62+
63+
middleware4 :: Middleware
64+
middleware4 next = do
65+
http <- ask
66+
if requestMethod http == "POST" && requestURL http == "/items"
67+
then liftEffect do
68+
setHeader http "Content-Type" "text/plain; charset=utf-8"
69+
setStatusCode http 201
70+
Just <$> body "Created Item"
71+
else next

0 commit comments

Comments
 (0)