parapet

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2019 License: MIT Imports: 14 Imported by: 20

README

parapet

Build Status codecov Go Report Card GoDoc

Reverse Proxy Framework

Currently in very early stage, API will breaking change a lot!!!

Example

package main

import (
    "log"

    "github.com/moonrhythm/parapet"
    "github.com/moonrhythm/parapet/pkg/body"
    "github.com/moonrhythm/parapet/pkg/compress"
    "github.com/moonrhythm/parapet/pkg/headers"
    "github.com/moonrhythm/parapet/pkg/healthz"
    "github.com/moonrhythm/parapet/pkg/host"
    "github.com/moonrhythm/parapet/pkg/hsts"
    "github.com/moonrhythm/parapet/pkg/location"
    "github.com/moonrhythm/parapet/pkg/logger"
    "github.com/moonrhythm/parapet/pkg/ratelimit"
    "github.com/moonrhythm/parapet/pkg/requestid"
    "github.com/moonrhythm/parapet/pkg/redirect"
    "github.com/moonrhythm/parapet/pkg/upstream"
)

func main() {
    s := parapet.New()
    s.Use(logger.Stdout())
    s.Use(requestid.New())
    s.Use(ratelimit.FixedWindowPerSecond(60))
    s.Use(ratelimit.FixedWindowPerMinute(300))
    s.Use(ratelimit.FixedWindowPerHour(2000))
    s.Use(body.LimitRequest(15 * 1024 * 1024)) // 15 MiB
    s.Use(body.BufferRequest())
    s.Use(compress.Gzip())
    s.Use(compress.Br())

    // sites
    s.Use(example())
    s.Use(mysite())
    s.Use(wordpress())

    // health check
    {
        l := location.Exact("/healthz")
        l.Use(logger.Disable())
        l.Use(healthz.New())
        s.Use(l)
    }

    s.Addr = ":8080"
    err := s.ListenAndServe()
    if err != nil {
        log.Fatal(err)
    }
}

func example() parapet.Middleware {
    h := host.New("example.com", "www.example.com")
    h.Use(ratelimit.FixedWindowPerSecond(20))
    h.Use(redirect.HTTPS())
    h.Use(hsts.Preload())
    h.Use(redirect.NonWWW())
    h.Use(upstream.New("http://example.default.svc.cluster.local:8080"))

    return h
}

func mysite() parapet.Middleware {
    var hs parapet.Middlewares

    {
        h := host.New("mysiteaaa.io", "www.mysiteaaa.io")
        h.Use(ratelimit.FixedWindowPerSecond(15))
        h.Use(redirect.HTTPS())
        h.Use(hsts.Preload())
        h.Use(redirect.WWW())
        h.Use(headers.DeleteResponse(
            "Server",
            "x-goog-generation",
            "x-goog-hash",
            "x-goog-meta-goog-reserved-file-mtime",
            "x-goog-metageneration",
            "x-goog-storage-class",
            "x-goog-stored-content-encoding",
            "x-goog-stored-content-length",
            "x-guploader-uploadid",
        ))
        h.Use(upstream.New("https://storage.googleapis.com"))

        hs.Use(h)
    }

    {
        h := host.New("mail.mysiteaaa.io")
        h.Use(redirect.HTTPS())
        h.Use(hsts.Preload())
        h.Use(redirect.To("https://mail.google.com/a/mysiteaaa.io", 302))

        hs.Use(h)
    }

    return hs
}

func wordpress() parapet.Middleware {
    h := host.New("myblogaaa.com", "www.myblogaaa.com")
    h.Use(ratelimit.FixedWindowPerMinute(150))
    h.Use(redirect.HTTPS())
    h.Use(hsts.Preload())
    h.Use(redirect.NonWWW())

    backend := upstream.New("http://wordpress.default.svc.cluster.local")

    l := location.RegExp(`\.(js|css|svg|png|jp(e)?g|gif)$`)
    l.Use(headers.SetResponse("Cache-Control", "public, max-age=31536000"))
    l.Use(backend)
    h.Use(l)

    h.Use(backend)

    return h
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block interface {
	Middleware
	Use(Middleware)
}

Block is the middleware block

type Middleware

type Middleware interface {
	ServeHandler(http.Handler) http.Handler
}

Middleware is the http middleware

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

MiddlewareFunc is the adapter type for Middleware

func (MiddlewareFunc) ServeHandler

func (f MiddlewareFunc) ServeHandler(h http.Handler) http.Handler

ServeHandler calls f

type Middlewares

type Middlewares []Middleware

Middlewares type

func (Middlewares) ServeHandler

func (ms Middlewares) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

func (*Middlewares) Use

func (ms *Middlewares) Use(m Middleware)

Use uses middleware

type Server

type Server struct {
	Addr               string
	Handler            http.Handler
	ReadTimeout        time.Duration
	ReadHeaderTimeout  time.Duration
	WriteTimeout       time.Duration
	IdleTimeout        time.Duration
	TCPKeepAlivePeriod time.Duration
	GraceTimeout       time.Duration
	ErrorLog           *log.Logger
	TrackConnState     bool
	TrustProxy         bool
	H2C                bool
	ReusePort          bool
	// contains filtered or unexported fields
}

Server is the parapet server

func New

func New() *Server

New creates new middleware server default config

This server should not expose to the internet but run behide reverse proxy

func NewBackend

func NewBackend() *Server

NewBackend creates new backend server default config

This server use to run behide parapet server or run behide other reverse proxy

func NewFrontend

func NewFrontend() *Server

NewFrontend creates new frontend server default config

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts web server

func (*Server) Serve

func (s *Server) Serve(l net.Listener) error

Serve serves incoming connections

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown shutdowns server

func (*Server) Use

func (s *Server) Use(m Middleware)

Use uses middleware

Jump to

Keyboard shortcuts

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