NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

About
LoggingHealth ChecksProfilingMulti-Server SupportStatic FilesRate LimitingPanic RecoveryCustom 404 HandlerCustom 404 PageError Handling
RouterFeatures

Static Files

Serve CSS, JavaScript, images, and favicon files from a local directory using a URL prefix.

The router package can serve static assets from a local directory.

This is useful for:

  • CSS files
  • JavaScript files
  • images
  • icons
  • fonts
  • public frontend assets

Static files are mapped from a real filesystem path to a public URL prefix.

For example:

r.Static("/assets/", "./public")

This means:

./public/style.css       → /assets/style.css
./public/favicon.ico     → /assets/favicon.ico
./public/images/logo.png → /assets/images/logo.png

The local directory name is not exposed in the URL. Only the files inside the directory are served under the configured prefix.


Directory Structure

Example project structure:

static_files/
├── public/
│   ├── favicon.ico
│   └── style.css
└── main.go

In this example, the real static directory is:

./public

and the public URL prefix is:

/assets/

Example

package main

import (
	"log/slog"
	"net/http"
	"os"

	"github.com/netlifeguru/router"
)

func main() {
	r := router.New()

	r.Static("/assets/", "./public")

	r.GET("/", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
		w.Header().Set("Content-Type", "text/html")
		w.Write([]byte(`
			<html>
				<head>
					<link rel="stylesheet" href="/assets/style.css">
				</head>
				<body>
					<h1>Static Files Example</h1>
					<p>Static assets are served from the ./public directory.</p>
				</body>
			</html>
		`))
	})

	if err := r.ListenAndServe(":4000"); err != nil {
		slog.Error("failed to start server", "error", err)
		os.Exit(1)
	}
}

Run the application:

go run .

Open the page:

http://localhost:4000

The CSS file is served from:

http://localhost:4000/assets/style.css

Favicon Support

Browsers commonly request favicon.ico from the root path:

/favicon.ico

When favicon.ico exists inside the static directory, the router automatically registers an additional root-level favicon route.

For example:

./public/favicon.ico

is available at both:

/assets/favicon.ico
/favicon.ico

This prevents unnecessary 404 responses when browsers request the favicon from the root of the domain.


Notes

Static file serving uses Go’s standard http.FileServer internally.

The configured URL prefix does not have to match the local directory name:

r.Static("/assets/", "./public")

In this case, files are loaded from ./public, but exposed publicly under /assets/.

Multi-Server Support

Run multiple HTTP listeners simultaneously using a single router instance for internal APIs, public endpoints, admin panels, or multi-port deployments.

Rate Limiting

Protect routes from excessive traffic using IP and route-based request throttling middleware.

On this page

Directory StructureExampleFavicon SupportNotes