github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/filesystem.md (about) 1 --- 2 id: filesystem 3 title: FileSystem 4 --- 5 6 Filesystem middleware for [Fiber](https://github.com/gofiber/fiber) that enables you to serve files from a directory. 7 8 :::caution 9 **`:params` & `:optionals?` within the prefix path are not supported!** 10 11 **To handle paths with spaces (or other url encoded values) make sure to set `fiber.Config{ UnescapePath: true }`** 12 ::: 13 14 ## Signatures 15 16 ```go 17 func New(config Config) fiber.Handler 18 ``` 19 20 ## Examples 21 22 Import the middleware package that is part of the Fiber web framework 23 24 ```go 25 import ( 26 "github.com/gofiber/fiber/v2" 27 "github.com/gofiber/fiber/v2/middleware/filesystem" 28 ) 29 ``` 30 31 After you initiate your Fiber app, you can use the following possibilities: 32 33 ```go 34 // Provide a minimal config 35 app.Use(filesystem.New(filesystem.Config{ 36 Root: http.Dir("./assets"), 37 })) 38 39 // Or extend your config for customization 40 app.Use(filesystem.New(filesystem.Config{ 41 Root: http.Dir("./assets"), 42 Browse: true, 43 Index: "index.html", 44 NotFoundFile: "404.html", 45 MaxAge: 3600, 46 })) 47 ``` 48 49 50 > If your environment (Go 1.16+) supports it, we recommend using Go Embed instead of the other solutions listed as this one is native to Go and the easiest to use. 51 52 ## embed 53 54 [Embed](https://golang.org/pkg/embed/) is the native method to embed files in a Golang excecutable. Introduced in Go 1.16. 55 56 ```go 57 package main 58 59 import ( 60 "embed" 61 "io/fs" 62 "log" 63 "net/http" 64 65 "github.com/gofiber/fiber/v2" 66 "github.com/gofiber/fiber/v2/middleware/filesystem" 67 ) 68 69 // Embed a single file 70 //go:embed index.html 71 var f embed.FS 72 73 // Embed a directory 74 //go:embed static/* 75 var embedDirStatic embed.FS 76 77 func main() { 78 app := fiber.New() 79 80 app.Use("/", filesystem.New(filesystem.Config{ 81 Root: http.FS(f), 82 })) 83 84 // Access file "image.png" under `static/` directory via URL: `http://<server>/static/image.png`. 85 // Without `PathPrefix`, you have to access it via URL: 86 // `http://<server>/static/static/image.png`. 87 app.Use("/static", filesystem.New(filesystem.Config{ 88 Root: http.FS(embedDirStatic), 89 PathPrefix: "static", 90 Browse: true, 91 })) 92 93 log.Fatal(app.Listen(":3000")) 94 } 95 ``` 96 97 ## pkger 98 99 [https://github.com/markbates/pkger](https://github.com/markbates/pkger) 100 101 ```go 102 package main 103 104 import ( 105 "github.com/gofiber/fiber/v2" 106 "github.com/gofiber/fiber/v2/middleware/filesystem" 107 108 "github.com/markbates/pkger" 109 ) 110 111 func main() { 112 app := fiber.New() 113 114 app.Use("/assets", filesystem.New(filesystem.Config{ 115 Root: pkger.Dir("/assets"), 116 })) 117 118 log.Fatal(app.Listen(":3000")) 119 } 120 ``` 121 122 ## packr 123 124 [https://github.com/gobuffalo/packr](https://github.com/gobuffalo/packr) 125 126 ```go 127 package main 128 129 import ( 130 "github.com/gofiber/fiber/v2" 131 "github.com/gofiber/fiber/v2/middleware/filesystem" 132 133 "github.com/gobuffalo/packr/v2" 134 ) 135 136 func main() { 137 app := fiber.New() 138 139 app.Use("/assets", filesystem.New(filesystem.Config{ 140 Root: packr.New("Assets Box", "/assets"), 141 })) 142 143 log.Fatal(app.Listen(":3000")) 144 } 145 ``` 146 147 ## go.rice 148 149 [https://github.com/GeertJohan/go.rice](https://github.com/GeertJohan/go.rice) 150 151 ```go 152 package main 153 154 import ( 155 "github.com/gofiber/fiber/v2" 156 "github.com/gofiber/fiber/v2/middleware/filesystem" 157 158 "github.com/GeertJohan/go.rice" 159 ) 160 161 func main() { 162 app := fiber.New() 163 164 app.Use("/assets", filesystem.New(filesystem.Config{ 165 Root: rice.MustFindBox("assets").HTTPBox(), 166 })) 167 168 log.Fatal(app.Listen(":3000")) 169 } 170 ``` 171 172 ## fileb0x 173 174 [https://github.com/UnnoTed/fileb0x](https://github.com/UnnoTed/fileb0x) 175 176 ```go 177 package main 178 179 import ( 180 "github.com/gofiber/fiber/v2" 181 "github.com/gofiber/fiber/v2/middleware/filesystem" 182 183 "<Your go module>/myEmbeddedFiles" 184 ) 185 186 func main() { 187 app := fiber.New() 188 189 app.Use("/assets", filesystem.New(filesystem.Config{ 190 Root: myEmbeddedFiles.HTTP, 191 })) 192 193 log.Fatal(app.Listen(":3000")) 194 } 195 ``` 196 197 ## statik 198 199 [https://github.com/rakyll/statik](https://github.com/rakyll/statik) 200 201 ```go 202 package main 203 204 import ( 205 "github.com/gofiber/fiber/v2" 206 "github.com/gofiber/fiber/v2/middleware/filesystem" 207 208 // Use blank to invoke init function and register data to statik 209 _ "<Your go module>/statik" 210 "github.com/rakyll/statik/fs" 211 ) 212 213 func main() { 214 statikFS, err := fs.New() 215 if err != nil { 216 panic(err) 217 } 218 219 app := fiber.New() 220 221 app.Use("/", filesystem.New(filesystem.Config{ 222 Root: statikFS, 223 })) 224 225 log.Fatal(app.Listen(":3000")) 226 } 227 ``` 228 229 ## Config 230 231 ```go 232 // Config defines the config for middleware. 233 type Config struct { 234 // Next defines a function to skip this middleware when returned true. 235 // 236 // Optional. Default: nil 237 Next func(c *fiber.Ctx) bool 238 239 // Root is a FileSystem that provides access 240 // to a collection of files and directories. 241 // 242 // Required. Default: nil 243 Root http.FileSystem `json:"-"` 244 245 // PathPrefix defines a prefix to be added to a filepath when 246 // reading a file from the FileSystem. 247 // 248 // Use when using Go 1.16 embed.FS 249 // 250 // Optional. Default "" 251 PathPrefix string `json:"path_prefix"` 252 253 // Enable directory browsing. 254 // 255 // Optional. Default: false 256 Browse bool `json:"browse"` 257 258 // Index file for serving a directory. 259 // 260 // Optional. Default: "index.html" 261 Index string `json:"index"` 262 263 // The value for the Cache-Control HTTP-header 264 // that is set on the file response. MaxAge is defined in seconds. 265 // 266 // Optional. Default value 0. 267 MaxAge int `json:"max_age"` 268 269 // File to return if path is not found. Useful for SPA's. 270 // 271 // Optional. Default: "" 272 NotFoundFile string `json:"not_found_file"` 273 274 // The value for the Content-Type HTTP-header 275 // that is set on the file response 276 // 277 // Optional. Default: "" 278 ContentTypeCharset string `json:"content_type_charset"` 279 } 280 ``` 281 282 ## Default Config 283 284 ```go 285 var ConfigDefault = Config{ 286 Next: nil, 287 Root: nil, 288 PathPrefix: "", 289 Browse: false, 290 Index: "/index.html", 291 MaxAge: 0, 292 ContentTypeCharset: "", 293 } 294 ```