gowebcompress

package module
v0.0.0-...-8cdffc6 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 12 Imported by: 0

README

Build Status Coverage Status Donate

gowebcompress

Optimal GO web compression.

IN DEVELOPMENT:

  • Needs more testing

This repo provides the best web compression:

  • Dynamic & Static

They both provide Brotli & GZip with the best settings for each scenario.

Examples

http.ListenAndServe(":80", gowebcompress.Dynamic(http.DefaultRouter))

s := gowebcompress.NewStatic("./static")
// In Handler:
s.SendFile(r, w, r.URL.Path)

See Godoc for more options.

They're cross-compatible & will not share a file you haven't allowed into static or sent directly.

License: MIT. Use it anywhere.

I gladly accept your improvement patches.

Documentation

Overview

Package gowebcompress applies top compression hueristics to offer a dynamic (for APIs) and Static http middleware to accelerate your server.

Index

Constants

This section is empty.

Variables

View Source
var DynamicLevels = Levels{2, 2}
View Source
var StaticLevels = Levels{6, 4}

Functions

func FS

func FS(w io.Writer, opts CacheOpts, origPath string) (handled bool)

FS is a convenience function for informing the cacher that a static file is being served and the compressed contents can be cached there. It requires the Middleware to have ran & fs must support StatFS. Use in handler (for static/foo.txt):

if gowebcompress.FS(w, os.DirFS("/"), os.Create, "static/foo.txt") {
  return;  // Bytes sent from Disk Cache
}

serveFile("static/foo.txt")

func Handler

func Handler(w http.ResponseWriter, req *http.Request) (newWriter http.ResponseWriter, complete func())

Handler makes it easy for wrapping for custom routers (ex: Gin)

func Middleware

func Middleware(handler http.Handler) http.Handler

Middleware for compression supporting Brotoli & Gzip. Uses a static-compiled Brotoli library (no external dep). Have a compiler ready. Optimized for low CPU usage: 80kb/ms today, or change DynamicLevels / StaticLevels.

func OSCreate

func OSCreate(abspath string) (io.WriteCloser, error)

Types

type CacheInfo

type CacheInfo interface {
	GetErrors() []error
}

type CacheOpts

type CacheOpts struct {
	fs.FS
	CreateFile
	BasePath string
}

type CreateFile

type CreateFile func(path string) (io.WriteCloser, error)

type Levels

type Levels struct {
	Gzip   int
	Brotli int
}

type OSFSStat

type OSFSStat struct {
	fs.FS
	// contains filtered or unexported fields
}

func NewOSFSStat

func NewOSFSStat(path string) *OSFSStat

func (*OSFSStat) Stat

func (o *OSFSStat) Stat(p string) (fs.FileInfo, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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