The previous period of time to guide the ranch source code, specifically see Ranch source Analysis (a),
Now we have sorted out the classic application cowboy under the Ranch framework.
SOURCE Place: Https://github.com/ninenines/cowboy ( currently using the cowboy-1.0.4 version )
Or find one of the simplest examples to start with, then choose this Static_world bar ~ ~
First, let's see Static_world_app.erl.
[email protected] src]# cat Static_world_app.erl%%feel free to use, reuse and abuse the code in this file.%%@private-Module(Static_world_app).-behaviour (application).%%API.-Export([START/2]).-Export([STOP/1]).%%API.Start (_type, _args)-Dispatch=Cowboy_router:compile ([{‘_‘, [ {"/", Cowboy_static, {priv_file, Static_world, "index.html"}}, {"/[...]", cowboy_static, {priv_dir, Static_world, "", [{mimetypes, Cow_mimetypes, all}}} ]}]), {OK, _}= Cowboy:start_http (http, +, [{port, 8080}], [{env, [{Dispatch, Dispatch}]}]), Static_world_sup:start_link (). Stop (_state)-OK.
Cowboy_router.erl is the path routing settings, we do not look at this source code, directly look at the results
1> cowboy_router:compile ([1> {' _ '), [1> {"/", Cowboy_static, {priv_file, static_ World, "index.html"},1> {"/[...]", cowboy_static, {priv_dir, Static_world, "",1> [{mimetypes, Cow_mimetypes, All}}} 1> ]}1> ]). [{' _ ', [], [{[],[],cowboy_static,{priv_file,static_world,' index.html '}}, {[ ' ... ', [],cowboy_static, {priv_dir,static_world,[], [{mimetypes,cow_mimetypes,all}] }}]}]
In fact, this route means that if the request is/, it is routed under the index.html of the Priv directory,
Otherwise, the requested file is read directly.
And then we start looking at COWBOY:START_HTTP/4, which is the main process of application initiation,
-Module(Cowboy).-Export([START_HTTP/4]).-Export([START_HTTPS/4]).-Export([START_SPDY/4]).-Export([STOP_LISTENER/1]).-Export([SET_ENV/3]).omit several lines in percent-Spec Start_http (Ranch:ref (), Non_neg_integer (), ranch_tcp:opts (), cowboy_protocol:opts ()){OK, pid ()} |{error, any ()}. start_http (Ref, Nbacceptors, transopts, protoopts) whenIs_integer (nbacceptors), nbacceptors > 0 Ranch:start_listener(Ref, Nbacceptors, Ranch_tcp, transopts, Cowboy_protocol, protoopts).-Spec Start_https (Ranch:ref (), Non_neg_integer (), ranch_ssl:opts (), cowboy_protocol:opts ()){OK, pid ()} |{error, any ()}.start_https (Ref, Nbacceptors, transopts, protoopts) whenIs_integer (nbacceptors), nbacceptors > 0Ranch:start_listener (Ref, Nbacceptors,
Omit several lines in percent
Can be seen here to start ranch, and Cowboy_protocol is defined user process, according to ranch source understanding, we know will start COWBOY_PROTOCOL:START_LINK/4 to process the user's application data request
So here we go into Cowboy's code world, starting with COWBOY_PROTOCOL:START_LINK/4.
(not to be continued ~)
Cowboy Source Analysis (i)