I'm trying to adapt the go HelloServer example to serve up some images
constructed by a go program writing svg in the local directory. When I
point a browser to it I get the 'broken-image' icon for the "local"
image but I can pull the same image from apache running on another
host. At this point I'm not sure that what I want to do can be done
since the "URL"
127.0.0.1/hello doesn't refer to anything in the
localhost directory tree. I know I can solve this by running
another go server instance to serve just the image directory at a
different port but that seems more cumbersome than necessary. And it
didn't work when given the full path (with search/read perms for all)
as opposed to the relative path in the code below. Any hints on better
ways would be most welcome. Thanks in advance.
Minimal code to reproduce problem (with weekly 2-22) follows:
// ====================
// zero.svg is in same directory as executable
// browse to
127.0.0.1/hello shows page text, but not image
package main
import (
"fmt"
"log"
"net/http"
)
const portNum = 12349
var portNumString = fmt.Sprintf(":%d",portNum)
func createSvg(name string) {
//..... svg writing code deleted for brevity .....
}
func HelloServer(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "<html>")
fmt.Fprintf(w, "<head>")
fmt.Fprintf(w, "<title>Test Title</title>")
fmt.Fprintf(w, "</head>")
fmt.Fprintf(w, "<body>")
fmt.Fprintf(w, "IMAGE --> <img src =\"zero.svg\">") // <-- gives
'broken-image' icon in Chrome
fmt.Fprintf(w, "IMAGE --> <img src =\"
http://10.1.2.106/gallery/
zero.svg\">") // <-- works fine
fmt.Fprintf(w, "</body>")
fmt.Fprintf(w, "</html>")
}
func main(){
createSvg("zero.svg")
fmt.Printf("Starting server at %s\n",portNumString)
http.HandleFunc("/hello", HelloServer)
err := http.ListenAndServe(portNumString, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}