filesystem

package
v0.0.0-...-326acc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2022 License: MIT Imports: 10 Imported by: 0

README

Filesystem Middleware

Filesystem middleware for Fiber that enables you to serve files from a directory.

⚠��? :params & :optionals? within the prefix path are not supported!

Table of Contents

Signatures

func New(config Config) fiber.Handler

Examples

First import the middleware from Fiber,

import (
  "github.com/yonzay/tlsfiber"
  "github.com/yonzay/tlsfiber/middleware/filesystem"
)

Then create a Fiber app with app := fiber.New().

Config
// Provide a minimal config
app.Use(filesystem.New(filesystem.Config{
	Root: http.Dir("./assets")
}))

// Or extend your config for customization
app.Use(filesystem.New(filesystem.Config{
	Root:         http.Dir("./assets"),
	Browse:       true,
	Index:        "index.html",
	NotFoundFile: "404.html",
	MaxAge:       3600,
}))
pkger

Pkger can be used to embed files in a Golang excecutable.

package main

import (
	"github.com/yonzay/tlsfiber"
	"github.com/yonzay/tlsfiber/middleware/filesystem"

	"github.com/markbates/pkger"
)

func main() {
	app := fiber.New()

	app.Use("/assets", filesystem.New(filesystem.Config{
		Root: pkger.Dir("/assets"),
	})

	log.Fatal(app.Listen(":3000"))
}
packr

Packr can be used to embed files in a Golang excecutable.

package main

import (
	"github.com/yonzay/tlsfiber"
	"github.com/yonzay/tlsfiber/middleware/filesystem"

	"github.com/gobuffalo/packr/v2"
)

func main() {
	app := fiber.New()

	app.Use("/assets", filesystem.New(filesystem.Config{
		Root: packr.New("Assets Box", "/assets"),
	})

	log.Fatal(app.Listen(":3000"))
}
go.rice

https://github.com/GeertJohan/go.rice

package main

import (
	"github.com/yonzay/tlsfiber"
	"github.com/yonzay/tlsfiber/middleware/filesystem"

	"github.com/GeertJohan/go.rice"
)

func main() {
	app := fiber.New()

	app.Use("/assets", filesystem.New(filesystem.Config{
		Root: rice.MustFindBox("assets").HTTPBox(),
	})

	log.Fatal(app.Listen(":3000"))
}
fileb0x

Fileb0x can be used to embed files in a Golang excecutable.

package main

import (
	"github.com/yonzay/tlsfiber"
	"github.com/yonzay/tlsfiber/middleware/filesystem"

	"<Your go module>/myEmbeddedFiles"
)

func main() {
	app := fiber.New()

	app.Use("/assets", filesystem.New(filesystem.Config{
		Root: myEmbeddedFiles.HTTP,
	})

	log.Fatal(app.Listen(":3000"))
}
statik

Statik can be used to embed files in a Golang excecutable.

package main

import (
	"github.com/yonzay/tlsfiber"
	"github.com/yonzay/tlsfiber/middleware/filesystem"

	"<Your go module>/statik"
	fs "github.com/rakyll/statik/fs"
)

func main() {
	statik, err := fs.New()
	if err != nil {
		panic(err)
	}

	app := fiber.New()

	app.Use("/", filesystem.New(filesystem.Config{
		Root: statikFS,
	})

	log.Fatal(app.Listen(":3000"))
}

Config

// Config defines the config for middleware.
type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Root is a FileSystem that provides access
	// to a collection of files and directories.
	//
	// Required. Default: nil
	Root http.FileSystem `json:"-"`

	// Enable directory browsing.
	//
	// Optional. Default: false
	Browse bool `json:"browse"`

	// Index file for serving a directory.
	//
	// Optional. Default: "index.html"
	Index string `json:"index"`

	// The value for the Cache-Control HTTP-header
	// that is set on the file response. MaxAge is defined in seconds.
	//
	// Optional. Default value 0.
	MaxAge    int `json:"max_age"`

	// File to return if path is not found. Useful for SPA's.
	//
	// Optional. Default: ""
	NotFoundFile string `json:"not_found_file"`
}
Default Config
var ConfigDefault = Config{
	Next:   nil,
	Root:   nil,
	Browse: false,
	Index:  "/index.html",
	MaxAge: 0,
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Next:   nil,
	Root:   nil,
	Browse: false,
	Index:  "/index.html",
	MaxAge: 0,
}

ConfigDefault is the default config

Functions

func New

func New(config ...Config) fiber.Handler

New creates a new middleware handler

func SendFile

func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) (err error)

SendFile ...

Types

type Config

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Root is a FileSystem that provides access
	// to a collection of files and directories.
	//
	// Required. Default: nil
	Root http.FileSystem `json:"-"`

	// Enable directory browsing.
	//
	// Optional. Default: false
	Browse bool `json:"browse"`

	// Index file for serving a directory.
	//
	// Optional. Default: "index.html"
	Index string `json:"index"`

	// The value for the Cache-Control HTTP-header
	// that is set on the file response. MaxAge is defined in seconds.
	//
	// Optional. Default value 0.
	MaxAge int `json:"max_age"`

	// File to return if path is not found. Useful for SPA's.
	//
	// Optional. Default: ""
	NotFoundFile string `json:"not_found_file"`
}

Config defines the config for middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL