Skip to content

Commit

Permalink
Merge pull request #7 from skpr/custom-headers
Browse files Browse the repository at this point in the history
Adds config file and ability to add custom headers
  • Loading branch information
nickschuch authored Nov 24, 2022
2 parents 47b399c + 309a8bb commit 4d4ca99
Show file tree
Hide file tree
Showing 27 changed files with 10,024 additions and 22 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:latest AS builder
FROM golang:1.19 AS builder

LABEL stage=builder
RUN mkdir -p /go/src/github.com/skpr/proxy-app
Expand All @@ -11,7 +11,11 @@ RUN GO111MODULE=on GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -ldflags '-
FROM skpr/base:1.x

ENV PROXY_APP_ADDR=":8080"
ENV PROXY_APP_MAX_AGE="5m"
ENV PROXY_APP_CONFIG_FILE_PATH="/etc/skpr/proxy-app/config.yaml"

ADD example/config.yaml /etc/skpr/proxy-app/config.yaml

RUN cat $PROXY_APP_CONFIG_FILE_PATH

COPY --from=builder /go/src/github.com/skpr/proxy-app/proxy-app /usr/local/bin/proxy-app
RUN chmod +x /usr/local/bin/proxy-app
Expand Down
3 changes: 3 additions & 0 deletions example/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
responseHeaders:
Cache-Control: "max-age=300, public"
# Strict-Transport-Security: "max-age=86400; includeSubDomains"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/skpr/go-config v0.0.0-20210803015120-ef4cd8061d76
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
Expand Down
31 changes: 31 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import (
"fmt"
"os"

"gopkg.in/yaml.v2"
)

// File for configuring the proxy application behaviours.
type File struct {
// ResponseHeaders to be added to all responses.
ResponseHeaders map[string]string `yaml:"responseHeaders"`
}

// Load config file from path.
func Load(path string) (File, error) {
var file File

data, err := os.ReadFile(path)
if err != nil {
return file, fmt.Errorf("failed to load file: %w", err)
}

err = yaml.Unmarshal(data, &file)
if err != nil {
return file, fmt.Errorf("failed to marshal data: %w", err)
}

return file, nil
}
20 changes: 6 additions & 14 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package server
import (
"encoding/base64"
"fmt"
"github.com/skpr/proxy-app/internal/config"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
)

// RunParams is passed to the Run() function.
Expand All @@ -22,8 +22,6 @@ type RunParams struct {
Password string
// TrimPathPrefix from backend requests.
TrimPathPrefix string
// MaxAge applied to a response.
MaxAge string
}

// Validate the server parameters.
Expand All @@ -36,24 +34,15 @@ func (p RunParams) Validate() error {
return fmt.Errorf("not provided: endpoint")
}

if p.MaxAge == "" {
return fmt.Errorf("not provided: max-age")
}

return nil
}

// Run the server.
func Run(params RunParams) error {
func Run(params RunParams, config config.File) error {
if err := params.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}

maxAge, err := time.ParseDuration(params.MaxAge)
if err != nil {
return fmt.Errorf("failed to parse max age: %w", err)
}

endpoint, err := url.Parse(params.Endpoint)
if err != nil {
return fmt.Errorf("failed to parse endpoint: %w", err)
Expand Down Expand Up @@ -82,7 +71,10 @@ func Run(params RunParams) error {
}

proxy.ModifyResponse = func(r *http.Response) error {
r.Header.Set("Cache-Control", fmt.Sprintf("max-age=%v, public", maxAge.Seconds()))
for headerKey, headerValue := range config.ResponseHeaders {
r.Header.Set(headerKey, headerValue)
}

return nil
}

Expand Down
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"errors"
"fmt"
"os"

skprconfig "github.com/skpr/go-config"
"github.com/skpr/proxy-app/internal/config"
"github.com/skpr/proxy-app/internal/server"
)

Expand All @@ -19,8 +21,6 @@ const (
EnvSkprConfigKeyPassword = "PROXY_APP_CONFIG_KEY_PASSWORD"
// EnvSkprConfigKeyTrimPathPrefix used to load the path prefix value from Skpr config.
EnvSkprConfigKeyTrimPathPrefix = "PROXY_APP_CONFIG_KEY_TRIM_PATH_PREFIX"
// EnvSkprConfigKeyMaxAge used to set the max-age for all responses.
EnvSkprConfigKeyMaxAge = "PROXY_APP_CONFIG_KEY_MAX_AGE"

// EnvAddr sets the address for the proxy application.
EnvAddr = "PROXY_APP_ADDR"
Expand All @@ -32,8 +32,8 @@ const (
EnvPassword = "PROXY_APP_PASSWORD"
// EnvTrimPathPrefix strips the path prefix from backend requests.
EnvTrimPathPrefix = "PROXY_APP_TRIM_PATH_PREFIX"
// EnvMaxAge used to set the max-age for all responses.
EnvMaxAge = "PROXY_APP_MAX_AGE"
// EnvConfigFilePath is used to load a file which configures this app's advanced behaviours.
EnvConfigFilePath = "PROXY_APP_CONFIG_FILE_PATH"
)

func main() {
Expand All @@ -48,10 +48,16 @@ func main() {
Username: skprclient.GetWithFallback(os.Getenv(EnvSkprConfigKeyUsername), os.Getenv(EnvUsername)),
Password: skprclient.GetWithFallback(os.Getenv(EnvSkprConfigKeyPassword), os.Getenv(EnvPassword)),
TrimPathPrefix: skprclient.GetWithFallback(os.Getenv(EnvSkprConfigKeyTrimPathPrefix), os.Getenv(EnvTrimPathPrefix)),
MaxAge: skprclient.GetWithFallback(os.Getenv(EnvSkprConfigKeyMaxAge), os.Getenv(EnvMaxAge)),
}

if err := server.Run(params); err != nil {
configFile, err := config.Load(os.Getenv(EnvConfigFilePath))
if err != nil {
panic(err)
}

fmt.Println(configFile)

if err := server.Run(params, configFile); err != nil {
panic(err)
}
}
12 changes: 12 additions & 0 deletions vendor/gopkg.in/yaml.v2/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 201 additions & 0 deletions vendor/gopkg.in/yaml.v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4d4ca99

Please sign in to comment.