Add Regex
option for HeaderConfig
This commit is contained in:
parent
758675c3a9
commit
ece4a91ec3
3 changed files with 74 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
vendor/
|
||||
goStatic
|
||||
.DS_Store
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -17,9 +18,22 @@ type HeaderConfigArray struct {
|
|||
|
||||
// HeaderConfig is a single header rule specification
|
||||
type HeaderConfig struct {
|
||||
Regex string `json:"regex"`
|
||||
Path string `json:"path"`
|
||||
FileExtension string `json:"fileExtension"`
|
||||
Headers []HeaderDefiniton `json:"headers"`
|
||||
|
||||
CompiledRegex *regexp.Regexp
|
||||
}
|
||||
|
||||
func (config *HeaderConfig) Init() {
|
||||
if config.UsesRegex() {
|
||||
config.CompiledRegex = regexp.MustCompile(config.Regex)
|
||||
}
|
||||
}
|
||||
|
||||
func (config *HeaderConfig) UsesRegex() bool {
|
||||
return len(config.Regex) > 0
|
||||
}
|
||||
|
||||
// HeaderDefiniton is a key value pair of a specified header rule
|
||||
|
@ -28,7 +42,7 @@ type HeaderDefiniton struct {
|
|||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
var headerConfigs HeaderConfigArray
|
||||
var headerConfigs *HeaderConfigArray
|
||||
|
||||
func fileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
|
@ -38,9 +52,13 @@ func fileExists(filename string) bool {
|
|||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func logHeaderConfig(config HeaderConfig) {
|
||||
func logHeaderConfig(config *HeaderConfig) {
|
||||
if config.UsesRegex() {
|
||||
fmt.Println("Regex: " + config.Regex)
|
||||
} else {
|
||||
fmt.Println("Path: " + config.Path)
|
||||
fmt.Println("FileExtension: " + config.FileExtension)
|
||||
}
|
||||
|
||||
for j := 0; j < len(config.Headers); j++ {
|
||||
headerRule := config.Headers[j]
|
||||
|
@ -69,7 +87,8 @@ func initHeaderConfig(headerConfigPath string) bool {
|
|||
fmt.Println("------------------------------")
|
||||
|
||||
for i := 0; i < len(headerConfigs.Configs); i++ {
|
||||
configEntry := headerConfigs.Configs[i]
|
||||
configEntry := &headerConfigs.Configs[i]
|
||||
configEntry.Init()
|
||||
logHeaderConfig(configEntry)
|
||||
}
|
||||
} else {
|
||||
|
@ -89,11 +108,19 @@ func customHeadersMiddleware(next http.Handler) http.Handler {
|
|||
|
||||
for i := 0; i < len(headerConfigs.Configs); i++ {
|
||||
configEntry := headerConfigs.Configs[i]
|
||||
var matches bool
|
||||
|
||||
if configEntry.UsesRegex() {
|
||||
if configEntry.CompiledRegex.MatchString(r.URL.Path) {
|
||||
matches = true
|
||||
}
|
||||
} else {
|
||||
fileMatch := configEntry.FileExtension == "*" || reqFileExtension == "."+configEntry.FileExtension
|
||||
pathMatch := configEntry.Path == "*" || strings.HasPrefix(r.URL.Path, configEntry.Path)
|
||||
matches = fileMatch && pathMatch
|
||||
}
|
||||
|
||||
if fileMatch && pathMatch {
|
||||
if matches {
|
||||
for j := 0; j < len(configEntry.Headers); j++ {
|
||||
headerEntry := configEntry.Headers[j]
|
||||
w.Header().Set(headerEntry.Key, headerEntry.Value)
|
||||
|
|
|
@ -4,7 +4,44 @@ With the header config, you can specify custom [HTTP Header](https://developer.m
|
|||
|
||||
## Config
|
||||
|
||||
You have to create a JSON file that serves as a config. The JSON must contain a `configs` array. For every entry, you can specify a certain path that must be matched as well as a file extension. You can use the `*` symbol to use the config entry for any path or filename. Note that the path option only matches the requested path from the start. Thatswhy you have to start with a `/` and can use paths like `/files/static/css`. The `headers` array includes a key-value pair of the actual header rule. The headers are not parsed so double check your spelling and test your site.
|
||||
You have to create a JSON file that serves as a config. The JSON must contain a `configs` array. For every entry, you can specify the rule in one of two ways:
|
||||
|
||||
1. A regular expression that the path must meet, e.g.:
|
||||
|
||||
```json
|
||||
{
|
||||
"regex": "/$",
|
||||
"headers": [
|
||||
{
|
||||
"key": "cache-control",
|
||||
"value": "no-cache, no-store, must-revalidate"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
This rule would match any path ending in `/` which is useful if you never want to cache the `index.html` that a directory leads to.
|
||||
|
||||
2. You may specify a combination of `path` and `fileExtension`:
|
||||
|
||||
```json
|
||||
{
|
||||
"path": "*",
|
||||
"fileExtension": "html",
|
||||
"headers": [
|
||||
{
|
||||
"key": "cache-control",
|
||||
"value": "public, max-age=0, must-revalidate"
|
||||
},
|
||||
{
|
||||
"key": "Strict-Transport-Security",
|
||||
"value": "max-age=31536000; includeSubDomains;"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
You can use the `*` symbol to use the config entry for any path or filename. Note that the path option only matches the requested path from the start. That's why you have to start with a `/` and can use paths like `/files/static/css`. The `headers` array includes a key-value pair of the actual header rule. The headers are not parsed so double check your spelling and test your site.
|
||||
|
||||
The created JSON config has to be mounted into the container via a volume into `/config/headerConfig.json` per default. When this file does not exist inside the container, the header middleware will not be active.
|
||||
|
||||
|
|
Loading…
Reference in a new issue