Skip to content

Commit

Permalink
Merge pull request #8 from jasongoodwin/main
Browse files Browse the repository at this point in the history
feat(api): add POST method that will add a uuid
  • Loading branch information
francisf2 authored Feb 1, 2024
2 parents 78f8095 + f32c908 commit feef16f
Show file tree
Hide file tree
Showing 29 changed files with 1,490 additions and 20 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ jobs:
build:
name: build
env:
GO_VERSION: 1.20.0
GOLANGCI_LINT_VERSION: v1.53
YAEGI_VERSION: v0.15.1
GO_VERSION: 1.21.3
GOLANGCI_LINT_VERSION: v1.55.2
CGO_ENABLED: 0
runs-on: ubuntu-latest
steps:
Expand All @@ -35,6 +34,13 @@ jobs:
- name: Set up GOPATH
run: go env -w GOPATH=${{ github.workspace }}/go

- name: Check and get dependencies
run: |
go mod tidy
git diff --exit-code go.mod
go mod download
go mod vendor
# https://github.com/marketplace/actions/run-golangci-lint
- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
uses: golangci/golangci-lint-action@v3
Expand All @@ -45,13 +51,5 @@ jobs:
- name: Lint, Test, Build
run: make lint test build

- name: Install yaegi ${{ env.YAEGI_VERSION }}
run: curl -sfL https://raw.githubusercontent.com/traefik/yaegi/master/install.sh | bash -s -- -b ${{ github.workspace }}/go/bin ${{ env.YAEGI_VERSION }}

- name: Copy sources
run: make copy_src

- name: Run tests with yaegi
run: ${{ github.workspace }}/go/bin/yaegi test -v github.com/bluecatengineering/traefik-aws-plugin
env:
GOPATH: ${{ github.workspace }}/go
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ experimental:
plugins:
aws:
moduleName: github.com/bluecatengineering/traefik-aws-plugin
version: v0.1.2
```
version: v0.2.0

Example labels for a given router:

Expand All @@ -37,7 +36,9 @@ To store objects in a local directory, use the following labels (example):
"traefik.http.middlewares.my-aws.plugin.aws.directory" : "aws-local-directory"
```

`GET` and `PUT` are supported.
`GET`, `PUT` and `POST` are supported.
`POST` will append a UUID to the path. There is a `Location` header in the response.


### S3

Expand All @@ -53,7 +54,8 @@ To store objects in [Amazon Simple Storage Service (S3)](https://docs.aws.amazon

Note that `prefix` must include the leading slash.

[PUT](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html) and [GET](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html) are supported.
[PUT](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html) and [GET](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html) are supported.
If you make a POST request, a UUID will be generated and the object will be created in the same manner as a PUT request. A `Location` header is sent back in the response.

When forwarding the request to S3, the plugin sets the following headers:

Expand Down
1 change: 1 addition & 0 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type Service interface {
Put(name string, payload []byte, contentType string, rw http.ResponseWriter) ([]byte, error)
Post(name string, payload []byte, contentType string, rw http.ResponseWriter) ([]byte, error)
Get(name string, rw http.ResponseWriter) ([]byte, error)
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/bluecatengineering/traefik-aws-plugin

go 1.20

require github.com/google/uuid v1.6.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
11 changes: 9 additions & 2 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package local

import (
"fmt"
"github.com/bluecatengineering/traefik-aws-plugin/log"
"net/http"
"os"

"github.com/bluecatengineering/traefik-aws-plugin/log"
"github.com/google/uuid"
)

type Local struct {
Expand All @@ -17,7 +19,7 @@ func New(directory string) *Local {
}
}

func (local *Local) Put(name string, payload []byte, _ string, _ http.ResponseWriter) ([]byte, error) {
func (local *Local) Put(name string, payload []byte, _ string, rw http.ResponseWriter) ([]byte, error) {
filePath := fmt.Sprintf("%s/%s", local.directory, name)
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
Expand All @@ -30,9 +32,14 @@ func (local *Local) Put(name string, payload []byte, _ string, _ http.ResponseWr
return nil, err
}
log.Debug(fmt.Sprintf("%q written", filePath))
rw.Header().Add("Location", name)
return []byte(fmt.Sprintf("%q written", filePath)), nil
}

func (local *Local) Post(path string, payload []byte, contentType string, rw http.ResponseWriter) ([]byte, error) {
return local.Put(path+"/"+uuid.NewString(), payload, contentType, rw)
}

func (local *Local) Get(name string, _ http.ResponseWriter) ([]byte, error) {
filePath := fmt.Sprintf("%s/%s", local.directory, name)
payload, err := os.ReadFile(filePath)
Expand Down
14 changes: 11 additions & 3 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"context"
"fmt"
"github.com/bluecatengineering/traefik-aws-plugin/ecs"
"github.com/bluecatengineering/traefik-aws-plugin/log"
"github.com/bluecatengineering/traefik-aws-plugin/signer"
"io"
"net/http"
"time"

"github.com/bluecatengineering/traefik-aws-plugin/ecs"
"github.com/bluecatengineering/traefik-aws-plugin/log"
"github.com/bluecatengineering/traefik-aws-plugin/signer"
"github.com/google/uuid"
)

type S3 struct {
Expand Down Expand Up @@ -68,14 +70,20 @@ func (s3 *S3) request(httpMethod string, name string, payload []byte, contentTyp
if err != nil {
log.Error(fmt.Sprintf("Reading S3 response body failed: %q", err.Error()))
}
resp.Header.Add("Location", s3.prefix+"/"+name)
copyHeader(rw.Header(), resp.Header)

return response, nil
}

func (s3 *S3) Put(name string, payload []byte, contentType string, rw http.ResponseWriter) ([]byte, error) {
return s3.request(http.MethodPut, name, payload, contentType, rw)
}

func (s3 *S3) Post(path string, payload []byte, contentType string, rw http.ResponseWriter) ([]byte, error) {
return s3.Put(path+"/"+uuid.NewString(), payload, contentType, rw)
}

func (s3 *S3) Get(name string, rw http.ResponseWriter) ([]byte, error) {
return s3.request(http.MethodGet, name, nil, "", rw)
}
Expand Down
41 changes: 41 additions & 0 deletions vendor/github.com/google/uuid/CHANGELOG.md

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

26 changes: 26 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

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

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

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

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

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

21 changes: 21 additions & 0 deletions vendor/github.com/google/uuid/README.md

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

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

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

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

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

Loading

0 comments on commit feef16f

Please sign in to comment.