post image :date_long | 1 min Read

Go StripPrefix()

package main

import (
	"io"
	"net/http"
)

func main() {
	http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets"))))

	http.HandleFunc("/xyz", takeAdvantageOfStripPrefix)

	http.HandleFunc("/tesla", teslaFromFileSystem)
	http.ListenAndServe(":8080", nil)

}

func teslaFromFileSystem(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	io.WriteString(w, `<img src="tesla.jpg">`)
}

func takeAdvantageOfStripPrefix(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	io.WriteString(w, `<img src="/resources/tesla.jpg">`)
}



author image

Jan Toth

I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …

comments powered by Disqus