Erlang学习:MochiWeb + ERedis

来源:互联网 发布:录屏软件画中画 编辑:程序博客网 时间:2024/04/30 12:19

接上篇: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.


原创粉丝点击