3

I'm new to go and web apps and trying do write an app that reads menu items from a database and send the item's description and the link of a picture related to it through an HTML page for a browser. The app will run on a local network so the image will be in a place like "c:\images\01.png". As of now the page source created is something like

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <p> <a href="http://localhost:8000/api/getitensdomenu/33">Bolinho de Aipim</a><img src="01.png" alt="Ícone do Menu" width="42" height="42"></p>
    <p> <a href="http://localhost:8000/api/getitensdomenu/32">Bolinho de Bacalhau</a><img src="01.png" alt="Ícone do Menu" width="42" height="42"></p>
</body>

where 01.png is the picture file located on the same folder as the go serever app on the LAN server. Of course it will show the same file, but this is a test. In the real app there will be image folders and each item will have its corresponding image, of course.

Well, the page is created ,sent and shown correctly but the image is not shown. I believe this is an HTML problem, at which I'm not an expert, because if I change the src tag for anything online (e.g. the link to a picture on an Internet Server using http) the page created is able to fetch and exhibit the image properly.

Unfortunately I must be able to solve this problem without using any resource beyond the LAN to which the server and client browsers will be connected.

Thanks for any help.

CMorgado
  • 73
  • 1
  • 6
  • 1
    The HTTP request to fetch the image will come to your server the same way the request for this page comes. You have to, in your server code, figure out by looking at the request url that the caller is requesting an image, load the image, and return it. Take a look at http.ServeMux, or github.com/gorilla/mux for something more elaborate. Once you figure out the request, you can use `http.ServeFile` to send the image file. – Burak Serdar Jan 20 '20 at 05:36
  • 1
    Adding to what Burak said, you can also let the [http.FileServer](https://stackoverflow.com/a/28798174) serve a directory that contains your images (maybe also other assets such as css) – xarantolus Jan 20 '20 at 07:53
  • Thank you for your help. I've benn trying this approach for a while. But being new to go my problem now is how to get info from the client's call to take the appropriate action. I tried to find the answer by myself on line but was not able. Can anybody sho me the way? Thanks. – CMorgado Jan 21 '20 at 13:42

1 Answers1

0

You can add this piece of code in your server code to serve your images.

fs := http.FileServer(http.Dir("/path/to/images"))
http.Handle("/", fs)