{"id":16889902,"url":"https://github.com/benoitc/mochicow","last_synced_at":"2025-03-22T08:31:06.231Z","repository":{"id":3538527,"uuid":"4598447","full_name":"benoitc/mochicow","owner":"benoitc","description":"mochiweb adapter for cowboy.","archived":false,"fork":false,"pushed_at":"2016-06-20T07:10:13.000Z","size":167,"stargazers_count":29,"open_issues_count":0,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-17T11:52:33.780Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benoitc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-08T14:51:26.000Z","updated_at":"2020-01-21T11:08:59.000Z","dependencies_parsed_at":"2022-09-06T04:40:19.658Z","dependency_job_id":null,"html_url":"https://github.com/benoitc/mochicow","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benoitc%2Fmochicow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benoitc%2Fmochicow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benoitc%2Fmochicow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benoitc%2Fmochicow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benoitc","download_url":"https://codeload.github.com/benoitc/mochicow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244931481,"owners_count":20534007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T16:59:44.065Z","updated_at":"2025-03-22T08:31:05.912Z","avatar_url":"https://github.com/benoitc.png","language":"Erlang","readme":"# mochicow\n\nmochicow is a mochiweb adapter for [cowboy](http://github.com/extend/cowboy).\n\nThere are 2 ways to use mochicow:\n\n- as a ranch protocol: It will use the socket acceptor pool of ranch\n  instead of the mochiweb one.\n\n- as a protocol upgrade. Like websockets you can upgrade a cowboy\n  handler to use a mochiweb loop. It allows you to use both cowboy and \n  mochiweb in your code.\n\n\n## Use the ranch socket pool with mochiweb\n\nTo use mochiweb with the ranch acceptor pool, you just need to use the\n`mochicow_protocol` module as the prococol when you start a cowboy\nlistener. You pass the mochiweb `loop` in the protocol options via the\n`loop` property.\n\nEx:\n\n```erlang\n-module(hello).\n\n-export([start/0, stop/0, loop/1]).\n-define(LOOP, {?MODULE, loop}).\n\n\nstart() -\u003e\n    {ok, _} = application:ensure_all_started(ranch),\n    ranch:start_listener(http, 100,\n                          ranch_tcp, [{port, 8000}],\n                          mochicow_protocol, [{loop, ?LOOP}]).\n\nstop() -\u003e\n  application:stop(ranch).\n\n\nloop(Req) -\u003e\n    Path = Req:get(path),\n    Resource = case string:str(Path, \"?\") of\n        0 -\u003e Path;\n        N -\u003e string:substr(Path, 1, length(Path) - (N + 1))\n    end,\n    handle_request(Resource, Req).\n\n\nhandle_request(\"/hello\", Req) -\u003e\n    Req:respond({200, [{\"Content-Type\", \"text/html\"}], \u003c\u003c\"Hello to you as well\"\u003e\u003e});\n\nhandle_request(Path, Req) -\u003e\n    Get = Req:parse_qs(),\n    Post = Req:parse_post(),\n    User_agent = Req:get_header_value(\"user-agent\"),\n    erlang:display({get, Get}),\n    erlang:display({post, Post}),\n    erlang:display({user_agent, User_agent}),\n    erlang:display({path, Path}),\n    Req:respond({200, [{\"Content-Type\", \"text/html\"}], \u003c\u003c\"Hello World!\"\u003e\u003e}).\n```\n\n\n## Upgrade the protocol\n\nYou can use mochicow to quietly migrate your code from mochiweb to\ncowboy or use both at the sametime. To do that you will need to use the\nupgrade \"sub-protocol\" using `mochicow_upgrade` as the protocol and compile \nyour code and the following option added to the erlang compiler flags:\n\n```erlang\n{parse_transform, mochicow}\n```\n\nAlternately, you can add it to the module you wish to use with mochiweb:\n\n```erlang\n-compile([{parse_transform, mochicow}]).\n```\n\n\u003e the mochicow parse_transform will replace at compilation any call to \nmochiweb_request by calls to mochicow_request. This is needed due to the way \ncowboy handle the first steps of the request keeping a buffer around. \n\n\n\n### Ex to start the cowboy_http_protocol:\n\n```erlang\n-module(hello_cowboy).\n-export([start/0, stop/0]).\n\n-define(LOOP, {mochi_handler, loop}).\n\nstart() -\u003e\n    {ok, _} = application:ensure_all_started(cowboy),\n     Dispatch = cowboy_router:compile([\n\n        {'_', [\n               {'_', mochi_hello_handler, [{loop, {mochi_hello_handler, loop}}]}\n             ]}\n    ]),\n\n    cowboy:start_http(http, 100,  [{port, 8080}], \n                      [{env, [{dispatch, Dispatch}]} ]).\n\nstop() -\u003e\n    application:stop(cowboy).\n```\n\n\n### The mochiweb handler:\n\n```erlang\n-module(mochi_hello_handler).\n-export([init/3, loop/1]).\n\ninit(_, _, _) -\u003e\n    {upgrade, protocol, mochicow_upgrade}.\n\nloop(Req) -\u003e\n    Req:respond({200, [{\"Content-Type\", \"text/html\"}],\n                 \u003c\u003c\"Hello from mochiweb\"\u003e\u003e}).\n```\n\n### Usage example\n\nSee more usage examples in the `examples` for the usage. For example launch the\n`hello_cowboy` application:\n\n```bash\n[upgrade_demo] rebar3 shell                                                                            2:39:25  ☁  master ☂ ⚡ ✭\n===\u003e Verifying dependencies...\n===\u003e Compiling mochicow\n===\u003e Compiling upgrade_demo\nErlang/OTP 18 [erts-7.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:0] [kernel-poll:false]\n\nEshell V7.3.1  (abort with ^G)\n1\u003e hello_cowboy:start().\n{ok,\u003c0.151.0\u003e}\n2\u003e\n```\n\nAnd query the server you just launched:\n\nAn echo server running a mochiweb handler (`mochi_echo_handler`)\n\n```bash\n[~] curl -XPOST http://localhost:8080/echo -d'test echo'               2:39:50\ntest echo\n```\n\nA simple mochiweb handler `mochi_hello_handler` returning \"hello\":\n```bash\n[~] curl  http://localhost:8080/hello                                  9:30:22\nHello from mochiweb\n```\n\nThe server is mixed with a cowboy habdler `cowboy_hello_handler`:\n```\n[~] curl  http://localhost:8080                                        9:32:04\nHello World!\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenoitc%2Fmochicow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenoitc%2Fmochicow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenoitc%2Fmochicow/lists"}