Skip to content

Commit

Permalink
Add headers configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislbr committed Mar 2, 2021
1 parent d5a4c09 commit 718d207
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions gss.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
directory: public
headers:
Referrer-Policy: "strict-origin-when-cross-origin"
Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload"
port: 7892
24 changes: 18 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -65,6 +68,8 @@ func setUpYAML() {
return
}

config := configYAML{}

// Serialize the YAML content
err = yaml.Unmarshal([]byte(content), &config)
if err != nil {
Expand All @@ -74,14 +79,17 @@ 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 ⚠️")
}

// Assign non-empty values
if config.Dir != "" {
dir = config.Dir
}
if len(config.Headers) != 0 {
headers = config.Headers
}
if config.Port != "" {
port = config.Port
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 718d207

Please sign in to comment.