Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples #38

Merged
merged 4 commits into from
May 31, 2022
Merged

Examples #38

merged 4 commits into from
May 31, 2022

Conversation

rustkas
Copy link
Contributor

@rustkas rustkas commented May 29, 2022

  • Add rebar3 implementation of Cowboy Web Server examples plus Cowmachine API simular examples that solve the same tasks.
    There are rebar3 (Escript, OTP) applications which demonstrate how to use Cowmachine API.

    1. controller_fuctions demonstrates controller with default Controller API functions.
    2. hello_world is simple demo.
    3. hello_world_chunked demonstrates chunked data transfer with two one-second delays.
    4. hello_world_rest demonstrates returning the data type that matches the request type (html, text, json).
    5. hello_world_ssl demonstrates simplest SSL application.
    6. echo_get GET parameter echo.
    7. echo_post POST parameter echo.
    8. websocket WebSocket example.
    9. websocket2 WebSocket example (another way).
    10. compress_response checks response compression support.
    11. cookie how to work with cookie example.
    12. eventsource how to work EventSource JS API and server-sent events.
    13. file_server file server implementation.
    14. markdown_middleware generate markdown file from html.
    15. upload multipart upload.
    16. rest_basic_auth authentication implementation.
  • Add a EUnit test.

  • Fix rebar.config typo.

  • Fix img absence of Zotonic logo for ExDoc documentation generation.

Demo examples are provided with instructions that will help you navigate their functionality. Demo examples are provided with instructions that will help you navigate their functionality.

…ne API simular examples that solve the same tasks. Demo examples are provided with instructions that will help you navigate their functionality.
file(DirName, FileName) ->
{ok, CurrentDir} = file:get_cwd(),
ParentDir = filename:join([CurrentDir, DirName]),
filename:join([ParentDir, FileName]).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to add here something like mochiweb_util:safe_relative_path/1 - just as an example that we need to be safe.

Copy link
Contributor Author

@rustkas rustkas May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. In OTP app all works just fine.

It is necessary be safe (checking for the existence of a file) in any way.
In the developed examples, I offer several options (types of projects (escript and OTP app way)).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we just add a comment here to warn the user to sanitize the path before concatenation the file name to the directory. Then it is up to the programmer taking inspiration from this code.

Copy link
Contributor Author

@rustkas rustkas May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment about it would be enough. filename:join make a lot of sanitizing.


process(<<"GET">>, _ContentType, _Accepted, Context) ->
{true, Context};
process(<<"POST">>, _ContentType, _Accepted, Context) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we have something the wrong way around in another example, or piece of code.

The arguments of process/4 are

process(_Method, AcceptedCT, ProvidedCT, Context)

Where AcceptedCT is the content-type of the incoming payload, and ProvidedCT is the content-type of the response.

Copy link
Contributor Author

@rustkas rustkas May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not expect that this parameter is so useful.

process(<<"GET">>, _ContentType, _Accepted, Context) ->
{true, Context};
process(<<"POST">>, _ContentType, _Accepted, Context) ->
case cowmachine_req:resp_content_type(Context) of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to get the content-type of the payload here, which is the AcceptedCT.
This is the same as the AcceptedCT in the process/4 parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mworrell for describing this. I have added this simplification.

@mworrell mworrell merged commit ad592a3 into zotonic:master May 31, 2022
@mworrell
Copy link
Member

Thanks!

This will be very helpful to new people, and as a general documentation.

@rustkas rustkas deleted the examples branch May 31, 2022 12:16
@rustkas
Copy link
Contributor Author

rustkas commented May 31, 2022

Thanks!

This will be very helpful to new people, and as a general documentation.

I am glad to add some improvements to Cowmachine project.
There is for Cowmachine and Cowbow new people. It is good point for start. I hope it will be simple to switch from one Web Server to another (for OTP prjects). By default Web Server selection is unxpected (randomly selected).

../src/main.erl

cowboy_options(Dispatch) ->

    TypeOfCallingCowmachine = rand:uniform(2),
    % TypeOfCallingCowmachine = 1+1,
    case TypeOfCallingCowmachine of
        1 ->
            #{ 
                env => #{dispatch => Dispatch}
            };
        2 ->
            #{ 
                middlewares => [ 
                    % ... add your dispatcher middlware
                    controller,
                    cowmachine 
                ] 
            }
    end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants