Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3643

Go file server doesn't serve folder

$
0
0

I am building a website with Golang, HTML, JS and CSS.Here is my folder structure:

/data    (Go files that contain data initialization)/handler    (Go files for HTTP handling)/model    (Go files that include model structs)/static    /css     /images     /js    /webfonts/template    /elements    /pages    /sectionsmain.gogo.modgo.sum

I initialize the data in data package by calling it from the main() function at the beginning. Here is my data.Initialize() function:

func Initialize() {    log.Println("Site data is initializing:")    serveStaticFiles()    log.Println("Static files are ready!")    parseTemplates()    log.Println("Templates are ready!")    initializeTestimonials()    initializeHomePageData()    log.Println("Home page data is ready!")    initializeAboutPageData()    log.Println("About page data is ready!")    initializeContactPageData()    log.Println("Contact page data is ready!")    initializeNotFoundPageData()    log.Println("404 page data is ready!")}

Here is my serveStaticFiles() function which is inside of data package:

func serveStaticFiles() {    fs := http.FileServer(http.Dir("static"))    http.Handle("/static/", http.StripPrefix("/static/", fs))}

I call data.Initialize() from the main() function as the first thing:

func main() {    data.Initialize()    mux := http.NewServeMux()    mux.HandleFunc("/", handler.HomeHandler)    mux.HandleFunc("/home", handler.HomeHandler)    mux.HandleFunc("/about", handler.AboutHandler)    mux.HandleFunc("/contact", handler.ContactHandler)    mux.HandleFunc("/404", handler.NotFoundHandler)    log.Println("Starting server on :8080")    http.ListenAndServe(":8080", mux)}

However, the files under /static directory don't appear on the browser, returning 404 code.

404

I have also tried different things with serveStaticFiles() function such as http.Dir("./static"). Nothing seems working. I tried to serve the same folder with Python and it did work. I tried different ports and it seems only the ones that are started from Golang aren't working. os.Getwd() returns the root folder of the project which is where main.go file is.

I appreciate if anyone has an idea. Thanks.


Viewing all articles
Browse latest Browse all 3643

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>