小编Ral*_*alf的帖子

http.Handle和http.HandleFunc之间的区别?

Go文档对http包有以下示例:

http.Handle("/foo", fooHandler)
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
Run Code Online (Sandbox Code Playgroud)

我有点难以理解Handle和HandleFunc之间的区别以及为什么需要两个.有人可以用清楚的语言向新的Gopher解释吗?

go

52
推荐指数
4
解决办法
2万
查看次数

在Go中需要HTTP Basic Auth的惯用法?

情况:

我正在使用Gorilla的mux作为路由器构建REST API.

我想知道如何使用简单的HTTP Basic Auth保护特定路由.我不需要从文件或任何外部源读取凭据,我真的只想通过硬编码的HTTP Basic Auth用户名和密码来保护选定的路由.

题:

在Go中这样做的惯用方法是什么?Gorilla是否提供任何东西以使其更容易?如果你能提供几行代码,那就太棒了.

go

35
推荐指数
4
解决办法
2万
查看次数

没有用xdebug,PhpStorm和Laravel 3/mod_rewrite命中的断点

我非常绝望并且没有想法:

我为Laravel 3项目配置了xdebug和PhpStorm.在Mac OS X Apache上本地运行项目,因此PhpStorm和Web应用程序在同一台机器上运行.配置虚拟主机,以便localhost.lt指向Laravel的公共目录.

php.ini中的相关xdebug条目:

zend_extension = /usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so
[xdebug]
xdebug.idekey="PHPSTORM"
xdebug.remote_enable=1
xdebug.profiler_enable=1
xdebug.remote_log=/var/log/xdebug_remote.log
xdebug.remote_connect_back=1
Run Code Online (Sandbox Code Playgroud)

确认扩展已加载.

设置PHP Web应用程序调试/运行配置,没有任何路径映射,因为没有符号链接,Web服务器和PhpStorm上的文件夹完全相同(因为Web服务器在同一台机器上).

当通过从IDE的"调试"发射,xdebug_remote.log正确地显示,我们已经在应用程序/库的一个文件中设置的断点:

<- breakpoint_set -i 5 -t line -f
file:///Users/RalfR/src/livetime/application/libraries/LiveTime.php -n 676
->
<response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="5" id="9230016"></response>
Run Code Online (Sandbox Code Playgroud)

但是,当我们单击从LiveTime.php库调用该函数的链接时,不会触发断点.日志显示:

<- stack_get -i 6 -> 
<response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="stack_get" transaction_id="6"><stack where="{main}" level="0" type="file" filename="file:///Users/RalfR/src/livetime/public/index.php" lineno="14"></stack></response>

<- run -i 7 -> 
<response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="run" transaction_id="7" status="stopping" reason="ok"></response>

<- run -i 8
Log closed at 2013-04-22 21:03:57
Run Code Online (Sandbox Code Playgroud)

与许多框架一样,Laravel使用.htaccess mod_rewrite通过public/index.php来管道所有内容.可能这是导致PhpStorm/xdebug无法捕获application/libraries/LiveTime.php中的断点的原因,因为xdebug似乎从未执行过LiveTime.php脚本?

如果是这样,我们如何解决这个问题呢?

xdebug phpstorm laravel laravel-3

13
推荐指数
1
解决办法
1万
查看次数

通过值或引用传递http.ResponseWriter?

假设我有一个中心方法,它为http.ResponseWriter添加一个特定的头.我不想使用HandleFunc包装器.

我想知道,我是否通过引用发送ResponseWriter.那么,什么是正确的:

addHeaders(&w)
Run Code Online (Sandbox Code Playgroud)

要么

addHeaders(w)
Run Code Online (Sandbox Code Playgroud)

问的不同:

func addHeaders(w *http.ResponseWriter) {...}
Run Code Online (Sandbox Code Playgroud)

要么

func addHeaders(w http.ResponseWriter) {...}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,我会说第一个版本是正确的,因为我不想创建ResponseWriter的副本.但我还没有看到任何代码,其中ResponseWriter通过引用传递,并想知道为什么.

谢谢!

go

12
推荐指数
1
解决办法
1580
查看次数

params不包含Elixir/Phoenix中的POST Body

我尝试构建一个非常简单的REST API.它不包括数据库或模型.

这是我的路由器:

defmodule Zentonies.Router do
  use Zentonies.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/v1/events/", Zentonies do
    pipe_through :api
    post "/call", PageController, :call
  end

end
Run Code Online (Sandbox Code Playgroud)

这是控制器:

defmodule Zentonies.PageController do
  require Logger
  import Joken
  use Zentonies.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end

  def call(conn, params) do
    Logger.debug inspect(params)
    conn
    |> put_status(200)
    |> text("Response.")
  end
end
Run Code Online (Sandbox Code Playgroud)

现在,如果我将HTTP POST发送到此端点,inspect(params)则不会返回我的POST请求的JSON主体.相反,它返回:call …

elixir phoenix-framework

7
推荐指数
1
解决办法
935
查看次数

将base64请求主体编码为二进制文件

我是Go语言的新手,很难实现以下目标:我收到一个base64字符串(基本上是一个编码图像),需要将它转换为服务器上的二进制形式.

func addOrUpdateUserBase64(w http.ResponseWriter, r *http.Request, params martini.Params) {
    c := appengine.NewContext(r)
    sDec, _ := b64.StdEncoding.DecodeString(r.Body)
...
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为DecodeString需要一个字符串......如何将request.Body转换为字符串?非常感谢任何提示!

go

4
推荐指数
1
解决办法
2105
查看次数

Auth gsutil作为服务帐户

我们正在使用App Engine中的Google云端存储,需要对20.000多个对象应用一些ACL调整.我们想使用gsutil,因为它似乎是为这样的用例而制作的.

不幸的是,出于任何奇怪的原因,App Engine使用服务帐户创建的对象不归桶的任何其他所有者所有.因此gsutil失败了.

我们如何将gsutil作为服务帐户使用其批量更改功能?

谷歌支持没有答案......有人在这里吗?

这是一次令人沮丧的经历,我们认为将这部分转移到S3,其中应用程序不排除人类所有者.

google-app-engine google-cloud-storage

4
推荐指数
1
解决办法
479
查看次数

(RuntimeError)预期的操作/ 2返回一个Plug.Conn,所有插件都必须接收一个连接(conn)并返回一个连接

在我的一个控制器中,我有以下代码(节选):

case HTTPoison.get("https://*****.zendesk.com/api/v2/users/search.json?query=" <> clid, headers, [hackney: hackney]) do
  {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
    conn
    |> put_status(200)
    |> json(body)
  {:ok, %HTTPoison.Response{status_code: 404}} ->
    conn
    |> put_status(404)
    |> json(%{error_code: "404", reason_given: "Resource not found."})
  {:error, %HTTPoison.Error{reason: reason}} ->
    conn
    |> put_status(500)
    |> json(%{error_code: "500", reason_given: "None."})
end
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,它工作正常,但是Phoenix抛出了运行时异常:

** (exit) an exception was raised:
    ** (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection
        (zentonies) web/controllers/page_controller.ex:1: Zentonies.PageController.phoenix_controller_pipeline/2
        (zentonies) lib/zentonies/endpoint.ex:1: Zentonies.Endpoint.instrument/4
        (zentonies) …
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework

3
推荐指数
1
解决办法
1906
查看次数

关于通过 BLE 实施固件 OTA 的任何好的资源?

我很想很好地了解应用于通过蓝牙低功耗 4.1 及更高版本执行无线固件更新(FOTA / OTA 通过 BLE)的策略。

假设客户端是 Android 和/或 iOS 设备。

非常感谢任何提示和帮助。

bluetooth-lowenergy

3
推荐指数
1
解决办法
2799
查看次数

不会调用NSCachedURLResponse willCacheResponse

我们根据下面的缩写代码片段设置了一个简单的NSURLConnection和NSURLCache.我们已经确定服务器(localhost:9615)返回以下缓存头:

ETag : abcdefgh
Cache-Control : public, max-age=60
Run Code Online (Sandbox Code Playgroud)

但是,永远不会调用willCacheResponse委托方法.任何的想法?

码:

// In the app delegate
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

// Triggered by a UIButton
- (IBAction)sendRequest:(UIButton *)sender {
    NSLog(@"sendRequest");
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:9615"] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
    NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!theConnection) {
        receivedData = nil;
    }
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    NSLog(@"willCacheResponse");
    // ...
}
Run Code Online (Sandbox Code Playgroud)

nsurlconnection nsurlcache ios

2
推荐指数
1
解决办法
2134
查看次数