| Author |
Message |
< Advanced Erlang/OTP ~ Yaws+POST Help! Cose to extinction... |
| seancharles |
Posted: Sun Aug 12, 2007 7:22 am |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
Brain cells. Must be the only explanation.
Apologies in advance for filling up trapexit.org's file server with my posting but I am really stumped and I have to get on with things! Sorry
Here's the setup. I am writing my killer application and finding that yaws files seem to be restricted to a single parameter (A), I decided to use erlang source files for every page and make my own page api:
render( Page, A, Bindings ) -> ...
It is working fine. I am using EHTML for generating forms, now comes the (my!) current problem...
I cannot find a 'clean' (read one that works) way to receive a POST outside of a yaws file. I looked at the yaws source to see if I could leave off the extension etc but no dice... the simple answer would seem to be a single yaws page that accepts every post from every form then routes it to the appropriate handler using the form 'name' tag. However, that still only delays my problem. Being able to put the action as Code: action="module:function" would be a really excellent thing to be able to do. Somebody tell me it already does this and I will be very chuffed. I am already thinking of having a go myself having built yaws from the source, it would seem 'trivial' (ha!) to attempt such a mod.
Currently, I have a .yaws file that receives my form data, validates it and then tries to hand back the response and that is the source of the problem.
My page:render() function hands back an {html, ...} tuple which is then bundled into a list of stuff, perhaps some code snippets would help, this is the controller entry point (mine) and what you see here is the data handed back to yaws
Code:
out( A ) ->
PageMenu = get_page_by_id( hd( string:tokens( A#arg.appmoddata, "/" ))),
_H = A#arg.headers,
show_page( PageMenu, A, [] ).
show_page( PageMenu, A, Extra ) when is_record( PageMenu, menuitem ) ->
Content = get_page_content( PageMenu, A, Extra ),
[
header_render( A, [{title,"Hello World"}]),
sitenav_render(),
banner_render( PageMenu, A ),
topnav_render( PageMenu ),
page_render( "", Content, ""),
footer_render( PageMenu, A )
].
%% ---->
get_page_content( PageMenu, A, Bindings ) ->
PageBase = "../pages/"++ atom_to_list( PageMenu#menuitem.page ) ++ ".",
case lists:filter(
fun( E ) -> filelib:is_file( nu:f("~s~s",[PageBase,E])) end,
['erl','yaws','html']) of
%% No file, show a small error message!
[] ->
nu:f("NO MATCHING FILE!~n",[]),
yaws_api:f("<span style='#c00'>No page! => ~s</span>", [PageBase]);
%% ..else decide how to render it based on type
[Extension] ->
nu:f("PAGE EXT! ~s", [Extension]),
case Extension of
'erl' -> (PageMenu#menuitem.page):render( PageMenu, A, Bindings );
'yaws' ->
YawsPage = lists:flatten( nu:f("~s~s", [PageBase, Extension])),
io:fwrite("YAWS PAGE: ~s~n", [YawsPage]),
{yssi, YawsPage};
'html' -> {ssi, nu:f("HTML PAGE: TBD",[]), "::", Bindings}
end
end.
The page contents are generated by this function, and like I say, until I POST to a page and try to call the *same* functions that *work* on a GET request, everything is just fine, here is the complete contact.yaws page handler:
Code:
<erl>
out(A)->
Page = controller:get_page_by_name( contact ),
Data = yaws_api:parse_post(A),
Errors = lists:flatten(
[
case nu:form_ensure( A, subject, undefined ) of
undefined ->
{subject, "Please enter the subject text."};
_Else -> []
end,
case nu:form_ensure( A, email, undefined ) of
undefined ->
{email, "Please enter a valid email address."};
_Else -> []
end,
case nu:form_ensure( A, message, undefined ) of
undefined ->
{message, "Please enter some message text."};
_Else -> []
end
]),
if
length(Errors) == 0 ->
io:fwrite("DECENT DATA!, MAIL IT & SHOW STATUS PAGE~n",[]),
{ehtml,
{p, [], "TO BE DONE"}}
;
true ->
%%
%% Re-display the contact page but pass it the
%% error message information as well as ensuring
%% it re-populates any data that was entered.
%%
contact:render( Page, A,
[
{errors, Errors},
{fields,
[
{subject, nu:form_ensure(A, subject, undefined)},
{email, nu:form_ensure(A, email, undefined)},
{message, nu:form_ensure(A, message, undefined)}
]
}
]
)
end.
</erl>
And the error I get when I call my contact page function results in:
Code:
=ERROR REPORT==== 12-Aug-2007::08:12:46 ===
Yaws process died: {function_clause,
[{yaws_server,binary_size,[0,{h1,[],"CONTACT"}]},
{yaws_server,binary_size,2},
{yaws_server,deliver_accumulated,5},
{yaws_server,finish_up_dyn_file,2},
{yaws_server,aloop,3},
{yaws_server,acceptor0,2}, {proc_lib,init_p,5}]}
So, in a nutshell, why does it work for GET but fail on a POST ? It miay not be the case; if in the get_content() function I try to include a yaws file, it too fails with the same message. So far that is my only clue / hope to follow!
HELP!
Thanks again,
Sean Charles... in the meantime I am still pursuing the problem with vigour. |
|
|
| Back to top |
|
| seancharles |
Posted: Sun Aug 12, 2007 6:47 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
Well, after implementing an appmod that allows me to call a module:function as a form action:
Code: <form action="/f/module:function"...
and re-organising and re-factoring my problem went away! I didn't think it should have been a problem to begin with but there you go, I have less than a months experience with Erlang and yaws so I guess the fog of confusion is bound to set in now and again.
I hope it's been entertaining reading because it sure as hell ain't been much fun being where I am !
 |
|
|
| Back to top |
|
| number |
Posted: Mon Jun 04, 2012 8:57 am |
|
|
|
User
Joined: 04 Jun 2012
Posts: 18
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|