Skip to content

mesopelagique/Tricho

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tricho 🕷

language language-top code-size release license

Helpers functions for web development and a web router:

$router:=tricho.router() 
$router.get("/hello";"Hello world")
$router.post("/create";Formula($2.status(201).download("path/of/file"))
$router.get("/employee";Formula($2.render($templateFile;$employeeObject)))
...

Utility methods

Mainly wrapper to use C_OBJECT

HTTP Headers

  • WebGetHTTPHeaders
  • WebSetHTTPHeaders

Variables

  • WebGetVariables: return an object of variables

Respond

  • WebSendObject: send object or collection as JSON text (using JSON Stringify)
  • WebSendFile: send a File

Router

A router allow you define entry points to respond to HTTP requests.

Typically you provide the URL path, the HTTP method and the code to execute when matching.

Create the rooter and add "route(s)"

$router:=tricho.router()
$router.get("/hello";"Hello world")
...

For test purpose you could create it at each client request but for efficiency in production mode cache it into a variable

Handle the request

In On Web Connection you could handle all request using code:

$router.handle($1;$2;$3;$4;$5;$6)

Register routes

Choose the HTTP method to respond

You can choose one http method(GET, POST, PUT, ...) or all methods

$router.get("/hello";"This is a GET")
$router.post("/hello";"This is a POST")
$router.all("/hello";Formula("This is a "+$1.method))

Providing data or code to execute

Last parameters is the data to return to the HTTP client.

If you use a formula,

  • the code could be dynamic ie. executed each times
  • you can call an other methods to manage response
  • you receive
    • as $1 a context/request object with some useful features (to get headers, variables, ...)
    • as $2 a reponse builder to be able to change status code, provoque a file download, add headers/cookies, etc..

If you return

  • an object or a collection, it will be JSON stringifyed
  • a File, it will be send as blob (with mime type according to file extension)

Have parameters in route

You can define parameters in route using :, for instance to get the employee id

$router.get("/employee/:id";Formula(ProceedEmployeeData($1.params.id)))

then in HTTP client, you could access the resource using path /employee/12

Using a class (advanced use)

A class must conform to some parameters and functions, then you can register as follow

$router.register(cs.YourRoute.new()) 

The class must defined the path and methods attributes.

Class constructor
  This.methods:=New collection(HTTPMethod .GET)
  This.path:="/a/class/path"

and must define a function to return the data.

Function respond
  C_VARIANT($0)
  C_OBJECT($1) // $context
  $0:="Hello" // Return a String, an Object(JSON), 4D.File...

alteratively you can defined function by HTTP method if you do not defined methods attribute

Function get
  $0:="Hello"

Function post
  $0:=New object("success";True)

Handler

Handler is an alternative to Router; it allow to register other methods to split and factorize your code used in On Web Connection

According to the request context (path, HTTP method, parameters) the handler must handle or not the request. If one handler respond, we stop.

First create the handler

$handler:=tricho. handler()

each time in On Web Connection or only one time in On Startup for instance

Then register some handlers

With formula which return True if request handled

$handler.register(Formula(MyMethodToRespond(This)))

Using class

You can also use a class wich contains handle function which True if request handled.

Function handle
  If ($1.path="/dayNumber")
    WEB SEND TEXT(String(Day number(Current date)))
    $0:=True // handled
  Else
    $0:=False // ignore request
  End if

Finally handle request

In On Web Connection

If ($handler.handle($1;$2;$3;$4;$5;$6))
  // handled
Else
  // other code like http 404 if not handled
End if

Ackowledgments

Router is inspired by numerous packages of different languages such as Flask for python, express for javascript, etc...

Info

mesopelagique