Serving static files

  1. func main() {
  2. app := iris.New()
  3. app.Favicon("./resources/favicon.ico")
  4. app.HandleDir("/assets", iris.Dir("./assets"))
  5. app.Listen(":8080")
  6. }

The HandleDir method accepts a third, optional argument of DirOptions:

  1. type DirOptions struct {
  2. // Defaults to "/index.html", if request path is ending with **/*/$IndexName
  3. // then it redirects to **/*(/) which another handler is handling it,
  4. // that another handler, called index handler, is auto-registered by the framework
  5. // if end developer does not managed to handle it by hand.
  6. IndexName string
  7. // PushTargets filenames (map's value) to
  8. // be served without additional client's requests (HTTP/2 Push)
  9. // when a specific request path (map's key WITHOUT prefix)
  10. // is requested and it's not a directory (it's an `IndexFile`).
  11. //
  12. // Example:
  13. // "/": {
  14. // "favicon.ico",
  15. // "js/main.js",
  16. // "css/main.css",
  17. // }
  18. PushTargets map[string][]string
  19. // PushTargetsRegexp like `PushTargets` but accepts regexp which
  20. // is compared against all files under a directory (recursively).
  21. // The `IndexName` should be set.
  22. //
  23. // Example:
  24. // "/": regexp.MustCompile("((.*).js|(.*).css|(.*).ico)$")
  25. // See `iris.MatchCommonAssets` too.
  26. PushTargetsRegexp map[string]*regexp.Regexp
  27. // Cache to enable in-memory cache and pre-compress files.
  28. Cache DirCacheOptions
  29. // When files should served under compression.
  30. Compress bool
  31. // List the files inside the current requested directory if `IndexName` not found.
  32. ShowList bool
  33. // If `ShowList` is true then this function will be used instead
  34. // of the default one to show the list of files of a current requested directory(dir).
  35. // See `DirListRich` package-level function too.
  36. DirList DirListFunc
  37. // Files downloaded and saved locally.
  38. Attachments Attachments
  39. // Optional validator that loops through each requested resource.
  40. AssetValidator func(ctx *context.Context, name string) bool
  41. }

Learn more about file-server.