Serving static content and access to http request by client?

blov · · 321 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m running a website with a very basic webserver</p> <pre><code>http.Handle(&#34;/&#34;, http.FileServer(http.Dir(&#34;static&#34;))) http.Handle(&#34;/ws&#34;, websocket.Handler(EchoServer)) log.Println(&#34;Listening at 3000...&#34;) if err := http.ListenAndServe(&#34;:3000&#34;, nil); err != nil { log.Fatal(&#34;ListenAndServe:&#34;, err) } </code></pre> <p>I need access to http.responseWriter in particular for the IP&#39;s of connecting client. I thought about using http.HandlerFunc, but nothing is served when I visit localhost:3000.</p> <pre><code>http.HandleFunc(&#34;/&#34;, func(w http.ResponseWriter, r *http.Request) { http.FileServer(http.Dir(&#34;static&#34;)) }) http.Handle(&#34;/ws&#34;, websocket.Handler(EchoServer)) log.Println(&#34;Listening at 3000...&#34;) if err := http.ListenAndServe(&#34;:3000&#34;, nil); err != nil { log.Fatal(&#34;ListenAndServe:&#34;, err) } </code></pre> <p>How can I server static files and still have access to http.request? Thanks!</p> <hr/>**评论:**<br/><br/>patrickdappollonio: <pre><p>You can change the code inside the <code>/</code> handler to be:</p> <pre><code>f, err := os.Open(r.URL.Path) if err != nil { if os.IsNotExists(err) { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } http.Error(w, err.Error(), http.StatusInternalServerError) return } http.ServeContent(w, r, info.Name(), info.ModTime(), f) f.Close() </code></pre> <p>It will require some extra code check just to know stuff like if the file exists, or if the path is a correct one and so on, but that way, you can still serve your static asset.</p> <p>The reason why you&#39;re not getting is because you&#39;re using <code>http.FileServer</code> which is a full featured file server, which is a handler by itself, but not getting called as a handler. It&#39;s meant to be passed to a function that can execute handlers.</p></pre>patrickdappollonio: <pre><p>I think you can also use <code>http.ServeFile</code> by changing all the above code to:</p> <pre><code>http.ServeFile(w, r, r.URL.Path) </code></pre> <p>This is way less verbose but the above code can help you out understanding the overall flow.</p></pre>julianxxxx: <pre><p>Thanks but your solution doesn&#39;t quite work. Not sure why. This solution worked for me</p> <pre><code>func homeHandler(w http.ResponseWriter, r *http.Request) { http.FileServer(http.Dir(&#34;static&#34;)).ServeHTTP(w, r) if r.URL.Path == &#34;/&#34; { r.RemoteAddr = strings.Replace(r.RemoteAddr, &#34;[::1]&#34;, &#34;localhost&#34;, -1) log.Printf(&#34;Request of %v from %v&#34;, r.URL.Path, r.RemoteAddr) } } func main() { http.HandleFunc(&#34;/&#34;, homeHandler) http.Handle(&#34;/ws&#34;, websocket.Handler(EchoServer)) log.Println(&#34;Serving to :3000&#34;) log.Fatal(http.ListenAndServe(&#34;:3000&#34;, nil)) } </code></pre></pre>patrickdappollonio: <pre><p>I think it&#39;s because in your code you&#39;re serving the &#34;static&#34; folder, whereas in my code I&#39;m serving <code>r.URL.Path</code></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

321 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传