Armeria: How about the way expose armeria service to browser

Created on 9 Apr 2017  路  4Comments  路  Source: line/armeria

In my case, I want to call armeria service from browser side(my HTML5 app or tranditional web), which means by ajax, the current way I do is like we did in DocService.
if we mapping a path for DocService like this, we could send ajax call to our armeria service,

builder.serviceUnder("/docs", new DocService());

I'm sure whether this is a good or bad pratice, I want to hear your voice.

question

All 4 comments

The DocService does not provide a universal invocation mechanism. The reason it is able to invoke a THttpService is that it talks to the THttpService directly using the TTEXT serialization format.

That being said, DocService is provided as a way to debug your services and it is not meant to serve any end-user requests. If you are interested in serving a plain HTTP request (e.g. RESTful API), I'd recommend extending AbstractHttpService or using DynamicHttpService.

Alternatively, if your THttpService is relatively low-traffic, you could invoke it using the TTEXT serialization format just like DocService does. You don't need a DocService to use it though. It's a THttpService that supports TTEXT. You may want to open your web browser's network monitoring console and see what happens when you make a call from the DocService debug form.

if I expose thrift based service by TTEXT serialization, is any performance issue?
since if we want to call a service method from browser, javascript could only operate on JSON format.

When using thrift from the browser, you should use thrift generated JavaScript code. This uses the TJSON format which is reasonably efficient, but not human readable. Here is a small tutorial

https://thrift.apache.org/tutorial/js

Will this work for you? If you use thrift I really recommend using thrift compiler code, otherwise much of the benefit of using an IDL API goes away and normal rest would be more appropriate.

As trustin says you can use TTEXT if you really need to, but it's really slow and I'm also not confident about its security which is why we recommend not exposing it to external users - it was implemented purely for debugging.

thank you @anuraaga , I will dig into the thrift javascript compiler.

Was this page helpful?
0 / 5 - 0 ratings