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开始

    (未完待续~)

  • 相关阅读:
    升级.net core 3.x 后mvc项目调试状态编辑view代码不能实时预览
    C# 方法执行超时处理
    css 带换行的垂直居中
    Golang Web应用 创建docker镜像笔记(win 平台)
    vue element-ui tree 根节点固定子节点懒加载 首次加载根节点并展开
    记一则 Lambda内递归调用方法将集合对象转换成树形结构
    一个简单的异常/条件重试类(C#)
    查找由于页面宽度溢出导致网页出现莫名空白的查看方法
    锯齿下边框效果
    cdh5.7 做完HA后hive 查询出现异常: expected: hdfs://nameservice
  • 原文地址:https://www.cnblogs.com/tudou008/p/5652362.html
Copyright © 2011-2022 走看看