How to serve static files over http - go

I'm following a tutorial on building a webpage in go. Everything in the tutorial is easy to grasp, but I'm trying to expand on it. Specifically, I'm trying to add some static files (pictures). I've been going through the go docs and came across FileServer and adding
http.ServeFile(w, r, "/home/jeff/web/foo.jpg")
in my handler I see an image being served but it's not using the template
<h1>{{.Title}}</h1>
<p>[edit]</p>
<img src="foo.jpg" alt="moooooo">
<img src="foo.jpg" alt="foooooo">
<div>{{printf "%s" .Body}}</div>
*I've tried giving the full path to the images too.
What I'm trying to do is get the image to occupy the html tags that I've placed so carefully in the template.
I want the image to appear where I tell them to, but get blank images where they should be. I'm not seeing any errors saying the file can't be found.
The way I think this should work (again no experience in this) is by telling the server I have this directory that houses some static files and whenever a template requests an image check here and if found serve it. Doesn't appear to be this simple. What am I doing wrong? How can I get this to work?
I'm using http.ListenAndServe(":8080", nil) in my main in other words I'm not using apache or some other web-server

The images should be served from a different URL path to the templates.
You need to define where the static files will be served from using something like:
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/home/jeff/web/"))))
and then make sure the <IMG> source URLs are something like:
<img src="/static/foo.jpg" alt="moooooo">
Hope that helps.

Related

404 Page Not Loading Images (File Source / Layers Problem)

I'm facing what could be a simple problem, but haven't been able to find any solutions yet.
I just recently built a simple 404 page on my website (nofound.html) which through a .htaccess redirection (ErrorDocument 404 /nofound.html) allows me to catch all URL errors and such. Basic.
The problem is that, since the 404 response can be called within different directories, say for example index, or /dir1, or /dir1/dir2, etc, the page is not loading its styles and images right, since the source paths do not link correctly (image.png links correctly from index/nofound.html, but should become ../image.png and so on when going into different directory layers).
I've managed to make the styles load correctly by loading the same stylesheet twice (styles.css AND ../styles.css), but for the images, I have not found any workaround yet (other than duplicating the image within layered directories, which is cumbersome and redundant).
Any thoughts? Thanks in advance!
Although it does not follow best practices, my final solution was to simply use absolute hyperlinks (such as https://website/img.png or something like that) rather than relative links within the website's anchors (../../img.png)
As such, every element on the page is loaded from its absolute -actual- directory, regardless of the relative relationship between the nofound.html result and the rest of the site architecture.

Replace a remote image with a local image on static web pages

I have an image that displays stock quotes update on our website. This is being fed by an external site with a static path pointing to this remote image. (This image gets updated with stock info there). The updates don't happen now and I will have to remove this image on several static web pages on my site until something else is in place.
Is there a way I can over-write this image easily on all pages with a local image within the site? I was wondering if there is a way I can get this done on the site level, may be tweak the apache conf file. Any other work around suggestions. Please let me know. Thanks!!
Add crossorigin attribute to get images from 3rd party websites
example:
<img height="100" width="100" crossorigin=anonymous src="https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAAjTAAAAJDNlOGJiNTRkLTJhMzMtNDZlMi05NjJiLTZmMGRhODkzMzVkNw.jpg" />

TeamCity to serve static HTML page

I am looking for a way to serve a few static HTML pages with TeamCity.
I dont want to set up an apache for that, if not absolutely neccessary.
Does anybody know a simple way (or URL scheme) to access static HTML content. I found the following plugin, but that only inserts snippets of HTML in certain positions on existing pages. No way to include a full page. PluginLink for others as help.
Thanks for ideas,
Chris
Solution:
Go to e.g. C:\TeamCity\webapps\ROOT
Create folder e.g. static
Place file in it (but extension .jsp even if html)
Will be served without any problems (on URL/static/test.jsp)
It's a Tomcat server, just go on your file system where you installed Teamcity and you should be able to find out where you can park some html that will then be available on the Teamcity urls.

Remove all external resources from HTML with Nokogiri

I want to remove all external resources from an html file.
I am using wget to make some local copies of a page. Wget has options to convert links to local file system and it is quite ok but still some links (at the end of the download depth I believe) keep their external src, so they contain http.
The closest I could get to find everything that contains http is using this:
doc.search("//*[starts-with(#href, 'http')]")
But that just finds the href elements and http can also in images, videos and anything.
Any ideas what would be the right instructions for Nokogiri to tell me everything that contains http?
Thanks.
If you simply want to expand your search to elements with any attribute starting with 'http' you can do this:
doc.search("//*[#*[starts-with(.,'http')]]")

how to aviod default break image icon if an image is not loaded from a file?

I have used a tag with attribute src="mydirectory/myimage.gif.
If the image is not present in the directory , how can i avoid showing a default break image inside my img tag ?
this is present in chrome and I.E browsers but not in Firefox...
Any idea to avoid this ?
Thank You ...
A popular way to do it is to edit your web server.
for example, on apache, if you store all your images in a directory "http://host/images",
and add a .htaccess file to that directory to redirect all 404s to serve a default blank image.
see here:
Can Apache serve a default file instead of a 404?
but you are serving an image instead of any kind of file.
There are other ways of doing it, for example, if you have access to a server side language (php, ruby) you can check if an image file exists on your server first, if not, serve a default image.
Or use the javascript method as mentioned by Pekka.
You can do it with your web server, server side / client side script.
I dont know if there's a way to do it with CSS, or other techniques, sure others will fill in.
Hope that is helpful.

Resources