Home > Backend Development > Golang > How to Serve a Homepage and Static Files from the Root Directory in Go?

How to Serve a Homepage and Static Files from the Root Directory in Go?

Susan Sarandon
Release: 2024-12-26 16:03:11
Original
665 people have browsed it

How to Serve a Homepage and Static Files from the Root Directory in Go?

Serving Homepage and Static Content from Root

In Go, serving static content from the root directory while maintaining a root handler for the homepage can be achieved using the following steps:

Handle Root Directory Files Explicitly

Create a function, such as serveSingle, to serve individual files located in the root directory. This approach is suitable for files like sitemap.xml, favicon.ico, and robots.txt that are typically expected to be present in the root:

func serveSingle(pattern string, filename string) {
    http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, filename)
    })
}
Copy after login

Register File Handlers

Register the serveSingle function to handle requests for specific files in the root directory:

serveSingle("/sitemap.xml", "./sitemap.xml")
serveSingle("/favicon.ico", "./favicon.ico")
serveSingle("/robots.txt", "./robots.txt")
Copy after login

Serve Static Content from a Subdirectory

Use http.FileServer to serve static content from a subdirectory, such as "/static/":

http.Handle("/static", http.FileServer(http.Dir("./static/")))
Copy after login

Register Homepage Handler

Register the root handler, such as HomeHandler, to handle requests for the homepage at "/":

http.HandleFunc("/", HomeHandler)
Copy after login

Example Code

Combining these techniques results in the following code:

package main

import (
    "fmt"
    "net/http"
)

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "HomeHandler")
}

func serveSingle(pattern string, filename string) {
    http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, filename)
    })
}

func main() {
    http.HandleFunc("/", HomeHandler) // homepage

    serveSingle("/sitemap.xml", "./sitemap.xml")
    serveSingle("/favicon.ico", "./favicon.ico")
    serveSingle("/robots.txt", "./robots.txt")

    http.Handle("/static", http.FileServer(http.Dir("./static/")))

    http.ListenAndServe(":8080", nil)
}
Copy after login

By explicitly handling root directory files while serving static content from a separate subdirectory, you can maintain both homepage handling and static content serving with a behavior similar to web servers like Apache and Nginx.

The above is the detailed content of How to Serve a Homepage and Static Files from the Root Directory in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template