github.com/jacobsoderblom/buffalo@v0.11.0/middleware/ssl/ssl.go (about)

     1  package ssl
     2  
     3  import (
     4  	"github.com/gobuffalo/buffalo"
     5  	"github.com/unrolled/secure"
     6  )
     7  
     8  // ForceSSL uses the github.com/unrolled/secure package to
     9  // automatically force a redirect to https from http.
    10  // See https://github.com/unrolled/secure/ for more details
    11  // on configuring.
    12  func ForceSSL(opts secure.Options) buffalo.MiddlewareFunc {
    13  	return func(next buffalo.Handler) buffalo.Handler {
    14  		sm := secure.New(opts)
    15  		return func(c buffalo.Context) error {
    16  			err := sm.Process(c.Response(), c.Request())
    17  			if err != nil {
    18  				return nil
    19  			}
    20  			if res, ok := c.Response().(*buffalo.Response); ok {
    21  				status := res.Status
    22  				if status > 300 && status < 399 {
    23  					return nil
    24  				}
    25  			}
    26  			return next(c)
    27  		}
    28  	}
    29  }