staticserve

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2020 License: Apache-2.0 Imports: 13 Imported by: 1

README

elton-static-serve

Build Status

Static serve for elton, it use to serve static file, such as html, image and etc.

package main

import (
	"bytes"
	"io"
	"os"

	packr "github.com/gobuffalo/packr/v2"
	"github.com/vicanso/elton"

	staticServe "github.com/vicanso/elton-static-serve"
)

var (
	box = packr.New("asset", "./")
)

type (
	staticFile struct {
		box *packr.Box
	}
)

func (sf *staticFile) Exists(file string) bool {
	return sf.box.Has(file)
}
func (sf *staticFile) Get(file string) ([]byte, error) {
	return sf.box.Find(file)
}
func (sf *staticFile) Stat(file string) os.FileInfo {
	return nil
}
func (sf *staticFile) NewReader(file string) (io.Reader, error) {
	buf, err := sf.Get(file)
	if err != nil {
		return nil, err
	}
	return bytes.NewReader(buf), nil
}

func main() {
	e := elton.New()

	sf := &staticFile{
		box: box,
	}

	// static file route
	e.GET("/static/*file", staticServe.New(sf, staticServe.Config{
		// 客户端缓存一年
		MaxAge: 365 * 24 * 3600,
		// 缓存服务器缓存一个小时
		SMaxAge:             60 * 60,
		DenyQueryString:     true,
		DisableLastModified: true,
	}))

	err := e.ListenAndServe(":7001")
	if err != nil {
		panic(err)
	}
}
package main

import (
	"github.com/vicanso/elton"

	staticServe "github.com/vicanso/elton-static-serve"
)

func main() {
	e := elton.New()

	sf := new(staticServe.FS)
	// static file route
	e.GET("/*file", staticServe.New(sf, staticServe.Config{
		Path: "/tmp",
		// 客户端缓存一年
		MaxAge: 365 * 24 * 3600,
		// 缓存服务器缓存一个小时
		SMaxAge:             60 * 60,
		DenyQueryString:     true,
		DisableLastModified: true,
		// packr不支持Stat,因此需要用强ETag 
		EnableStrongETag: true,
	}))

	err := e.ListenAndServe(":3000")
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

View Source
const (
	// ErrCategory static serve error category
	ErrCategory = "elton-static-serve"
)

Variables

View Source
var (
	// ErrNotAllowQueryString not all query string
	ErrNotAllowQueryString = getStaticServeError("static serve not allow query string", http.StatusBadRequest)
	// ErrNotFound static file not found
	ErrNotFound = getStaticServeError("static file not found", http.StatusNotFound)
	// ErrOutOfPath file out of path
	ErrOutOfPath = getStaticServeError("out of path", http.StatusBadRequest)
	// ErrNotAllowAccessDot file include dot
	ErrNotAllowAccessDot = getStaticServeError("static server not allow with dot", http.StatusBadRequest)
)

Functions

func New

func New(staticFile StaticFile, config Config) elton.Handler

New create a static serve middleware

func NewDefault

func NewDefault(config Config) elton.Handler

NewDefault create a static server milldeware use FS

Types

type Config

type Config struct {
	// 静态文件目录
	Path string
	// http cache control max age
	MaxAge int
	// http cache control s-maxage
	SMaxAge int
	// http response header
	Header map[string]string
	// 禁止query string(因为有时静态文件为CDN回源,避免生成各种重复的缓存)
	DenyQueryString bool
	// 是否禁止文件路径以.开头(因为这些文件有可能包括重要信息)
	DenyDot bool
	// 是否使用strong etag
	EnableStrongETag bool
	// 禁止生成ETag
	DisableETag bool
	// 禁止生成 last-modifed
	DisableLastModified bool
	// 如果404,是否调用next执行后续的中间件(默认为不执行,返回404错误)
	NotFoundNext bool
	Skipper      elton.Skipper
}

Config static serve config

type FS

type FS struct {
}

FS file system

func (*FS) Exists

func (fs *FS) Exists(file string) bool

Exists check the file exists

func (*FS) Get

func (fs *FS) Get(file string) (buf []byte, err error)

Get get the file's content

func (*FS) NewReader

func (fs *FS) NewReader(file string) (io.Reader, error)

NewReader new a reader for file

func (*FS) Stat

func (fs *FS) Stat(file string) os.FileInfo

Stat get stat of file

type StaticFile

type StaticFile interface {
	Exists(string) bool
	Get(string) ([]byte, error)
	Stat(string) os.FileInfo
	NewReader(string) (io.Reader, error)
}

StaticFile static file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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