<commit_before>[![wercker status](https://app.wercker.com/status/1438db57ab5937e1fd92d45eca974257/s/master "wercker status")](https://app.wercker.com/project/bykey/1438db57ab5937e1fd92d45eca974257)
[![Coverage Status](https://coveralls.io/repos/jadengore/go-json-rest-middleware-force-ssl/badge.svg?branch=HEAD&service=github)](https://coveralls.io/github/jadengore/go-json-rest-middleware-force-ssl?branch=HEAD)
[![GoDoc](https://godoc.org/github.com/jadengore/go-json-rest-middleware-force-ssl?status.svg)](https://godoc.org/github.com/jadengore/go-json-rest-middleware-force-ssl)
[![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/jadengore/go-json-rest-middleware-force-ssl/master/LICENSE)

# Force SSL Middleware for go-json-rest
Middleware to force SSL on requests to a `go-json-rest` API.

## Installation

```sh
go get github.com/jadengore/go-json-rest-middleware-force-ssl
```

## Example Usage

```go
package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "github.com/jadengore/go-json-rest-middleware-force-ssl"
    "log"
    "net/http"
)

func main() {
    api := rest.NewApi()
    api.Use(forceSSL.Middleware{}) // struct with options
    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()))
}
```

## Options

| Option                 | Type      | Description   | Defaults to   |
| ---------------------- | --------- | ------------- | ------------- |
| **TrustXFPHeader**     | `bool`    | Trust `X-Forwarded-Proto` headers (this can allow a client to spoof whether they were using HTTPS) | `false` |
| **Enable301Redirects** | `bool`    | Enables `301` redirects to the HTTPS version of the request. | `false` |
| **Message**            | `string`  | Allows a custom response message when forcing SSL without redirect. | `SSL Required.` |

## Middleware Options Example

```go
api.Use(forceSSL.Middleware{
  TrustXFPHeader: true,
  Enable301Redirects: true,
  Message: "We are unable to process your request over HTTP."
})
```<commit_msg>Add documentation for per-route SSL settings<commit_after>[![wercker status](https://app.wercker.com/status/1438db57ab5937e1fd92d45eca974257/s/master "wercker status")](https://app.wercker.com/project/bykey/1438db57ab5937e1fd92d45eca974257)
[![Coverage Status](https://coveralls.io/repos/jadengore/go-json-rest-middleware-force-ssl/badge.svg?branch=HEAD&service=github)](https://coveralls.io/github/jadengore/go-json-rest-middleware-force-ssl?branch=HEAD)
[![GoDoc](https://godoc.org/github.com/jadengore/go-json-rest-middleware-force-ssl?status.svg)](https://godoc.org/github.com/jadengore/go-json-rest-middleware-force-ssl)
[![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/jadengore/go-json-rest-middleware-force-ssl/master/LICENSE)

# Force SSL Middleware for go-json-rest
Middleware to force SSL on requests to a `go-json-rest` API.

## Installation

```sh
go get github.com/jadengore/go-json-rest-middleware-force-ssl
```

## Example Usage

```go
package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "github.com/jadengore/go-json-rest-middleware-force-ssl"
    "log"
    "net/http"
)

func main() {
    api := rest.NewApi()
    api.Use(&forceSSL.Middleware{}) // struct with options
    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()))
}
```

## Options

| Option                 | Type      | Description   | Defaults to   |
| ---------------------- | --------- | ------------- | ------------- |
| **TrustXFPHeader**     | `bool`    | Trust `X-Forwarded-Proto` headers (this can allow a client to spoof whether they were using HTTPS) | `false` |
| **Enable301Redirects** | `bool`    | Enables `301` redirects to the HTTPS version of the request. | `false` |
| **Message**            | `string`  | Allows a custom response message when forcing SSL without redirect. | `SSL Required.` |

### Middleware Options Example

```go
api.Use(forceSSL.Middleware{
  TrustXFPHeader: true,
  Enable301Redirects: true,
  Message: "We are unable to process your request over HTTP."
})
```

### Per-route SSL Settings
Using `rest.IfMiddleware` in `go-json-rest`, it is possible to force SSL on a per-route basis.

#### Example Usage
```go
forceSSLMiddleware := &forceSSL.Middleware{
	TrustXFPHeader:     true,
	Enable301Redirects: false,
	Message:            "Login required for Admin portal.",
}
api := rest.NewApi()

// Conditionally force certain routes to use forceSSLMiddleware
api.Use(&rest.IfMiddleware{
	Condition: func(request *rest.Request) bool {
		return request.URL.Path == "/admin"
	},
	IfTrue: forceSSLMiddleware,
})
```
{'fraction_non_alphanumeric': 0.17642292890419284, 'fraction_numerical': 0.023293498075754506, 'mean_word_length': 3.693916349809886, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2509980', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1653, 'n_words': 306}