Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

mochiweb adapter for cowboy.

License

NotificationsYou must be signed in to change notification settings

benoitc/mochicow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mochicow is a mochiweb adapter forcowboy.

There are 2 ways to use mochicow:

  • as a ranch protocol: It will use the socket acceptor pool of ranchinstead of the mochiweb one.

  • as a protocol upgrade. Like websockets you can upgrade a cowboyhandler to use a mochiweb loop. It allows you to use both cowboy andmochiweb in your code.

Use the ranch socket pool with mochiweb

To use mochiweb with the ranch acceptor pool, you just need to use themochicow_protocol module as the prococol when you start a cowboylistener. You pass the mochiwebloop in the protocol options via theloop property.

Ex:

-module(hello).-export([start/0,stop/0,loop/1]).-define(LOOP, {?MODULE,loop}).start()->    {ok,_}=application:ensure_all_started(ranch),ranch:start_listener(http,100,ranch_tcp, [{port,8000}],mochicow_protocol, [{loop,?LOOP}]).stop()->application:stop(ranch).loop(Req)->Path=Req:get(path),Resource=casestring:str(Path,"?")of0 ->Path;N ->string:substr(Path,1,length(Path)- (N+1))end,handle_request(Resource,Req).handle_request("/hello",Req)->Req:respond({200, [{"Content-Type","text/html"}], <<"Hello to you as well">>});handle_request(Path,Req)->Get=Req:parse_qs(),Post=Req:parse_post(),User_agent=Req:get_header_value("user-agent"),erlang:display({get,Get}),erlang:display({post,Post}),erlang:display({user_agent,User_agent}),erlang:display({path,Path}),Req:respond({200, [{"Content-Type","text/html"}], <<"Hello World!">>}).

Upgrade the protocol

You can use mochicow to quietly migrate your code from mochiweb tocowboy or use both at the sametime. To do that you will need to use theupgrade "sub-protocol" usingmochicow_upgrade as the protocol and compileyour code and the following option added to the erlang compiler flags:

{parse_transform,mochicow}

Alternately, you can add it to the module you wish to use with mochiweb:

-compile([{parse_transform,mochicow}]).

the mochicow parse_transform will replace at compilation any call tomochiweb_request by calls to mochicow_request. This is needed due to the waycowboy handle the first steps of the request keeping a buffer around.

Ex to start the cowboy_http_protocol:

-module(hello_cowboy).-export([start/0,stop/0]).-define(LOOP, {mochi_handler,loop}).start()->    {ok,_=application:ensure_all_started(cowboy),Dispatch=cowboy_router:compile([        {'_', [               {'_',mochi_hello_handler, [{loop, {mochi_hello_handler,loop}}]}             ]}    ]),cowboy:start_http(http,100,  [{port,8080}],                       [{env, [{dispatch,Dispatch}]} ]).stop()->application:stop(cowboy).

The mochiweb handler:

-module(mochi_hello_handler).-export([init/3,loop/1]).init(_,_,_)->    {upgrade,protocol,mochicow_upgrade}.loop(Req)->Req:respond({200, [{"Content-Type","text/html"}],                 <<"Hello from mochiweb">>}).

Usage example

See more usage examples in theexamples for the usage. For example launch thehello_cowboy application:

[upgrade_demo] rebar3 shell                                                                            2:39:25  ☁  master ☂ ⚡ ✭===> Verifying dependencies...===> Compiling mochicow===> Compiling upgrade_demoErlang/OTP 18 [erts-7.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:0] [kernel-poll:false]Eshell V7.3.1  (abort with ^G)1>hello_cowboy:start().{ok,<0.151.0>}2>

And query the server you just launched:

An echo server running a mochiweb handler (mochi_echo_handler)

[~] curl -XPOST http://localhost:8080/echo -d'test echo'               2:39:50testecho

A simple mochiweb handlermochi_hello_handler returning "hello":

[~] curl  http://localhost:8080/hello                                  9:30:22Hello from mochiweb

The server is mixed with a cowboy habdlercowboy_hello_handler:

[~] curl  http://localhost:8080                                        9:32:04Hello World!

About

mochiweb adapter for cowboy.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp