Search code examples
restgohttpserverfileserver

Go file server doesn't serve folder


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
    /sections
main.go
go.mod
go.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 work. I tried to serve the same folder with Python and it did work. I tried different ports and it seems only the ones that 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.


Solution

  • After trying different solutions, I found the problem. I was serving my web pages through a mux object while serving files on an http file server. Now I am serving web pages through http.HandleFunc(). The problem is fixed and the website is working properly. Here is the new main() function:

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