<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't/can'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't just call (localhost:8000)/imgstrg/{path} but rather calls /img/imgstrg/{path} which then (of course) can'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'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("/imgstrg/", http.StripPrefix("/img/", http.FileServer(http.Dir(fs.ImgStoragePath))))
</code></pre>
<p>I used it like this, but it still doesn't work :/</p></pre>fouljabber: <pre><p>Try this :</p>
<p>r.Handle("/imgstrg/", http.StripPrefix("<strong>/img</strong>", http.FileServer(http.Dir(fs.ImgStoragePath))))</p>
<p>I removed the forward slash from "/img/" </p></pre>mko31: <pre><p>I tried it and it's still not working. I'm almost 100% sure I made a fundamental because I'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'd be very happy if you could take a look. :)</p></pre>fouljabber: <pre><p>I'm guessing that you are working on http.go line 40. I can't find anything wrong with the code. line 40 is fairly isolated form the rest of your code, because you didn'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's line 40. I just can't figure out a way for it to work. But I guess I'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'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'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'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'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("/imgstrg", imgSrcHandler)
</code></pre>
<p>and have a imgSrcHandler function, the function never gets called. All I get is the default "Not found" error from my errorHandler</p></pre>th3noname: <pre><p>You are building a image URL that beginns with
"./imgstorage/"</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/<path>.<file extension> 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/<name>/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/<name>/Documents/workspace/go/src/imgstorage/4c0/0f4/883/e23/f4463c0.jpg.
INF: serving static file => 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 => 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("/imgstrg", (http.StripPrefix("/img", http.FileServer(http.Dir(fs.ImgStoragePath)))))
</code></pre>
<p>fs.ImgStoragePath = /Users/<name>/Documents/workspace/go/src/imgstorage/</p>
<p>and my path string for the template is now being built like this (just wasn't on GitHub):</p>
<pre><code>img := Img{title, "/imgstrg/" + imgPath}
</code></pre></pre>th3noname: <pre><p>Could you please change the strip prefix to:</p>
<pre><code>r.Handle("/imgstrg", (http.StripPrefix("/imgstrg", 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/<path>.<file extension>. It can'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's "imgstorage/"</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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传