diff --git a/README.md b/README.md index dc4ed5b..4bb5a7b 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,22 @@ Default: `dist`. > docker run -p 8080:80 -v $PWD/public:/public lewislbr/gss -d public > ``` +### `headers` (YAML) + +Headers to add to the response. + +Default: `Server: GSS`. + +> Example: +> +> YAML: +> +> ```yaml +> headers: +> Referrer-Policy: "strict-origin-when-cross-origin" +> Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload" +> ``` + ### `-p` (CLI), `port` (YAML) Port where to run the server. diff --git a/gss.yaml b/gss.yaml index 8d4f43e..91cc9dc 100644 --- a/gss.yaml +++ b/gss.yaml @@ -1,2 +1,5 @@ directory: public +headers: + Referrer-Policy: "strict-origin-when-cross-origin" + Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload" port: 7892 diff --git a/src/main.go b/src/main.go index d644f8a..f5f179a 100644 --- a/src/main.go +++ b/src/main.go @@ -13,12 +13,16 @@ import ( "gopkg.in/yaml.v2" ) -var dir = "dist" -var port = "80" +var ( + dir = "dist" + headers = map[string]string{} + port = "80" +) type configYAML struct { - Dir string `yaml:"directory,omitempty"` - Port string `yaml:"port,omitempty"` + Dir string `yaml:"directory,omitempty"` + Headers map[string]string `yaml:"headers,omitempty"` + Port string `yaml:"port,omitempty"` } func main() { @@ -47,7 +51,6 @@ func startServer() { // setUpYAML enables configuration via YAML file. func setUpYAML() { - config := configYAML{} configFile := "gss.yaml" // Check if there is a config file @@ -65,6 +68,8 @@ func setUpYAML() { return } + config := configYAML{} + // Serialize the YAML content err = yaml.Unmarshal([]byte(content), &config) if err != nil { @@ -74,7 +79,7 @@ func setUpYAML() { } // Check if values are empty - if config.Dir == "" || config.Port == "" { + if config.Dir == "" || len(config.Headers) == 0 || config.Port == "" { log.Println("Warning: some YAML config values are empty ⚠️") } @@ -82,6 +87,9 @@ func setUpYAML() { if config.Dir != "" { dir = config.Dir } + if len(config.Headers) != 0 { + headers = config.Headers + } if config.Port != "" { port = config.Port } @@ -190,6 +198,10 @@ func addHeaders(h http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Server", "GSS") + for k, v := range headers { + w.Header().Add(k, v) + } + h.ServeHTTP(w, r) } }