Giter Club home page Giter Club logo

elton-static-serve's Introduction

elton-static-serve

The middleware has been archived, please use the middleware of elton.

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)
	}
}

elton-static-serve's People

Contributors

vicanso avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

elton-static-serve's Issues

Invalid when you use packr2 and run it under windows

Issue description

Invaid path when you use packr2 as static content and run it under windows

Steps to reproduce the issue

pack your resources packr2 and use it in elton-static-serve with config.Path = /static
a single resource path looks like
/css/main.74968888.chunk.css

after calling under windows

file = filepath.Join(config.Path, file)

resource will become :

\static\css\main.74968888.chunk.css

and the flowing check always fails

if !strings.HasPrefix(file, config.Path) {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.