Problem with routing

agolangf · · 585 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello!</p> <p>I have an annoying problem with routing. I have an image storage with random directories generated from a random string and split into multiple pieces. This storage should be outside of my project folder or should be changeable. So I have a route with /img/{id} which returns an html page with an image with the given id. The problem is, that I don&#39;t/can&#39;t (?) render the image into the template. I just render the path into it and therefore the route the page calls should be imgstrg/{path}, which will then return the image itself to use in the html page. Now the page doesn&#39;t just call (localhost:8000)/imgstrg/{path} but rather calls /img/imgstrg/{path} which then (of course) can&#39;t work. If I pass the real path to the html template, it still searches for the file in the /img directory, which is just non-existent. How would I get rid of the /img? Is there any way? It&#39;s really annoying.</p> <p>Thanks in advance!</p> <hr/>**评论:**<br/><br/>fouljabber: <pre><p>Try the <a href="http://golang.org/pkg/net/http/#StripPrefix" rel="nofollow">StripPrefix method</a> in the net/http package </p></pre>mko31: <pre><pre><code>r.Handle(&#34;/imgstrg/&#34;, http.StripPrefix(&#34;/img/&#34;, http.FileServer(http.Dir(fs.ImgStoragePath)))) </code></pre> <p>I used it like this, but it still doesn&#39;t work :/</p></pre>fouljabber: <pre><p>Try this :</p> <p>r.Handle(&#34;/imgstrg/&#34;, http.StripPrefix(&#34;<strong>/img</strong>&#34;, http.FileServer(http.Dir(fs.ImgStoragePath))))</p> <p>I removed the forward slash from &#34;/img/&#34; </p></pre>mko31: <pre><p>I tried it and it&#39;s still not working. I&#39;m almost 100% sure I made a fundamental because I&#39;m like really tired.</p> <p><a href="https://github.com/mkocs/imgturtle/tree/rewr_org" rel="nofollow">https://github.com/mkocs/imgturtle/tree/rewr_org</a> I&#39;d be very happy if you could take a look. :)</p></pre>fouljabber: <pre><p>I&#39;m guessing that you are working on http.go line 40. I can&#39;t find anything wrong with the code. line 40 is fairly isolated form the rest of your code, because you didn&#39;t define a handler for it; therefore, it is likely that fixing this one line could solve your problem. I suggest creating a new servemux using the net/http package and commenting out the gorrilla/mux and negroni code so that you can try troubleshooting methods with line 40.</p></pre>mko31: <pre><p>Yes it&#39;s line 40. I just can&#39;t figure out a way for it to work. But I guess I&#39;m gonna change a few things and transform the website into a single-page website. Do you have any experience with that? Does templating work that way?</p></pre>fouljabber: <pre><p>Sorry I don&#39;t have much experience with that right now. I actually just started to build the front end for my restful api in reactjs, so I am still trying to figure out the process for myself.</p></pre>calebdoxsey: <pre><p>I tried to run your code but it just failed for me:</p> <pre><code>PANIC: runtime error: slice bounds out of range /home/caleb/src/imgturtle/http/reqimg.go:167 +0xbaf </code></pre> <p>Debugging it was going to take too long, so I&#39;ll just give you some ideas and maybe you can figure it out.</p> <ol> <li><p>You actually can render an image directly into a template using a data URI (<a href="https://en.wikipedia.org/wiki/Data_URI_scheme" rel="nofollow">https://en.wikipedia.org/wiki/Data_URI_scheme</a>). I don&#39;t know if doing this is a good idea, but its certainly possible.</p></li> <li><p>You should use an absolute URL instead of a relative one. So in your img src it should be <code>/imgstrg/{path}</code> not <code>imgstrg/{path}</code>.</p></li> </ol></pre>mko31: <pre><p>I think it fails for you because you need to provide some parameters. Should have mentioned that. But I can&#39;t really use an absolute URL. The directory is (or can be) outside of the project folder. How would I link to that folder other than ../imagestorage ? Even if I create a route with </p> <pre><code>r.HandleFunc(&#34;/imgstrg&#34;, imgSrcHandler) </code></pre> <p>and have a imgSrcHandler function, the function never gets called. All I get is the default &#34;Not found&#34; error from my errorHandler</p></pre>th3noname: <pre><p>You are building a image URL that beginns with &#34;./imgstorage/&#34;</p> <p>That is not the URL you want because it tells your browser that the image is based in a subdirectory of /img. Your URL needs to start with a leading slash.</p> <p>In another comment you said that you wanted to use URL like ../imagestorage to load images from a parent directory. That is not possible since the Go FileServer calls <a href="http://golang.org/pkg/path/#Clean" rel="nofollow">path.Clean</a> which removes relative path information. The FileServer only allows access to sub not parent directorys. </p></pre>mko31: <pre><p>When I use it like /imgstrg/&lt;path&gt;.&lt;file extension&gt; and use a handler for /imgstrg and link a file server in the imgstorage outside of the project folder (which is what I meant by ../imgstorage) out it still never gets called. </p> <pre><code>INF: File /Users/&lt;name&gt;/Documents/workspace/go/src/imgstorage/4c0/0f4/883/e23/f4463c0.jpg has been created. INF: Content of uploaded image (976914 Bytes) has been copied to /Users/&lt;name&gt;/Documents/workspace/go/src/imgstorage/4c0/0f4/883/e23/f4463c0.jpg. INF: serving static file =&gt; img.html with image 4c0/0f4/883/e23/f4463c0.jpg INF: request for /imgstrg/4c0/0f4/883/e23/f4463c0.jpg. 404, not found. INF: serving static file =&gt; error.html </code></pre> <p>This is the upload success message and the error message when trying to fetch the image</p> <p>The handler looks like this:</p> <pre><code>r.Handle(&#34;/imgstrg&#34;, (http.StripPrefix(&#34;/img&#34;, http.FileServer(http.Dir(fs.ImgStoragePath))))) </code></pre> <p>fs.ImgStoragePath = /Users/&lt;name&gt;/Documents/workspace/go/src/imgstorage/</p> <p>and my path string for the template is now being built like this (just wasn&#39;t on GitHub):</p> <pre><code>img := Img{title, &#34;/imgstrg/&#34; + imgPath} </code></pre></pre>th3noname: <pre><p>Could you please change the strip prefix to:</p> <pre><code>r.Handle(&#34;/imgstrg&#34;, (http.StripPrefix(&#34;/imgstrg&#34;, http.FileServer(http.Dir(fs.ImgStoragePath))))) </code></pre></pre>mko31: <pre><p>Thanks for trying to help. The error remains...I also tried to just place the directory in the project root and change the path in the template to /imgstorage/&lt;path&gt;.&lt;file extension&gt;. It can&#39;t even find that. It only works, if the imgstorage directory is placed in public/img/</p></pre>th3noname: <pre><p>The fs.ImgStoragePath var has no value assigned within the http package context. Please check that the variable has the right value.</p></pre>mko31: <pre><p>It does have a value. The value is whatever you assign via environment variable or user input. In my case right now, it&#39;s &#34;imgstorage/&#34;</p></pre>mko31: <pre><p>I managed to fix the problem. I now have a route /{id} to serve the html page with the image path and a route /img/{id} to serve the image file itself. So glad it works now. </p></pre>

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

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