Documentation › Traditional web application › Serving static files
Static files for your web application should be stored inside the assets/static directory. By default, this directory contains the sub-directories css, img and js, which are intended to store CSS files, images and JavaScript files respectively.
You'll see that there is an assets/static/css/main.css file already present, which contains some basic CSS styles for the application. Feel free to edit this as you wish.
When you build or run the application, the files in assets/static directory will be embedded into the application binary. Specifically, they will be available via the EmbeddedFiles variable in the assets/efs.go file.
By default, these embedded files are served using Go's http.Fileserver whenever the application receives a GET request with a path beginning /static/.
So – for example – if your application is running on localhost:6633:
localhost:6633/static/css/main.css will serve the file assets/static/css/main.css.localhost:6633/static/media/audio/click.mp3 will serve the file assets/static/media/audio/click.mp3.localhost:6633/static/favicon.ico will serve the file assets/static/favicon.ico.Feel free to rename, remove or add new folders within assets/static as necessary to organize your files in whatever way you want.
Because static files are served using the Go http.FileServer it's worth mentioning that we get some nice features for free, including:
. and .. elements are removed from the URL path, which helps to stop directory traversal attacks.Content-Type response header will be automatically set based on the file extension.Last-Modified and If-Modified-Since headers are transparently supported. If the file hasn't changed since the client's last request, then the client will be sent a 304 Not Modified status code instead of the file itself.