关闭

Erlang学习:MochiWeb + ERedis

807人阅读 评论(0) 收藏 举报
分类:

接上篇:http://blog.csdn.net/kunshan_shenbin/article/details/17066221

启动Redis,教程:http://redis.io/

loop(Req, DocRoot) ->
    "/" ++ Path = Req:get(path),
    try
        case Req:get(method) of
            Method when Method =:= 'GET'; Method =:= 'HEAD' ->
                case Path of
                    _ ->
                        %Req:serve_file(Path, DocRoot)
						{ok, Client} = eredis:start_link("127.0.0.1", 6379, 0, ""),
						{{Y,M,D},{H,I,S}} = calendar:local_time(),
						Date = string:join([integer_to_list(Y), integer_to_list(M), integer_to_list(D)], "-"),
						Time = string:join([integer_to_list(H), integer_to_list(I), integer_to_list(S)], ":"),
						{ok, <<"OK">>} = eredis:q(Client, ["SET", "datetime", Date ++ " " ++ Time]),
						{ok, Result} = eredis:q(Client, ["GET", "datetime"]),
						Req:respond({200, [{"Content-Type", "text/plain"}], Result}),
						eredis:stop(Client)
                end;
            'POST' ->
                case Path of
                    _ ->
                        Req:not_found()
                end;
            _ ->
                Req:respond({501, [], []})
        end
    catch
        Type:What ->
            Report = ["web request failed",
                      {path, Path},
                      {type, Type}, {what, What},
                      {trace, erlang:get_stacktrace()}],
            error_logger:error_report(Report),
            %% NOTE: mustache templates need \ because they are not awesome.
            Req:respond({500, [{"Content-Type", "text/plain"}],
                         "request failed, sorry\n"})
    end.


0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1667792次
    • 积分:28446
    • 等级:
    • 排名:第189名
    • 原创:1092篇
    • 转载:443篇
    • 译文:1篇
    • 评论:467条
    最新评论