Router Helper

From Onix OS Wiki
Jump to: navigation, search

The router socket structure was created with the intention of creating an API over the HTTP/S protocol and exchanging data through the socket. In web based projects it is possible to write web server quickly and easily. In addition, the API can be created in micro-services.

Definition

def config = { 
  "METHOD" : { 
    "PATH" : ....result: string or function...
  },
... other method and paths....
}
router(port,  config);

It creates an HTTP/HTTPS router based on the simple supplied config, and it automatically creates and connects to the socket. It can be considered as HTTP / HTTPS serverlet. Config must be hash. It should be written in a regular format according to the sample.

Example

hello.ol (as hello module)

def hello = fn(name){
  return ("Hello, "+name+"! Welcome to Olang")
}

router.ol (as router module)

load "hello.ol"
def config = { 
  "GET" : { 
    "/" : "Welcome to the O-Lang Router!" 
  }, 
  "POST":{ 
    "/:name" : (hello("{{.name}}"))
  }
}
show("Listening 0.0.0.0:8080... Ctrl+C to exit.")
router("8080",  config);

GET method test

~> curl -X GET localhost:8080
Welcome to the O-Lang Router!

POST method test

Note: oytun as parameter value for name [ :name => {{.name}} ]

~> curl -X POST localhost:8080/oytun
Hello, oytun! Welcome to Olang

As you can see in the example, we wrote a web micro service using port 8080 via get and post methods.