R
R
Roman Rakzin2015-07-15 01:44:21
go
Roman Rakzin, 2015-07-15 01:44:21

File server in golang?

Used this code for static files
func main() {
http.Handle("/", http.FileServer(http.Dir("./files"))) http.ListenAndServe
(":5000", nil)
}
expand the application. Added routing
package main
import (
"net/http"
"github.com/gorilla/mux"
"fmt"
)
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
router.HandleFunc("/files/", FileServer)
http.ListenAndServe(":5000",
fmt.Fprintln(w, "Hi. This is the front page")
}
func FileServer(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("./files"))
}
---- ----------
But static files are not loaded. It is necessary that according to the link site/files/css/my.css the corresponding file is loaded.
How can I make routing and what would the file server be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Rakzin, 2015-07-15
@TwoRS

router := mux.NewRouter()
s := http.StripPrefix("/files/", http.FileServer(http.Dir("./files/")))
router.HandleFunc("/", Index)
router. PathPrefix("/files/").Handler(s)
http.Handle("/", router)
http.ListenAndServe(":5000", nil)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question