Static File Service Configuration

By default, the GoFrame Server disables the static file service feature. If the developer configures a static file directory, the static file service will be automatically enabled.

Common configuration methods involving the static file service are as follows:

  1. // Set HTTP server parameter - ServerRoot
  2. func (s *Server) SetServerRoot(root string)
  3. // Add static file search directories; the absolute path of the directory must be specified
  4. func (s *Server) AddSearchPath(path string)
  5. // Set HTTP server parameter - IndexFiles, default displayed files, such as: index.html, index.htm
  6. func (s *Server) SetIndexFiles(index []string)
  7. // Allow or disallow displaying the directory listing of accessible directories
  8. func (s *Server) SetIndexFolder(enabled bool)
  9. // Add mapping of URI to static directory
  10. func (s *Server) AddStaticPath(prefix string, path string)
  11. // Main switch for static file service: enable/disable static file service
  12. func (s *Server) SetFileServerEnabled(enabled bool)
  13. // Set URI rewrite rule
  14. func (s *Server) SetRewrite(uri string, rewrite string)
  15. // Set URI rewrite rules (batch)
  16. func (s *Server) SetRewriteMap(rewrites map[string]string)

Brief introduction:

  1. IndexFiles is the list of default file names searched when accessing directories (searched in the order of the slice). If the searched file exists, the file content is returned; otherwise, the directory listing is displayed (when SetIndexFolder is true). The default IndexFiles are: index.html, index.htm.
  2. SetIndexFolder is set to allow displaying the file list in the directory when users access a file directory and no IndexFiles are found in the directory. It is disabled by default.
  3. SetServerRoot sets the default static file directory for services. This directory is automatically added as the first search path in SearchPath.
  4. AddSearchPath adds static file search directories, which can be multiple, and priority searches are performed in the order of added file directories.
  5. AddStaticPath adds mapping relationships between URI and directory paths, allowing customization of static file directory access URI rules.
  6. SetRewrite/SetRewriteMap sets rewrite rules (similar to nginx rewrite), which technically apply not only to static file services but also support dynamic route registration rewrite.

Static File Service - 图1tip

When setting the directory path for the static file service, you can use either absolute or relative paths. For example, to set the current running directory to provide static file services, use SetServerRoot(".").

Developers can set multiple file directories to provide static file services and prioritize directories and URIs. However, if the static service is disabled via SetFileServerEnabled, all static file/directory access will become invalid.

Example 1, Basic Usage

  1. package main
  2. import "github.com/gogf/gf/v2/frame/g"
  3. // Basic usage of static file server
  4. func main() {
  5. s := g.Server()
  6. s.SetIndexFolder(true)
  7. s.SetServerRoot("/Users/john/Temp")
  8. s.AddSearchPath("/Users/john/Documents")
  9. s.SetPort(8199)
  10. s.Run()
  11. }

Example 2, Static Directory Mapping

  1. package main
  2. import "github.com/gogf/gf/v2/frame/g"
  3. // Static file server supports custom static directory mapping
  4. func main() {
  5. s := g.Server()
  6. s.SetIndexFolder(true)
  7. s.SetServerRoot("/Users/john/Temp")
  8. s.AddSearchPath("/Users/john/Documents")
  9. s.AddStaticPath("/my-doc", "/Users/john/Documents")
  10. s.SetPort(8199)
  11. s.Run()
  12. }

Example 3, Static Directory Mapping, Priority Control

The priority of static directory mapping is controlled according to the precision of the bound URI. The more precise the bound URI (depth-first matching), the higher the priority.

  1. package main
  2. import "github.com/gogf/gf/v2/frame/g"
  3. // Static file server supports custom static directory mapping
  4. func main() {
  5. s := g.Server()
  6. s.SetIndexFolder(true)
  7. s.SetServerRoot("/Users/john/Temp")
  8. s.AddSearchPath("/Users/john/Documents")
  9. s.AddStaticPath("/my-doc", "/Users/john/Documents")
  10. s.AddStaticPath("/my-doc/test", "/Users/john/Temp")
  11. s.SetPort(8199)
  12. s.Run()
  13. }

Here, accessing /my-doc/test has a higher priority than /my-doc. Therefore, if there is a test directory under /Users/john/Documents (conflicting with the custom /my-doc/test), it cannot be accessed.

Example 4, URI Rewrite

The static file service of the GoFrame framework supports rewriting any URI to replace it with a designated URI, using SetRewrite/SetRewriteMap methods.

Example: In the /Users/john/Temp directory, there are only two files test1.html and test2.html.

  1. package main
  2. import "github.com/gogf/gf/v2/frame/g"
  3. func main() {
  4. s := g.Server()
  5. s.SetServerRoot("/Users/john/Temp")
  6. s.SetRewrite("/test.html", "/test1.html")
  7. s.SetRewriteMap(g.MapStrStr{
  8. "/my-test1": "/test1.html",
  9. "/my-test2": "/test2.html",
  10. })
  11. s.SetPort(8199)
  12. s.Run()
  13. }

After execution,

  1. When accessing /test.html, it is eventually rewritten to test1.html, and the content of this file is returned.
  2. When accessing /my-test1, it is eventually rewritten to test1.html, and the content of this file is returned.
  3. When accessing /my-test2, it is eventually rewritten to test2.html, and the content of this file is returned.

Example 5, Cross-Origin

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "github.com/gogf/gf/v2/os/glog"
  6. )
  7. func beforeServeHook(r *ghttp.Request) {
  8. glog.Debugf(r.GetCtx(), "beforeServeHook [is file:%v] URI:%s", r.IsFileRequest(), r.RequestURI)
  9. r.Response.CORSDefault()
  10. }
  11. // Use hooks to inject cross-origin configuration
  12. func main() {
  13. s := g.Server()
  14. s.BindHookHandler("/*", ghttp.HookBeforeServe, beforeServeHook)
  15. s.SetServerRoot(".")
  16. s.SetFileServerEnabled(true)
  17. s.SetAddr(":8080")
  18. s.Run()
  19. }