mwencoding

package module
v0.0.0-...-6b89116 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2018 License: MIT Imports: 6 Imported by: 0

README

go-middleware-encoding

Content encoding middleware for the go-json-rest

Encoders:
  • None Encoder
  • Deflate Encoder (RFC 1950 and RFC 1951)
  • Gzip Encoder (RFC 1952)
Optional C encoders:
  • C Gzip Encoder (RFC 1952)
  • C Brotli encoder (no RFC yet)
Examples
1. Using default settings (deflate, gzip)
package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "github.com/urakozz/go-middleware-encoding"
    "log"
    "net/http"
)

func main() {

    mw := []rest.Middleware{
    		&rest.PoweredByMiddleware{
    			XPoweredBy: "Golang",
    		},
    		&mwencoding.EncodingMiddleware{},
    	}
    	
    api := rest.NewApi()
    api.Use(mw...)
    api.SetApp(rest.AppSimple(func(w rest.ResponseWriter, r *rest.Request) {
        w.WriteJson(map[string]string{"Body": "Hello World!"})
    }))
    log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}
2. Using (deflate, C-gzip, brotli)
package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "github.com/urakozz/go-middleware-encoding"
    "github.com/urakozz/go-middleware-encoding/encoding/cencoding"
    "log"
    "net/http"
)

func main() {
    mwencoding.RegisterEncoder(&cencoding.EncoderCGzip{})
    mwencoding.RegisterEncoder(&cencoding.EncoderCBrotli{})
    
    mw := []rest.Middleware{
    		&rest.PoweredByMiddleware{
    			XPoweredBy: "Golang",
    		},
    		&mwencoding.EncodingMiddleware{},
    	}
    	
    api := rest.NewApi()
    api.Use(mw...)
    api.SetApp(rest.AppSimple(func(w rest.ResponseWriter, r *rest.Request) {
        w.WriteJson(map[string]string{"Body": "Hello World!"})
    }))
    log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterEncoder

func RegisterEncoder(e encoding.Encoder)

func SelectEncoder

func SelectEncoder(accept string) (enc encoding.Encoder)

Types

type EncodingMiddleware

type EncodingMiddleware struct {
	Level int
}

GzipMiddleware is responsible for compressing the payload with gzip and setting the proper headers when supported by the client. It must be wrapped by TimerMiddleware for the compression time to be captured. And It must be wrapped by RecorderMiddleware for the compressed BYTES_WRITTEN to be captured.

func (*EncodingMiddleware) MiddlewareFunc

func (mw *EncodingMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc

MiddlewareFunc makes GzipMiddleware implement the Middleware interface.

type Flusher

type Flusher interface {
	Flush() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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