Skip to content

Instantly share code, notes, and snippets.

@ichtrojan
Created December 7, 2019 13:36
Show Gist options
  • Save ichtrojan/45c0ecf569af1a84111e846d62cc4552 to your computer and use it in GitHub Desktop.
Save ichtrojan/45c0ecf569af1a84111e846d62cc4552 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
)
func main () {
route := mux.NewRouter()
http.FileServer(http.Dir("/public"))
route.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
view, err := template.ParseFiles("views/errors/404.html")
if err != nil {
log.Fatal(err)
}
_ = view.Execute(writer, nil)
})
err := http.ListenAndServe(":9000", route)
if err != nil {
log.Fatal(err)
}
}
@adelowo
Copy link

adelowo commented Dec 9, 2019

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func main() {
	route := mux.NewRouter()

	route.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("./public"))))

	err := http.ListenAndServe(":9000", route)
	if err != nil {
		log.Fatal(err)
	}
}

This works. You need to have a public directory directly at the same level in the filesystem as the binary you are running. So if a file is in ./public/css/index.css, it will be accessed by <a href="/assets/css/index.css">

You can change the paths obviously

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment