zoukankan      html  css  js  c++  java
  • cowboy源码分析(一)

    前段时间导读了ranch的源码,具体见ranch 源码分析(一)

    现在整理了下ranch框架下经典应用cowboy。

    源码地方:https://github.com/ninenines/cowboy目前使用的是cowboy-1.0.4版本

    还是找其中最简单的例子来入手,那就选这个static_world吧~~

    首先我们来看static_world_app.erl

    [root@erlang004 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, 100, [{port, 8080}], [
            {env, [{dispatch, Dispatch}]}
        ]),
        static_world_sup:start_link().
    
    stop(_State) ->
        ok.

    cowboy_router.erl是路径路由设置,我们就不看这个源码,直接看一下结果

    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}]}}]}]

    其实这个路由的意思是如果请求的是/,就路由在priv目录的index.html下面,

    否则就直接读取请求的文件。

    然后我们开始看cowboy:start_http/4,就是应用启动的主进程,

    -module(cowboy).
    
    -export([start_http/4]).
    -export([start_https/4]).
    -export([start_spdy/4]).
    -export([stop_listener/1]).
    -export([set_env/3]).
    
    %%省略若干行
    
    -spec start_http(ranch:ref(), non_neg_integer(), ranch_tcp:opts(),
        cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
    start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
            when is_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)
            when is_integer(NbAcceptors), NbAcceptors > 0 ->
        ranch:start_listener(Ref, NbAcceptors,

    %%省略若干行
     

    可以看到这里启动ranch,而cowboy_protocol就是定义的用户进程,根据ranch源码(忘记了的参见ranch源码分析)理解,我们知道会启动cowboy_protocol:start_link/4来处理用户的应用数据请求

    下面我们就进入cowboy的代码世界,从cowboy_protocol:start_link/4开始

    (未完待续~)

  • 相关阅读:
    向量代数与空间解析几何(前篇)
    操作系统(笔试系列)-第七讲设备管理
    win10系统IIS服务器配置
    IIS本地部署Arcgis for js API开发文档
    IIS本地部署Arcgis for js API
    vue中使用mockjs服务器测试项目
    vue动态配置嵌套页面(含iframe嵌套)可实现白天夜间皮肤切换
    如何在vue项目打包去掉console
    Vue之element table 后端排序实现
    D3.tsv与D3.csv加载数据
  • 原文地址:https://www.cnblogs.com/tudou008/p/5652362.html
Copyright © 2011-2022 走看看