All Projects → lpar → gzipped

lpar / gzipped

Licence: BSD-3-Clause license
Replacement for golang http.FileServer which supports precompressed static assets.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to gzipped

pyrus-cramjam
Thin Python wrapper to de/compression algorithms in Rust - lightweight & no dependencies
Stars: ✭ 40 (-53.49%)
Mutual labels:  compression, gzip, brotli
Fastify Compress
Fastify compression utils
Stars: ✭ 95 (+10.47%)
Mutual labels:  compression, gzip, brotli
Turbobench
Compression Benchmark
Stars: ✭ 211 (+145.35%)
Mutual labels:  compression, gzip, brotli
EasyCompressor
⚡ A compression library that implements many compression algorithms such as LZ4, Zstd, LZMA, Snappy, Brotli, GZip, and Deflate. It helps you to improve performance by reducing Memory Usage and Network Traffic for caching.
Stars: ✭ 167 (+94.19%)
Mutual labels:  compression, gzip, brotli
Docker Nginx Brotli
Stable nginx with google brotli compression module
Stars: ✭ 107 (+24.42%)
Mutual labels:  compression, brotli
Sharpcompress
SharpCompress is a fully managed C# library to deal with many compression types and formats.
Stars: ✭ 1,397 (+1524.42%)
Mutual labels:  compression, gzip
restio
HTTP Client for Dart inspired by OkHttp
Stars: ✭ 46 (-46.51%)
Mutual labels:  gzip, brotli
Bit7z
A C++ static library offering a clean and simple interface to the 7-zip DLLs.
Stars: ✭ 159 (+84.88%)
Mutual labels:  compression, gzip
Genozip
Compressor for genomic files (FASTQ, SAM/BAM, VCF, FASTA, GVF, 23andMe...), up to 5x better than gzip and faster too
Stars: ✭ 53 (-38.37%)
Mutual labels:  compression, gzip
Gzip
💾 Golang gzip middleware for Gin and net/http | Golang gzip中间件,支持Gin和net/http,开箱即用同时可定制
Stars: ✭ 113 (+31.4%)
Mutual labels:  compression, gzip
Uzlib
Radically unbloated DEFLATE/zlib/gzip compression/decompression library. Can decompress any gzip/zlib data, and offers simplified compressor which produces gzip-compatible output, while requiring much less resources (and providing less compression ratio of course).
Stars: ✭ 168 (+95.35%)
Mutual labels:  compression, gzip
precompress
Generate pre-compressed .gz and .br files for static web servers
Stars: ✭ 27 (-68.6%)
Mutual labels:  gzip, brotli
Compression
Node.js compression middleware
Stars: ✭ 2,506 (+2813.95%)
Mutual labels:  compression, gzip
Zippy
Pure Nim implementation of deflate, zlib, gzip and zip.
Stars: ✭ 88 (+2.33%)
Mutual labels:  compression, gzip
Libbrotli
meta project to build libraries from the brotli source code
Stars: ✭ 110 (+27.91%)
Mutual labels:  compression, brotli
Denoflate
WebAssembly powered Deflate/Gzip/Zlib compression for Deno, written in Rust
Stars: ✭ 80 (-6.98%)
Mutual labels:  compression, gzip
Tinydeflate
A deflate/gzip decompressor that requires minimal amount of memory to work
Stars: ✭ 148 (+72.09%)
Mutual labels:  compression, gzip
restish
Restish is a CLI for interacting with REST-ish HTTP APIs with some nice features built-in
Stars: ✭ 453 (+426.74%)
Mutual labels:  gzip, brotli
Peazip
Free Zip / Unzip software and Rar file extractor. Cross-platform file and archive manager. Features volume spanning, compression, authenticated encryption. Supports 7Z, 7-Zip sfx, ACE, ARJ, Brotli, BZ2, CAB, CHM, CPIO, DEB, GZ, ISO, JAR, LHA/LZH, NSIS, OOo, PAQ/LPAQ, PEA, QUAD, RAR, RPM, split, TAR, Z, ZIP, ZIPX, Zstandard.
Stars: ✭ 827 (+861.63%)
Mutual labels:  compression, brotli
Deno brotli
🗜 Brotli wasm module for deno
Stars: ✭ 40 (-53.49%)
Mutual labels:  compression, brotli

GoDoc Build Status codecov

gzipped.FileServer

Drop-in replacement for golang http.FileServer which supports static content compressed with gzip (including zopfli) or brotli.

This allows major bandwidth savings for CSS, JavaScript libraries, fonts, and other static compressible web content. It also means you can compress the content without significant runtime penalty.

Example

Suppose /var/www/assets/css contains your style sheets, and you want to make them available as /css/*.css:

package main

import (
	"log"
	"net/http"

	"github.com/lpar/gzipped/v2"
)

func main() {
	log.Fatal(http.ListenAndServe(":8080", http.StripPrefix("/css",
    gzipped.FileServer(gzipped.Dir("/var/www/assets/css")))))
}
// curl localhost:8080/css/styles.css

Using httprouter?

router := httprouter.New()
router.Handler("GET", "/css/*filepath", 
  gzipped.FileServer(gzipped.Dir("/var/www/assets/css"))))
log.Fatal(http.ListenAndServe(":8080", router)

Change history

In version 2.0, we require use of gzipped.Dir, a drop-in replacement for http.Dir. Our gzipped.Dir has the additional feature of letting us check for the existence of files without opening them. This means we can scan to see what encodings are available, then negotiate that list against the client's preferences, and then only (attempt to) open and serve the correct file.

This change means we can let github.com/kevinpollet/nego handle the content negotiation, and remove the dependency on gddo (godoc), which was pulling in 48 dependencies (see #6).

Detail

For any given request at /path/filename.ext, if:

  1. There exists a file named /path/filename.ext.(gz|br) (starting from the appropriate base directory), and
  2. the client will accept content compressed via the appropriate algorithm, and
  3. the file can be opened,

then the compressed file will be served as /path/filename.ext, with a Content-Encoding header set so that the client transparently decompresses it. Otherwise, the request is passed through and handled unchanged.

Unlike other similar code I found, this package has a license, parses Accept-Encoding headers properly, and has unit tests.

Caveats

All requests are passed to Go's standard http.ServeContent method for fulfilment. MIME type mapping, accept ranges, content negotiation and other tricky details are handled by that method. If the extension of a file doesn't have a defined MIME type, ServeContent's sniffing may result in it assigning a MIME type such as application/x-gzip rather than what you might hope. See issue #18 for more information.

It is up to you to ensure that your compressed and uncompressed resources are kept in sync.

Directory browsing isn't supported. That includes remapping URLs ending in / to index.html, index.htm, Welcome.html or whatever — if you want URLs remapped that way, I suggest having your router do it, or using middleware, so that you have control over the behavior. For example, to add support for index.html files in directories:

func withIndexHTML(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasSuffix(r.URL.Path, "/") || len(r.URL.Path) == 0 {
			newpath := path.Join(r.URL.Path, "index.html")
			r.URL.Path = newpath
		}
		h.ServeHTTP(w, r)
	})
}
// ...

fs := withIndexHTML(gzipped.FileServer(http.Dir("/var/www")))

Or to add support for directory browsing:

func withBrowsing(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasSuffix(r.URL.Path, "/") {
			http.ServeFile(w, r, ".")
		}
	})
}
// ...

fs := withBrowsing(gzipped.FileServer(http.Dir("/var/www")))

Related

  • You might consider precompressing your CSS with minify.

  • If you want to get the best possible compression for clients which don't support brotli, use zopfli.

  • To compress your dynamically-generated HTML pages on the fly, I suggest gziphandler.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].