Skip to content

publishing project to use #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ make bindings like `bs-express` or `bs-fastify`. The unique binding this project
- [x] create endpoint response html
- [x] create endpoint response json
- [x] create structure of projects
- [ ] create list routers using
- [x] create list routers using
- [ ] not need create router to `/api` and `/api/`
- [ ] create crud Rest Api with `/users` using [GET, POST, PUT, DELETE] methods HTTP
- [ ] improve use of json to response data
Expand Down
32 changes: 16 additions & 16 deletions bsconfig.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "re-http",
"version": "0.0.1",
"sources": {
"dir": "lib",
"subdirs": true
},
"package-specs": {
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": ["bs-node-http", "@glennsl/bs-json"],
"warnings": {
"error": "+101"
},
"refmt": 3
"name": "re-http",
"version": "0.0.1",
"sources": {
"dir": "lib",
"subdirs": true
},
"package-specs": {
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": ["bs-node-http", "@glennsl/bs-json"],
"warnings": {
"error": "+101"
},
"refmt": 3
}
53 changes: 0 additions & 53 deletions lib/App.bs.js

This file was deleted.

29 changes: 0 additions & 29 deletions lib/App.re

This file was deleted.

4 changes: 2 additions & 2 deletions lib/helpers/Method.bs.js → lib/Method.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions lib/Method.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
type method =
GET
| POST
| PUT
| UPDATE
| DELETE
| HEAD
| OPTION
| CONNECT
| TRACE
| PATCH

let methodString = (method) =>
switch(method) {
| GET => "GET"
| POST => "POST"
| PUT => "PUT"
| UPDATE => "UPDATE"
| DELETE => "DELETE"
| HEAD => "HEAD"
| OPTION => "OPTION"
| CONNECT => "CONNECT"
| TRACE => "TRACE"
| PATCH => "PATCH"
}

let getMethod = (method) => {
switch(method) {
| `GET => GET
| `POST => POST
| `PUT => PUT
| `UPDATE => UPDATE
| `DELETE => DELETE
| `HEAD => HEAD
| `OPTION => OPTION
| `CONNECT => CONNECT
| `TRACE => TRACE
| `PATCH => PATCH
}
}
18 changes: 18 additions & 0 deletions lib/ReHttpNode.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/ReHttpNode.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
open Server;
open Rest;

let start = (~routers, ~port) => {
let app = Http.createServer((~request, ~response) => {
Routers.start(~request, ~response, ~routers)
});

renderRouters(routers);

print_endline("server running in localhost:"++ string_of_int(port));
}
2 changes: 1 addition & 1 deletion lib/ResponseNotFound.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
20 changes: 20 additions & 0 deletions lib/Rest.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open Http;

let bodyResponse = (response, statusCode, message) => {
ServerResponse.(
response
|> setStatusCode(statusCode)
|> write(message)
|> end_
);
};

let bodyJsonResponse = (response, statusCode, message) => {
ServerResponse.(
response
|> setHeader("content-type", "application/json")
|> setStatusCode(statusCode)
|> write(message)
|> end_
);
};
22 changes: 22 additions & 0 deletions lib/Routers.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/Routers.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let start = (~request, ~response, ~routers) => {
let methodOfRequest = request
-> Http.ClientRequest.getMethod
-> Method.getMethod

let uriRequest = Http.ClientRequest.getUrl(request);

let methodSelected = Server.methodSelected(methodOfRequest, uriRequest, routers);

switch (methodSelected) {
| None => ResponseNotFound.run(request, response)
| Some(router) => router.action(request, response)
}
};
41 changes: 34 additions & 7 deletions lib/Server.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 36 additions & 4 deletions lib/Server.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
let server =
Http.createServer((~request, ~response) => App.start(~request, ~response));
open Http;
open Method;

Http.Server.(server |>listen(~port=3000));
print_endline("server running in localhost:3000");
type port = int;

type uri = string;

type request = ClientRequest.t

type response = ServerResponse.t

type action = (request, response) => unit;

type router = {
method: method,
uri: uri,
action: action
};

let starting = (app, port) => Http.Server.(app |> listen(~port=port));

let methodSelected = (methodOfRequest, uriRequest, routers) => List.find_opt(router => {
router.method == methodOfRequest &&
router.uri == uriRequest
}, routers)


let transformTring = (method) => Js.String.make(method)

let renderRouterItem = (method, uri) => {
print_endline(Method.methodString(method) ++ ": " ++ uri)
}


let renderRouters = (routers) => {
List.map(router => renderRouterItem(router.method, router.uri), routers);
}
Loading