embed

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2020 License: MIT Imports: 9 Imported by: 1

README

Embed

FileServer middleware for Fiber

Special thanks and credits to Alireza Salary

Install

go get -u github.com/gofiber/fiber
go get -u github.com/gofiber/embed

Signature

embed.New(config ...embed.Config) func(c *fiber.Ctx)

Config

Property Type Description Default
Index string Index file name index.html
Browse bool Enable directory browsing false
Root http.FileSystem http.FileSystem to use nil
ErrorHandler func(*fiber.Ctx, error) Error handler InternalServerError

pkger

package main

import (
  "github.com/gofiber/fiber"
  "github.com/gofiber/embed"

  "github.com/markbates/pkger"
)

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

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

  app.Listen(8080)
}

packr

package main

import (
  "github.com/gofiber/fiber"
  "github.com/gofiber/embed"

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

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

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

  app.Listen(8080)
}

go.rice

package main

import (
  "github.com/gofiber/fiber"
  "github.com/gofiber/embed"

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

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

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

  app.Listen(8080)
}

fileb0x

package main

import (
  "github.com/gofiber/fiber"
  "github.com/gofiber/embed"
  "<Your go module>/myEmbeddedFiles"
)

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

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

  app.Listen(8080)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config ...Config) func(*fiber.Ctx)

New returns an embed middleware for serving files

Types

type Config

type Config struct {
	// Root is a FileSystem that provides access
	// to a collection of files and directories.
	// Required. Default: nil
	Root http.FileSystem

	// ErrorHandler defines the response body when an error raised.
	// Optional. Defaul: Next for NotExistError and 403 for PermissionError and 500 for others
	ErrorHandler func(*fiber.Ctx, error)

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

	// Enable directory browsing.
	// Optional. Default: false
	Browse bool
}

Config holds the configuration for the middleware

Jump to

Keyboard shortcuts

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