controllers for template and json

23 views
Skip to first unread message

2316134491

unread,
Oct 25, 2017, 7:31:52 AM10/25/17
to chicagoboss
Hi,

We are building a server to serve ordinary web pages by ok->ErlyDTL, also as backend api server to provide json data.

The data to serve is the same, if we write 2 controllers, one with {ok, Values} for web page and one with {json, Values}, most code of the controllers will be duplicated.

Is there better way to do it?

Thanks.

Konstantin

unread,
Oct 25, 2017, 4:25:11 PM10/25/17
to chica...@googlegroups.com

I think you used different URLs to serve html and json. But the common solution is to use the header "Accept" in an HTTP request and build an output based on it value, not an URL. With this approach you can use one controller to produce a data with different mime-types.

More: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept

--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at https://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/tencent_EFD0EEB826B3474F38F36FD27784361A7B08%40qq.com.
For more options, visit https://groups.google.com/d/optout.

2316134491

unread,
Oct 25, 2017, 8:17:44 PM10/25/17
to chicagoboss
Indeed, we planed to return ok or json based on subdomain like www.domain.com or api.domain.com with one controller. Appreciate your remind.


------------------ 原始邮件 ------------------
发件人: "Konstantin";<k.n.go...@gmail.com>;
发送时间: 2017年10月26日(星期四) 凌晨4:26
收件人: "chicagoboss"<chica...@googlegroups.com>;
主题: Re: [chicagoboss] controllers for template and json

Phang Mulianto

unread,
Nov 11, 2017, 8:45:19 AM11/11/17
to ChicagoBoss

Hi,

I will consider the processing inside the html or api function.
Usually API version will have less logic, only return the key val of json format, while with html output you will consider the context to be passed to your template.

Less work if use 1 fun for returning 2 type of output, html or json, but if your api access is bigger latter, you will have also process the html outputing part.

If you duplicate it, for API and one for the html output, more works, but you have a separation of task. less confusion on the developer and less logic to process for each specific output.



On Thursday, 26 October 2017 08:17:44 UTC+8, 2316134491 wrote:
Indeed, we planed to return ok or json based on subdomain like www.domain.com or api.domain.com with one controller. Appreciate your remind.

2316134491

unread,
Nov 17, 2017, 8:23:00 PM11/17/17
to chicagoboss
Thank you.

Will start from simple, if it grows bigger, copy a new set of controllers and modify is easy.


------------------ 原始邮件 ------------------
发件人: "Phang Mulianto";<brav...@gmail.com>;
发送时间: 2017年11月11日(星期六) 晚上9:45
收件人: "ChicagoBoss"<chica...@googlegroups.com>;
主题: Re: 回复: [chicagoboss] controllers for template and json
Reply all
Reply to author
Forward
0 new messages