mirror of
https://github.com/gofiber/storage.git
synced 2025-09-27 21:02:20 +08:00
Compare commits
204 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
20ba96f664 | ||
![]() |
e8a33f952c | ||
![]() |
c850eaff51 | ||
![]() |
2bd20cfa5a | ||
![]() |
2ee234e829 | ||
![]() |
1137fb9036 | ||
![]() |
a9dc413939 | ||
![]() |
825eb938c2 | ||
![]() |
e5eb169055 | ||
![]() |
4e6f1c95ff | ||
![]() |
c010712741 | ||
![]() |
161724ceae | ||
![]() |
6627d9d1ee | ||
![]() |
d533947dca | ||
![]() |
595b1917f3 | ||
![]() |
f112c6ddff | ||
![]() |
070dec8c74 | ||
![]() |
8125d5ae39 | ||
![]() |
45e450a3d8 | ||
![]() |
737624d914 | ||
![]() |
1401f6b804 | ||
![]() |
8c679bf3a2 | ||
![]() |
fc7eb4fd27 | ||
![]() |
7486445385 | ||
![]() |
dfdfd7fbf3 | ||
![]() |
87862b1ac6 | ||
![]() |
bbfc356c01 | ||
![]() |
d8d446b1a5 | ||
![]() |
6ba900f18c | ||
![]() |
dc7bef5d10 | ||
![]() |
dff3b2aa26 | ||
![]() |
560664ab5a | ||
![]() |
68c1b99eac | ||
![]() |
998df25440 | ||
![]() |
b59617e2a6 | ||
![]() |
c6162ed666 | ||
![]() |
31f94813cf | ||
![]() |
1f256f5391 | ||
![]() |
a299404c1b | ||
![]() |
72b47afe1e | ||
![]() |
ce744287b7 | ||
![]() |
9f14918a5a | ||
![]() |
11aacc8bd1 | ||
![]() |
01da118ae2 | ||
![]() |
94d968b7f7 | ||
![]() |
57c24a378b | ||
![]() |
fdea0f9267 | ||
![]() |
58364534e9 | ||
![]() |
bc10416921 | ||
![]() |
c30115270c | ||
![]() |
2c315e1ce7 | ||
![]() |
ff64bffed1 | ||
![]() |
34200e124b | ||
![]() |
20a6c8a79b | ||
![]() |
d2569fbe93 | ||
![]() |
bf76d31f6c | ||
![]() |
551c37101c | ||
![]() |
23f59994f8 | ||
![]() |
109c6b6f4f | ||
![]() |
fc6b3c841b | ||
![]() |
0b3e720b84 | ||
![]() |
7bcf46ac24 | ||
![]() |
0b31353707 | ||
![]() |
872f038e0b | ||
![]() |
b08cc03b22 | ||
![]() |
328c5ce28f | ||
![]() |
3e54ad6cac | ||
![]() |
7990d6d708 | ||
![]() |
95f32bf522 | ||
![]() |
95f98b0c41 | ||
![]() |
b0af7b2d2e | ||
![]() |
51f7507c86 | ||
![]() |
0965ae08a9 | ||
![]() |
2e6aade129 | ||
![]() |
7d29b5bbf0 | ||
![]() |
6666191087 | ||
![]() |
43b37e9b7e | ||
![]() |
6c53e5e46e | ||
![]() |
d178fca7df | ||
![]() |
230c3a6c5b | ||
![]() |
9b85e40cc6 | ||
![]() |
e38e2dcbba | ||
![]() |
a5d0a37b49 | ||
![]() |
808d103448 | ||
![]() |
30e0b8afa8 | ||
![]() |
da887624a4 | ||
![]() |
5eb8d57c09 | ||
![]() |
89493b1be0 | ||
![]() |
7ad1f0f105 | ||
![]() |
ffa78322a4 | ||
![]() |
ad9cf6da5d | ||
![]() |
a5a18bb80b | ||
![]() |
81764f4072 | ||
![]() |
327c871825 | ||
![]() |
4126450357 | ||
![]() |
8e81d7cded | ||
![]() |
94977c4028 | ||
![]() |
278f9d748a | ||
![]() |
d5f9d825aa | ||
![]() |
3a4eb3934d | ||
![]() |
d0b6aa2131 | ||
![]() |
310758fc7a | ||
![]() |
c1ab36765b | ||
![]() |
f24af60e42 | ||
![]() |
7d0bbde770 | ||
![]() |
efffcc135b | ||
![]() |
1b233673bb | ||
![]() |
07bde5f757 | ||
![]() |
ce174aed64 | ||
![]() |
cff33471cf | ||
![]() |
e211b33874 | ||
![]() |
05c149e84f | ||
![]() |
bf50899345 | ||
![]() |
b2f8339751 | ||
![]() |
e0435cd1a0 | ||
![]() |
d03bf4584c | ||
![]() |
e40f7b86df | ||
![]() |
3a8b8d4f71 | ||
![]() |
c38c925975 | ||
![]() |
d27ddd4739 | ||
![]() |
0aec65bb77 | ||
![]() |
671a3e4a04 | ||
![]() |
cbc4d3d251 | ||
![]() |
fe8cc5f97c | ||
![]() |
af3630ee22 | ||
![]() |
602260698d | ||
![]() |
ad27e8bcbb | ||
![]() |
d4ddc46c29 | ||
![]() |
ac29a7e86c | ||
![]() |
daa2dc7ee4 | ||
![]() |
1e822400d5 | ||
![]() |
57f7bb86d5 | ||
![]() |
ba65508bc9 | ||
![]() |
30e23d86d0 | ||
![]() |
7c79c78623 | ||
![]() |
caf28d82f7 | ||
![]() |
28ae1f71fb | ||
![]() |
bdea86b669 | ||
![]() |
2ec5a2cf70 | ||
![]() |
841d5eaedd | ||
![]() |
9ee4838edf | ||
![]() |
12203cbbe8 | ||
![]() |
e2ce496727 | ||
![]() |
4c11a63573 | ||
![]() |
725abe0123 | ||
![]() |
81c40a28df | ||
![]() |
2465b14c41 | ||
![]() |
d002c1076e | ||
![]() |
c3e5299489 | ||
![]() |
2d113619b3 | ||
![]() |
2fdd8600ad | ||
![]() |
dbc7a0636d | ||
![]() |
382fd19d57 | ||
![]() |
364ffb1dda | ||
![]() |
b787cedd82 | ||
![]() |
2dd7251e59 | ||
![]() |
b6574fdbfa | ||
![]() |
adcd9c09c3 | ||
![]() |
e8a6d1b229 | ||
![]() |
6d96fb56af | ||
![]() |
6fd94ebd5a | ||
![]() |
0c0027d11d | ||
![]() |
07d3c6b7c1 | ||
![]() |
8be14c8950 | ||
![]() |
2f13554ac3 | ||
![]() |
3d96c04300 | ||
![]() |
debcd26997 | ||
![]() |
96284ee8b7 | ||
![]() |
a1f5f8b1b0 | ||
![]() |
6ea9ef03b5 | ||
![]() |
663d7da0e7 | ||
![]() |
3432c9df7b | ||
![]() |
46d63d36ec | ||
![]() |
035b445a98 | ||
![]() |
ef2bde0f4a | ||
![]() |
b2e9530bc7 | ||
![]() |
bd65618ff5 | ||
![]() |
6420cf2823 | ||
![]() |
2f656df41c | ||
![]() |
83339bc156 | ||
![]() |
e464f894c6 | ||
![]() |
a5b31b013b | ||
![]() |
0dd7781663 | ||
![]() |
771b96b211 | ||
![]() |
aeda350995 | ||
![]() |
551f99d5d2 | ||
![]() |
2aa051f366 | ||
![]() |
7d95149519 | ||
![]() |
1c100fe50a | ||
![]() |
395ea51b28 | ||
![]() |
d327e9b0dd | ||
![]() |
5cbf98de49 | ||
![]() |
22444f8aa9 | ||
![]() |
cc5ccf062b | ||
![]() |
315f14ce58 | ||
![]() |
f9eaa6ae4d | ||
![]() |
2be6e0cc10 | ||
![]() |
5b444c36df | ||
![]() |
61fa9709f2 | ||
![]() |
45cc6b9dcd | ||
![]() |
e7293b3841 | ||
![]() |
69b0b5a26f | ||
![]() |
edb920dc05 | ||
![]() |
df9fdcbaf9 |
78
.github/dependabot.yml
vendored
78
.github/dependabot.yml
vendored
@@ -2,111 +2,93 @@
|
|||||||
|
|
||||||
version: 2
|
version: 2
|
||||||
updates:
|
updates:
|
||||||
|
- package-ecosystem: "github-actions"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: daily
|
||||||
|
labels:
|
||||||
|
- "🤖 Dependencies"
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/" # Location of package manifests
|
directory: "/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/arangodb/" # Location of package manifests
|
directory: "/arangodb/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/badger/" # Location of package manifests
|
directory: "/badger/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/dynamodb/" # Location of package manifests
|
directory: "/dynamodb/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/memcache/" # Location of package manifests
|
directory: "/memcache/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/memory/" # Location of package manifests
|
directory: "/memory/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/mongodb/" # Location of package manifests
|
directory: "/mongodb/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/mysql/" # Location of package manifests
|
directory: "/mysql/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/postgres/" # Location of package manifests
|
directory: "/postgres/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/redis/" # Location of package manifests
|
directory: "/redis/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/sqlite3/" # Location of package manifests
|
directory: "/sqlite3/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/ristretto/" # Location of package manifests
|
directory: "/ristretto/" # Location of package manifests
|
||||||
default_labels:
|
labels:
|
||||||
|
- "🤖 Dependencies"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
|
- package-ecosystem: "gomod"
|
||||||
|
directory: "/s3/" # Location of package manifests
|
||||||
|
labels:
|
||||||
|
- "🤖 Dependencies"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
|
- package-ecosystem: "gomod"
|
||||||
|
directory: "/bbolt/" # Location of package manifests
|
||||||
|
labels:
|
||||||
- "🤖 Dependencies"
|
- "🤖 Dependencies"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
automerged_updates:
|
|
||||||
- match:
|
|
||||||
dependency_name: "gofiber/fiber/*"
|
|
||||||
|
3
.github/release-drafter.yml
vendored
3
.github/release-drafter.yml
vendored
@@ -16,6 +16,7 @@ categories:
|
|||||||
- '📒 Documentation'
|
- '📒 Documentation'
|
||||||
change-template: '- $TITLE (#$NUMBER)'
|
change-template: '- $TITLE (#$NUMBER)'
|
||||||
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
|
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
|
||||||
|
sort-by: 'title'
|
||||||
version-resolver:
|
version-resolver:
|
||||||
major:
|
major:
|
||||||
labels:
|
labels:
|
||||||
@@ -90,4 +91,6 @@ autolabeler:
|
|||||||
template: |
|
template: |
|
||||||
$CHANGES
|
$CHANGES
|
||||||
|
|
||||||
|
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION
|
||||||
|
|
||||||
Thank you $CONTRIBUTORS for making this update possible.
|
Thank you $CONTRIBUTORS for making this update possible.
|
||||||
|
8
.github/workflows/codeql-analysis.yml
vendored
8
.github/workflows/codeql-analysis.yml
vendored
@@ -16,7 +16,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
# We must fetch at least the immediate parents so that if this is
|
# We must fetch at least the immediate parents so that if this is
|
||||||
# a pull request then we can checkout the head.
|
# a pull request then we can checkout the head.
|
||||||
@@ -29,7 +29,7 @@ jobs:
|
|||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@v1
|
uses: github/codeql-action/init@v2
|
||||||
# Override language selection by uncommenting this and choosing your languages
|
# Override language selection by uncommenting this and choosing your languages
|
||||||
with:
|
with:
|
||||||
languages: go
|
languages: go
|
||||||
@@ -37,7 +37,7 @@ jobs:
|
|||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: Autobuild
|
- name: Autobuild
|
||||||
uses: github/codeql-action/autobuild@v1
|
uses: github/codeql-action/autobuild@v2
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
# 📚 https://git.io/JvXDl
|
# 📚 https://git.io/JvXDl
|
||||||
@@ -51,4 +51,4 @@ jobs:
|
|||||||
# make release
|
# make release
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@v1
|
uses: github/codeql-action/analyze@v2
|
||||||
|
39
.github/workflows/dependabot_automerge.yml
vendored
Normal file
39
.github/workflows/dependabot_automerge.yml
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
name: Dependabot auto-merge
|
||||||
|
on: pull_request
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
wait_for_checks:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Wait for check is finished
|
||||||
|
uses: lewagon/wait-on-check-action@v1.1.2
|
||||||
|
id: wait_for_checks
|
||||||
|
with:
|
||||||
|
ref: ${{ github.event.pull_request.head.sha || github.sha }}
|
||||||
|
running-workflow-name: wait_for_checks
|
||||||
|
check-regexp: Tests
|
||||||
|
repo-token: ${{ secrets.PR_TOKEN }}
|
||||||
|
wait-interval: 10
|
||||||
|
dependabot:
|
||||||
|
needs: [wait_for_checks]
|
||||||
|
name: Dependabot auto-merge
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: ${{ github.actor == 'dependabot[bot]' }}
|
||||||
|
steps:
|
||||||
|
- name: Dependabot metadata
|
||||||
|
id: metadata
|
||||||
|
uses: dependabot/fetch-metadata@v1.3.4
|
||||||
|
with:
|
||||||
|
github-token: "${{ secrets.PR_TOKEN }}"
|
||||||
|
- name: Enable auto-merge for Dependabot PRs
|
||||||
|
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor' || steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
|
||||||
|
run: |
|
||||||
|
gh pr review --approve "$PR_URL"
|
||||||
|
gh pr merge --auto --merge "$PR_URL"
|
||||||
|
env:
|
||||||
|
PR_URL: ${{github.event.pull_request.html_url}}
|
||||||
|
GITHUB_TOKEN: ${{secrets.PR_TOKEN}}
|
4
.github/workflows/linter.yml
vendored
4
.github/workflows/linter.yml
vendored
@@ -5,8 +5,8 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Fetch Repository
|
- name: Fetch Repository
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: Run Golint
|
- name: Run Golint
|
||||||
uses: reviewdog/action-golangci-lint@v1
|
uses: reviewdog/action-golangci-lint@v2
|
||||||
with:
|
with:
|
||||||
golangci_lint_flags: "--tests=false"
|
golangci_lint_flags: "--tests=false"
|
||||||
|
16
.github/workflows/security.yml
vendored
16
.github/workflows/security.yml
vendored
@@ -5,18 +5,18 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Fetch Repository
|
- name: Fetch Repository
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: '^1.17.6'
|
go-version: '^1.17.6'
|
||||||
- name: Install Gosec
|
- name: Install Gosec
|
||||||
run: |
|
run: |
|
||||||
export PATH=${PATH}:`go env GOPATH`/bin
|
export PATH=${PATH}:`go env GOPATH`/bin
|
||||||
go get -u github.com/securego/gosec/v2/cmd/gosec
|
go install github.com/securego/gosec/v2/cmd/gosec@latest
|
||||||
- name: Run Gosec (root)
|
- name: Run Gosec (root)
|
||||||
working-directory: .
|
working-directory: .
|
||||||
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal -exclude-dir=arangodb -exclude-dir=badger -exclude-dir=dynamodb -exclude-dir=memcache -exclude-dir=memory -exclude-dir=mongodb -exclude-dir=mysql -exclude-dir=postgres -exclude-dir=redis -exclude-dir=ristretto -exclude-dir=sqlite3 ./..."
|
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal -exclude-dir=arangodb -exclude-dir=badger -exclude-dir=dynamodb -exclude-dir=memcache -exclude-dir=memory -exclude-dir=mongodb -exclude-dir=mysql -exclude-dir=postgres -exclude-dir=redis -exclude-dir=ristretto -exclude-dir=sqlite3 -exclude-dir=s3 -exclude-dir=bbolt ./..."
|
||||||
# -----
|
# -----
|
||||||
- name: Run Gosec (arangodb)
|
- name: Run Gosec (arangodb)
|
||||||
working-directory: ./arangodb
|
working-directory: ./arangodb
|
||||||
@@ -58,7 +58,15 @@ jobs:
|
|||||||
working-directory: ./sqlite3
|
working-directory: ./sqlite3
|
||||||
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
|
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
|
||||||
# -----
|
# -----
|
||||||
|
- name: Run Gosec (s3)
|
||||||
|
working-directory: ./s3
|
||||||
|
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
|
||||||
|
# -----
|
||||||
- name: Run Gosec (ristretto)
|
- name: Run Gosec (ristretto)
|
||||||
working-directory: ./ristretto
|
working-directory: ./ristretto
|
||||||
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
|
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
|
||||||
# -----
|
# -----
|
||||||
|
- name: Run Gosec (bbolt)
|
||||||
|
working-directory: ./bbolt
|
||||||
|
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
|
||||||
|
# -----
|
||||||
|
82
.github/workflows/test-arangodb.yml
vendored
82
.github/workflows/test-arangodb.yml
vendored
@@ -1,33 +1,51 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: ArangoDB
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests ArangoDB"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
arangodb:
|
arangodb:
|
||||||
image: 'arangodb:latest'
|
image: 'arangodb:latest'
|
||||||
env:
|
env:
|
||||||
ARANGO_NO_AUTH: 1
|
ARANGO_NO_AUTH: 1
|
||||||
ports:
|
ports:
|
||||||
- '8529:8529'
|
- '8529:8529'
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
steps:
|
||||||
steps:
|
- name: Install Go
|
||||||
- name: Install Go
|
uses: actions/setup-go@v3
|
||||||
uses: actions/setup-go@v1
|
with:
|
||||||
with:
|
go-version: '${{ matrix.go-version }}'
|
||||||
go-version: '${{ matrix.go-version }}'
|
- name: Setup Golang caches
|
||||||
- name: Fetch Repository
|
uses: actions/cache@v3
|
||||||
uses: actions/checkout@v2
|
with:
|
||||||
- name: Run Test
|
# In order:
|
||||||
run: cd ./arangodb && go test ./... -v -race
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./arangodb && go test ./... -v -race
|
||||||
|
44
.github/workflows/test-bbolt.yml
vendored
Normal file
44
.github/workflows/test-bbolt.yml
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Bbolt"
|
||||||
|
jobs:
|
||||||
|
Tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version:
|
||||||
|
- 1.14.x
|
||||||
|
- 1.16.x
|
||||||
|
- 1.19.x
|
||||||
|
platform:
|
||||||
|
- ubuntu-latest
|
||||||
|
- windows-latest
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '${{ matrix.go-version }}'
|
||||||
|
- name: Setup Golang caches
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
# In order:
|
||||||
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./bbolt && go mod tidy && go test ./... -v -race
|
49
.github/workflows/test-dynamodb.yml
vendored
Normal file
49
.github/workflows/test-dynamodb.yml
vendored
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests DynamoDB"
|
||||||
|
jobs:
|
||||||
|
Tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
services:
|
||||||
|
mongo:
|
||||||
|
image: 'amazon/dynamodb-local:latest'
|
||||||
|
ports:
|
||||||
|
- '8000:8000'
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version:
|
||||||
|
- 1.14.x
|
||||||
|
- 1.16.x
|
||||||
|
- 1.19.x
|
||||||
|
platform:
|
||||||
|
- ubuntu-latest
|
||||||
|
- windows-latest
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '${{ matrix.go-version }}'
|
||||||
|
- name: Setup Golang caches
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
# In order:
|
||||||
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./dynamodb && go test ./... -v -race
|
78
.github/workflows/test-memcache.yml
vendored
78
.github/workflows/test-memcache.yml
vendored
@@ -1,31 +1,49 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: Memcache
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Memcache"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
mongo:
|
mongo:
|
||||||
image: 'memcached:latest'
|
image: 'memcached:latest'
|
||||||
ports:
|
ports:
|
||||||
- '11211:11211'
|
- '11211:11211'
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
steps:
|
||||||
steps:
|
- name: Install Go
|
||||||
- name: Install Go
|
uses: actions/setup-go@v3
|
||||||
uses: actions/setup-go@v1
|
with:
|
||||||
with:
|
go-version: '${{ matrix.go-version }}'
|
||||||
go-version: '${{ matrix.go-version }}'
|
- name: Setup Golang caches
|
||||||
- name: Fetch Repository
|
uses: actions/cache@v3
|
||||||
uses: actions/checkout@v2
|
with:
|
||||||
- name: Run Test
|
# In order:
|
||||||
run: cd ./memcache && go test ./... -v -race
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./memcache && go test ./... -v -race
|
||||||
|
78
.github/workflows/test-mongodb.yml
vendored
78
.github/workflows/test-mongodb.yml
vendored
@@ -1,31 +1,49 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: MongoDB
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Mongodb"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
mongo:
|
mongo:
|
||||||
image: 'mongo:latest'
|
image: 'mongo:latest'
|
||||||
ports:
|
ports:
|
||||||
- '27017:27017'
|
- '27017:27017'
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
steps:
|
||||||
steps:
|
- name: Install Go
|
||||||
- name: Install Go
|
uses: actions/setup-go@v3
|
||||||
uses: actions/setup-go@v1
|
with:
|
||||||
with:
|
go-version: '${{ matrix.go-version }}'
|
||||||
go-version: '${{ matrix.go-version }}'
|
- name: Setup Golang caches
|
||||||
- name: Fetch Repository
|
uses: actions/cache@v3
|
||||||
uses: actions/checkout@v2
|
with:
|
||||||
- name: Run Test
|
# In order:
|
||||||
run: cd ./mongodb && go test ./... -v -race
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./mongodb && go test ./... -v -race
|
||||||
|
102
.github/workflows/test-mysql.yml
vendored
102
.github/workflows/test-mysql.yml
vendored
@@ -1,43 +1,61 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: MySQL
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests MySQL"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
mysql:
|
mysql:
|
||||||
image: 'mysql:latest'
|
image: 'mysql:latest'
|
||||||
env:
|
env:
|
||||||
MYSQL_DATABASE: fiber
|
MYSQL_DATABASE: fiber
|
||||||
MYSQL_USER: username
|
MYSQL_USER: username
|
||||||
MYSQL_PASSWORD: password
|
MYSQL_PASSWORD: password
|
||||||
MYSQL_ROOT_PASSWORD: password
|
MYSQL_ROOT_PASSWORD: password
|
||||||
ports:
|
ports:
|
||||||
- '3306:3306'
|
- '3306:3306'
|
||||||
options: >-
|
options: >-
|
||||||
--health-cmd "mysqladmin ping" --health-interval 10s --health-timeout
|
--health-cmd "mysqladmin ping" --health-interval 10s --health-timeout
|
||||||
5s --health-retries 5
|
5s --health-retries 5
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
steps:
|
||||||
steps:
|
- name: Install Go
|
||||||
- name: Install Go
|
uses: actions/setup-go@v3
|
||||||
uses: actions/setup-go@v1
|
with:
|
||||||
with:
|
go-version: '${{ matrix.go-version }}'
|
||||||
go-version: '${{ matrix.go-version }}'
|
- name: Setup Golang caches
|
||||||
- name: Fetch Repository
|
uses: actions/cache@v3
|
||||||
uses: actions/checkout@v2
|
with:
|
||||||
- name: Run Test
|
# In order:
|
||||||
run: cd ./mysql && go test ./... -v -race
|
# * Module download cache
|
||||||
env:
|
# * Build cache (Linux)
|
||||||
MYSQL_USERNAME: username
|
# * Build cache (Mac)
|
||||||
MYSQL_PASSWORD: password
|
# * Build cache (Windows)
|
||||||
MYSQL_DATABASE: fiber
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./mysql && go test ./... -v -race
|
||||||
|
env:
|
||||||
|
MYSQL_USERNAME: username
|
||||||
|
MYSQL_PASSWORD: password
|
||||||
|
MYSQL_DATABASE: fiber
|
||||||
|
100
.github/workflows/test-postgres.yml
vendored
100
.github/workflows/test-postgres.yml
vendored
@@ -1,42 +1,60 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: Postgres
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Postgres"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: 'postgres:latest'
|
image: 'postgres:latest'
|
||||||
ports:
|
ports:
|
||||||
- '5432:5432'
|
- '5432:5432'
|
||||||
env:
|
env:
|
||||||
POSTGRES_DB: fiber
|
POSTGRES_DB: fiber
|
||||||
POSTGRES_USER: username
|
POSTGRES_USER: username
|
||||||
POSTGRES_PASSWORD: password
|
POSTGRES_PASSWORD: password
|
||||||
options: >-
|
options: >-
|
||||||
--health-cmd pg_isready --health-interval 10s --health-timeout 5s
|
--health-cmd pg_isready --health-interval 10s --health-timeout 5s
|
||||||
--health-retries 5
|
--health-retries 5
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
steps:
|
||||||
steps:
|
- name: Install Go
|
||||||
- name: Install Go
|
uses: actions/setup-go@v3
|
||||||
uses: actions/setup-go@v1
|
with:
|
||||||
with:
|
go-version: '${{ matrix.go-version }}'
|
||||||
go-version: '${{ matrix.go-version }}'
|
- name: Setup Golang caches
|
||||||
- name: Fetch Repository
|
uses: actions/cache@v3
|
||||||
uses: actions/checkout@v2
|
with:
|
||||||
- name: Run Test
|
# In order:
|
||||||
run: cd ./postgres && go test ./... -v -race
|
# * Module download cache
|
||||||
env:
|
# * Build cache (Linux)
|
||||||
POSTGRES_DATABASE: fiber
|
# * Build cache (Mac)
|
||||||
POSTGRES_USERNAME: username
|
# * Build cache (Windows)
|
||||||
POSTGRES_PASSWORD: password
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./postgres && go test ./... -v -race
|
||||||
|
env:
|
||||||
|
POSTGRES_DATABASE: fiber
|
||||||
|
POSTGRES_USERNAME: username
|
||||||
|
POSTGRES_PASSWORD: password
|
||||||
|
97
.github/workflows/test-redis.yml
vendored
97
.github/workflows/test-redis.yml
vendored
@@ -1,43 +1,62 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: Redis
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Redis"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
steps:
|
||||||
steps:
|
- name: Fetch Repository
|
||||||
- name: Fetch Repository
|
uses: actions/checkout@v3
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Setup Redis
|
- name: Setup Redis
|
||||||
uses: shogo82148/actions-setup-redis@v1
|
uses: shogo82148/actions-setup-redis@v1
|
||||||
with:
|
with:
|
||||||
redis-version: 'latest'
|
redis-version: '6.x'
|
||||||
auto-start: 'false'
|
auto-start: 'false'
|
||||||
redis-port: '6379'
|
redis-port: '6379'
|
||||||
redis-tls-port: '6380'
|
redis-tls-port: '6380'
|
||||||
|
|
||||||
- name: Run Redis
|
- name: Run Redis
|
||||||
run: |
|
run: |
|
||||||
redis-server --tls-port 6380 --port 6379 \
|
redis-server --tls-port 6380 --port 6379 \
|
||||||
--tls-cert-file ./redis/tests/tls/redis.crt \
|
--tls-cert-file ./redis/tests/tls/redis.crt \
|
||||||
--tls-key-file ./redis/tests/tls/redis.key \
|
--tls-key-file ./redis/tests/tls/redis.key \
|
||||||
--tls-ca-cert-file ./redis/tests/tls/ca.crt&
|
--tls-ca-cert-file ./redis/tests/tls/ca.crt&
|
||||||
|
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: '${{ matrix.go-version }}'
|
go-version: '${{ matrix.go-version }}'
|
||||||
|
|
||||||
- name: Run Test
|
- name: Setup Golang caches
|
||||||
run: cd ./redis && go test ./... -v -race
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
# In order:
|
||||||
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./redis && go test ./... -v -race
|
||||||
|
66
.github/workflows/test-ristretto.yml
vendored
66
.github/workflows/test-ristretto.yml
vendored
@@ -1,24 +1,44 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: Ristretto
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Ristretto"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.16.x
|
- 1.14.x
|
||||||
- 1.17.x
|
- 1.16.x
|
||||||
platform:
|
- 1.19.x
|
||||||
- ubuntu-latest
|
platform:
|
||||||
- windows-latest
|
- ubuntu-latest
|
||||||
steps:
|
- windows-latest
|
||||||
- name: Install Go
|
steps:
|
||||||
uses: actions/setup-go@v1
|
- name: Install Go
|
||||||
with:
|
uses: actions/setup-go@v3
|
||||||
go-version: '${{ matrix.go-version }}'
|
with:
|
||||||
- name: Fetch Repository
|
go-version: '${{ matrix.go-version }}'
|
||||||
uses: actions/checkout@v2
|
- name: Setup Golang caches
|
||||||
- name: Run Test
|
uses: actions/cache@v3
|
||||||
run: cd ./ristretto && go mod tidy && go test ./... -v -race
|
with:
|
||||||
|
# In order:
|
||||||
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./ristretto && go mod tidy && go test ./... -v -race
|
||||||
|
53
.github/workflows/test-s3.yml
vendored
Normal file
53
.github/workflows/test-s3.yml
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests S3"
|
||||||
|
jobs:
|
||||||
|
Tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version:
|
||||||
|
- 1.14.x
|
||||||
|
- 1.16.x
|
||||||
|
- 1.19.x
|
||||||
|
platform:
|
||||||
|
- ubuntu-latest
|
||||||
|
- windows-latest
|
||||||
|
steps:
|
||||||
|
- name: Install MinIO
|
||||||
|
run: |
|
||||||
|
docker run -d -p 9000:9000 --name minio minio/minio server /data
|
||||||
|
|
||||||
|
export AWS_ACCESS_KEY_ID=minioadmin
|
||||||
|
export AWS_SECRET_ACCESS_KEY=minioadmin
|
||||||
|
export AWS_EC2_METADATA_DISABLED=true
|
||||||
|
|
||||||
|
aws --endpoint-url http://127.0.0.1:9000/ s3 mb s3://testbucket
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '${{ matrix.go-version }}'
|
||||||
|
- name: Setup Golang caches
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
# In order:
|
||||||
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Test
|
||||||
|
run: cd ./s3 && go test ./... -v -race
|
81
.github/workflows/test.yml
vendored
81
.github/workflows/test.yml
vendored
@@ -1,30 +1,53 @@
|
|||||||
'on':
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
name: Local Storage
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
name: "Tests Local Storage"
|
||||||
jobs:
|
jobs:
|
||||||
Tests:
|
Tests:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go-version:
|
go-version:
|
||||||
- 1.14.x
|
- 1.14.x
|
||||||
- 1.15.x
|
- 1.16.x
|
||||||
- 1.16.x
|
- 1.19.x
|
||||||
- 1.17.x
|
platform:
|
||||||
platform:
|
- ubuntu-latest
|
||||||
- ubuntu-latest
|
- windows-latest
|
||||||
- windows-latest
|
runs-on: '${{ matrix.platform }}'
|
||||||
runs-on: '${{ matrix.platform }}'
|
steps:
|
||||||
steps:
|
- name: Install Go
|
||||||
- name: Install Go
|
uses: actions/setup-go@v3
|
||||||
uses: actions/setup-go@v1
|
with:
|
||||||
with:
|
go-version: '${{ matrix.go-version }}'
|
||||||
go-version: '${{ matrix.go-version }}'
|
- name: Setup Golang caches
|
||||||
- name: Fetch Repository
|
uses: actions/cache@v3
|
||||||
uses: actions/checkout@v2
|
with:
|
||||||
- name: Test Badger
|
# In order:
|
||||||
run: cd ./badger && go test ./... -v -race
|
# * Module download cache
|
||||||
- name: Test Memory
|
# * Build cache (Linux)
|
||||||
run: cd ./memory && go test ./... -v -race
|
# * Build cache (Mac)
|
||||||
- name: Test SQLite3
|
# * Build cache (Windows)
|
||||||
run: cd ./sqlite3 && go test ./... -v -race
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-${{ matrix.go-version }}-
|
||||||
|
|
||||||
|
- name: Fetch Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Test Badger
|
||||||
|
run: cd ./badger && go test ./... -v -race
|
||||||
|
- name: Test Memory
|
||||||
|
run: cd ./memory && go test ./... -v -race
|
||||||
|
- name: Test SQLite3 - with -race check
|
||||||
|
if: runner.os != 'Windows'
|
||||||
|
run: cd ./sqlite3 && go test ./... -v -race
|
||||||
|
- name: Test SQLite3 - without -race check
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
run: cd ./sqlite3 && go test ./... -v
|
||||||
|
@@ -57,6 +57,9 @@ type Storage interface {
|
|||||||
* [Badger](/badger) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
|
* [Badger](/badger) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
|
||||||
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
||||||
</a>
|
</a>
|
||||||
|
* [Bbolt](/bbolt) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Bbolt%22">
|
||||||
|
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Bbolt?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
||||||
|
</a>
|
||||||
* [DynamoDB](/dynamodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22DynamoDB%22">
|
* [DynamoDB](/dynamodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22DynamoDB%22">
|
||||||
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/DynamoDB?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/DynamoDB?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
||||||
</a>
|
</a>
|
||||||
@@ -81,3 +84,6 @@ type Storage interface {
|
|||||||
* [SQLite3](/sqlite3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
|
* [SQLite3](/sqlite3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
|
||||||
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
||||||
</a>
|
</a>
|
||||||
|
* [S3](/s3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22S3%22">
|
||||||
|
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/S3?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
|
||||||
|
</a>
|
@@ -16,6 +16,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() driver.Client
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
ArangoDB is tested on the 2 last (1.14/1.15) [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
ArangoDB is tested on the 2 last (1.14/1.15) [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
|
@@ -246,3 +246,8 @@ func (s *Storage) gc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() driver.Client {
|
||||||
|
return s.client
|
||||||
|
}
|
||||||
|
@@ -132,3 +132,7 @@ func Test_ARANGODB_Non_UTF8(t *testing.T) {
|
|||||||
func Test_ARANGODB_Close(t *testing.T) {
|
func Test_ARANGODB_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_ARANGODB_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
@@ -3,6 +3,6 @@ module github.com/gofiber/storage/arangodb
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/arangodb/go-driver v1.2.1
|
github.com/arangodb/go-driver v1.4.0
|
||||||
github.com/gofiber/utils v0.1.2
|
github.com/gofiber/utils v0.1.2
|
||||||
)
|
)
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
github.com/arangodb/go-driver v1.2.1 h1:HREDHhDmzdIWxHmfkfTESbYUnRjESjPh4WUuXq7FZa8=
|
github.com/arangodb/go-driver v1.4.0 h1:uNCbVYkr5ZP3hIVUP6wqjOVyhMYOL9NDmR762tIeYP0=
|
||||||
github.com/arangodb/go-driver v1.2.1/go.mod h1:zdDkJJnCj8DAkfbtIjIXnsTrWIiy6VhP3Vy14p+uQeY=
|
github.com/arangodb/go-driver v1.4.0/go.mod h1:5GAx3XvK72DJPhJgyjZOtYAGc4SpY7rZDb3LyhCvLcQ=
|
||||||
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e h1:Xg+hGrY2LcQBbxd0ZFdbGSyRKTYMZCfBbw/pMJFOk1g=
|
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e h1:Xg+hGrY2LcQBbxd0ZFdbGSyRKTYMZCfBbw/pMJFOk1g=
|
||||||
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e/go.mod h1:mq7Shfa/CaixoDxiyAAc5jZ6CVBAyPaNQCGS7mkj4Ho=
|
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e/go.mod h1:mq7Shfa/CaixoDxiyAAc5jZ6CVBAyPaNQCGS7mkj4Ho=
|
||||||
github.com/coreos/go-iptables v0.4.3/go.mod h1:/mVI274lEDI2ns62jHCDnCyBF9Iwsmekav8Dbxlm1MU=
|
github.com/coreos/go-iptables v0.4.3/go.mod h1:/mVI274lEDI2ns62jHCDnCyBF9Iwsmekav8Dbxlm1MU=
|
||||||
|
@@ -19,6 +19,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *badger.DB
|
||||||
```
|
```
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
@@ -123,3 +123,8 @@ func (s *Storage) gc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *badger.DB {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -119,3 +119,7 @@ func Test_Badger_Reset(t *testing.T) {
|
|||||||
func Test_Badger_Close(t *testing.T) {
|
func Test_Badger_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Badger_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
93
bbolt/README.md
Normal file
93
bbolt/README.md
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# Bbolt
|
||||||
|
A Bbolt storage driver using [etcd-io/bbolt](https://github.com/etcd-io/bbolt). Bolt is a pure Go key/value store inspired by [Howard Chu's](https://twitter.com/hyc_symas) [LMDB project](https://www.symas.com/symas-embedded-database-lmdb). The goal of the project is to provide a simple, fast, and reliable database for projects that don't require a full database server such as Postgres or MySQL.
|
||||||
|
|
||||||
|
|
||||||
|
### Table of Contents
|
||||||
|
- [Signatures](#signatures)
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Config](#config)
|
||||||
|
- [Default Config](#default-config)
|
||||||
|
|
||||||
|
### Signatures
|
||||||
|
```go
|
||||||
|
func New(config ...Config) Storage
|
||||||
|
func (s *Storage) Get(key string) ([]byte, error)
|
||||||
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
||||||
|
func (s *Storage) Delete(key string) error
|
||||||
|
func (s *Storage) Reset() error
|
||||||
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *bbolt.DB
|
||||||
|
```
|
||||||
|
### Installation
|
||||||
|
Bbolt is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
|
```bash
|
||||||
|
go mod init github.com/<user>/<repo>
|
||||||
|
```
|
||||||
|
And then install the s3 implementation:
|
||||||
|
```bash
|
||||||
|
go get github.com/gofiber/storage/bbolt
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Import the storage package.
|
||||||
|
```go
|
||||||
|
import "github.com/gofiber/storage/bbolt"
|
||||||
|
```
|
||||||
|
|
||||||
|
You can use the following possibilities to create a storage:
|
||||||
|
```go
|
||||||
|
// Initialize default config
|
||||||
|
store := bbolt.New()
|
||||||
|
|
||||||
|
// Initialize custom config
|
||||||
|
store := bbolt.New(bbolt.Config{
|
||||||
|
Database: "my_database.db",
|
||||||
|
Bucket: "my-bucket",
|
||||||
|
Reset: false,
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Config
|
||||||
|
```go
|
||||||
|
// Config defines the config for storage.
|
||||||
|
type Config struct {
|
||||||
|
// Database path
|
||||||
|
//
|
||||||
|
// Optional. Default is "fiber.db"
|
||||||
|
Database string
|
||||||
|
|
||||||
|
// Bbolt bucket name
|
||||||
|
//
|
||||||
|
// Optional. Default is "fiber_storage"
|
||||||
|
Bucket string
|
||||||
|
|
||||||
|
// Timeout is the amount of time to wait to obtain a file lock.
|
||||||
|
// Only available on Darwin and Linux.
|
||||||
|
//
|
||||||
|
// Optional. Default is 60 * time.Second.
|
||||||
|
Timeout time.Duration
|
||||||
|
|
||||||
|
// Open database in read-only mode.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
ReadOnly bool
|
||||||
|
|
||||||
|
// Reset clears any existing keys in existing Bucket
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
Reset bool
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Default Config
|
||||||
|
```go
|
||||||
|
// ConfigDefault is the default config
|
||||||
|
var ConfigDefault = Config{
|
||||||
|
Database: "fiber.db",
|
||||||
|
Bucket: "fiber_storage",
|
||||||
|
Timeout: 60 * time.Second,
|
||||||
|
ReadOnly: false,
|
||||||
|
Reset: false,
|
||||||
|
}
|
||||||
|
```
|
111
bbolt/bbolt.go
Normal file
111
bbolt/bbolt.go
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package bbolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/utils"
|
||||||
|
"go.etcd.io/bbolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Storage interface that is implemented by storage providers
|
||||||
|
type Storage struct {
|
||||||
|
conn *bbolt.DB
|
||||||
|
bucket string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new storage
|
||||||
|
func New(config ...Config) *Storage {
|
||||||
|
// Set default config
|
||||||
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
|
conn, err := bbolt.Open(cfg.Database, 0666, &bbolt.Options{
|
||||||
|
Timeout: cfg.Timeout,
|
||||||
|
ReadOnly: cfg.ReadOnly,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset bucket if field selected
|
||||||
|
if cfg.Reset {
|
||||||
|
if err := removeBucket(cfg, conn); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create bucket if not exists
|
||||||
|
if err := createBucket(cfg, conn); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Storage{
|
||||||
|
conn: conn,
|
||||||
|
bucket: cfg.Bucket,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get value by key
|
||||||
|
func (s *Storage) Get(key string) ([]byte, error) {
|
||||||
|
if len(key) <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var value []byte
|
||||||
|
|
||||||
|
err := s.conn.View(func(tx *bbolt.Tx) error {
|
||||||
|
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
|
||||||
|
value = b.Get(utils.UnsafeBytes(key))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return value, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set key with value
|
||||||
|
func (s *Storage) Set(key string, value []byte, exp time.Duration) error {
|
||||||
|
if len(key) <= 0 || len(value) <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.conn.Update(func(tx *bbolt.Tx) error {
|
||||||
|
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
|
||||||
|
|
||||||
|
return b.Put(utils.UnsafeBytes(key), value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete entry by key
|
||||||
|
func (s *Storage) Delete(key string) error {
|
||||||
|
if len(key) <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.conn.Update(func(tx *bbolt.Tx) error {
|
||||||
|
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
|
||||||
|
|
||||||
|
return b.Delete(utils.UnsafeBytes(key))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset all entries
|
||||||
|
func (s *Storage) Reset() error {
|
||||||
|
return s.conn.Update(func(tx *bbolt.Tx) error {
|
||||||
|
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
|
||||||
|
|
||||||
|
return b.ForEach(func(k, _ []byte) error {
|
||||||
|
return b.Delete(k)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the database
|
||||||
|
func (s *Storage) Close() error {
|
||||||
|
return s.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *bbolt.DB {
|
||||||
|
return s.conn
|
||||||
|
}
|
101
bbolt/bbolt_test.go
Normal file
101
bbolt/bbolt_test.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package bbolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gofiber/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testStore = New()
|
||||||
|
|
||||||
|
func Test_Bbolt_Set(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Set_Override(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Get(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, val, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Get_NotExist(t *testing.T) {
|
||||||
|
|
||||||
|
result, err := testStore.Get("notexist")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Delete(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Delete(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Reset(t *testing.T) {
|
||||||
|
var (
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set("john1", val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Set("john2", val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Reset()
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get("john1")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
|
||||||
|
result, err = testStore.Get("john2")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Close(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Bbolt_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
63
bbolt/config.go
Normal file
63
bbolt/config.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package bbolt
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Config defines the config for storage.
|
||||||
|
type Config struct {
|
||||||
|
// Database path
|
||||||
|
//
|
||||||
|
// Optional. Default is "fiber.db"
|
||||||
|
Database string
|
||||||
|
|
||||||
|
// Bbolt bucket name
|
||||||
|
//
|
||||||
|
// Optional. Default is "fiber_storage"
|
||||||
|
Bucket string
|
||||||
|
|
||||||
|
// Timeout is the amount of time to wait to obtain a file lock.
|
||||||
|
// Only available on Darwin and Linux.
|
||||||
|
//
|
||||||
|
// Optional. Default is set to 60 * time.Second.
|
||||||
|
Timeout time.Duration
|
||||||
|
|
||||||
|
// Open database in read-only mode.
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
ReadOnly bool
|
||||||
|
|
||||||
|
// Reset clears any existing keys in existing Bucket
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
Reset bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigDefault is the default config
|
||||||
|
var ConfigDefault = Config{
|
||||||
|
Database: "fiber.db",
|
||||||
|
Bucket: "fiber_storage",
|
||||||
|
Timeout: 60 * time.Second,
|
||||||
|
ReadOnly: false,
|
||||||
|
Reset: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to set default values
|
||||||
|
func configDefault(config ...Config) Config {
|
||||||
|
// Return default config if nothing provided
|
||||||
|
if len(config) < 1 {
|
||||||
|
return ConfigDefault
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override default config
|
||||||
|
cfg := config[0]
|
||||||
|
|
||||||
|
// Set default values
|
||||||
|
if cfg.Database == "" {
|
||||||
|
cfg.Database = ConfigDefault.Database
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Bucket == "" {
|
||||||
|
cfg.Bucket = ConfigDefault.Bucket
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
10
bbolt/go.mod
Normal file
10
bbolt/go.mod
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
module github.com/gofiber/storage/bbolt
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gofiber/utils v0.1.2
|
||||||
|
go.etcd.io/bbolt v1.3.6
|
||||||
|
)
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect
|
7
bbolt/go.sum
Normal file
7
bbolt/go.sum
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
|
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
|
||||||
|
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
|
||||||
|
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE=
|
||||||
|
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
20
bbolt/utils.go
Normal file
20
bbolt/utils.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package bbolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/utils"
|
||||||
|
"go.etcd.io/bbolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createBucket(cfg Config, conn *bbolt.DB) error {
|
||||||
|
return conn.Update(func(tx *bbolt.Tx) error {
|
||||||
|
_, err := tx.CreateBucketIfNotExists(utils.UnsafeBytes(cfg.Bucket))
|
||||||
|
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeBucket(cfg Config, conn *bbolt.DB) error {
|
||||||
|
return conn.Update(func(tx *bbolt.Tx) error {
|
||||||
|
return tx.DeleteBucket(utils.UnsafeBytes(cfg.Bucket))
|
||||||
|
})
|
||||||
|
}
|
@@ -1,4 +1,7 @@
|
|||||||
# ⚠ DynamoDB is still in development, do not use in production!
|
# DynamoDB
|
||||||
|
A DynamoDB storage driver using [aws/aws-sdk-go-v2](https://github.com/aws/aws-sdk-go-v2).
|
||||||
|
|
||||||
|
**Note:** If config fields of credentials not given, credentials are using from the environment variables, ~/.aws/credentials, or EC2 instance role. If config fields of credentials given, credentials are using from config. Look at: [specifying credentials](https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials)
|
||||||
|
|
||||||
....
|
....
|
||||||
|
|
||||||
@@ -20,6 +23,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *awsdynamodb.Client
|
||||||
```
|
```
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
@@ -60,28 +64,70 @@ type Config struct {
|
|||||||
// Optional ("fiber_storage" by default).
|
// Optional ("fiber_storage" by default).
|
||||||
Table string
|
Table string
|
||||||
|
|
||||||
// AWS access key ID (part of the credentials).
|
|
||||||
// Optional (read from shared credentials file or environment variable if not set).
|
|
||||||
// Environment variable: "AWS_ACCESS_KEY_ID".
|
|
||||||
AWSaccessKeyID string
|
|
||||||
|
|
||||||
// AWS secret access key (part of the credentials).
|
|
||||||
// Optional (read from shared credentials file or environment variable if not set).
|
|
||||||
// Environment variable: "AWS_SECRET_ACCESS_KEY".
|
|
||||||
AWSsecretAccessKey string
|
|
||||||
|
|
||||||
// CustomEndpoint allows you to set a custom DynamoDB service endpoint.
|
// CustomEndpoint allows you to set a custom DynamoDB service endpoint.
|
||||||
// This is especially useful if you're running a "DynamoDB local" Docker container for local testing.
|
// This is especially useful if you're running a "DynamoDB local" Docker container for local testing.
|
||||||
// Typical value for the Docker container: "http://localhost:8000".
|
// Typical value for the Docker container: "http://localhost:8000".
|
||||||
// See https://hub.docker.com/r/amazon/dynamodb-local/.
|
// See https://hub.docker.com/r/amazon/dynamodb-local/.
|
||||||
// Optional ("" by default)
|
// Optional ("" by default)
|
||||||
CustomEndpoint string
|
Endpoint string
|
||||||
|
|
||||||
|
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
|
||||||
|
//
|
||||||
|
// Optional. Default is Credentials{}
|
||||||
|
Credentials Credentials
|
||||||
|
|
||||||
|
// The maximum number of times requests that encounter retryable failures should be attempted.
|
||||||
|
//
|
||||||
|
// Optional. Default is 3
|
||||||
|
MaxAttempts int
|
||||||
|
|
||||||
|
// Reset clears any existing keys in existing Bucket
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
Reset bool
|
||||||
|
|
||||||
|
// ReadCapacityUnits of the table.
|
||||||
|
// Only required when the table doesn't exist yet and is created by gokv.
|
||||||
|
// Optional (5 by default, which is the same default value as when creating a table in the web console)
|
||||||
|
// 25 RCUs are included in the free tier (across all tables).
|
||||||
|
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
|
||||||
|
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
|
||||||
|
ReadCapacityUnits int64
|
||||||
|
|
||||||
|
// ReadCapacityUnits of the table.
|
||||||
|
// Only required when the table doesn't exist yet and is created by gokv.
|
||||||
|
// Optional (5 by default, which is the same default value as when creating a table in the web console)
|
||||||
|
// 25 RCUs are included in the free tier (across all tables).
|
||||||
|
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
|
||||||
|
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
|
||||||
|
WriteCapacityUnits int64
|
||||||
|
|
||||||
|
// If the table doesn't exist yet, gokv creates it.
|
||||||
|
// If WaitForTableCreation is true, gokv will block until the table is created, with a timeout of 15 seconds.
|
||||||
|
// If the table still doesn't exist after 15 seconds, an error is returned.
|
||||||
|
// If WaitForTableCreation is false, gokv returns the client immediately.
|
||||||
|
// In the latter case you need to make sure that you don't read from or write to the table before it's created,
|
||||||
|
// because otherwise you will get ResourceNotFoundException errors.
|
||||||
|
// Optional (true by default).
|
||||||
|
WaitForTableCreation *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Credentials struct {
|
||||||
|
AccessKey string
|
||||||
|
SecretAccessKey string
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Default Config
|
### Default Config
|
||||||
```go
|
```go
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
|
Credentials: Credentials{},
|
||||||
|
MaxAttempts: 3,
|
||||||
|
Reset: false,
|
||||||
|
ReadCapacityUnits: 5,
|
||||||
|
WriteCapacityUnits: 5,
|
||||||
|
WaitForTableCreation: aws.Bool(true),
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package dynamodb
|
package dynamodb
|
||||||
|
|
||||||
import "github.com/aws/aws-sdk-go/aws"
|
import "github.com/aws/aws-sdk-go-v2/aws"
|
||||||
|
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -15,22 +15,27 @@ type Config struct {
|
|||||||
// Optional ("fiber_storage" by default).
|
// Optional ("fiber_storage" by default).
|
||||||
Table string
|
Table string
|
||||||
|
|
||||||
// AWS access key ID (part of the credentials).
|
|
||||||
// Optional (read from shared credentials file or environment variable if not set).
|
|
||||||
// Environment variable: "AWS_ACCESS_KEY_ID".
|
|
||||||
AWSaccessKeyID string
|
|
||||||
|
|
||||||
// AWS secret access key (part of the credentials).
|
|
||||||
// Optional (read from shared credentials file or environment variable if not set).
|
|
||||||
// Environment variable: "AWS_SECRET_ACCESS_KEY".
|
|
||||||
AWSsecretAccessKey string
|
|
||||||
|
|
||||||
// CustomEndpoint allows you to set a custom DynamoDB service endpoint.
|
// CustomEndpoint allows you to set a custom DynamoDB service endpoint.
|
||||||
// This is especially useful if you're running a "DynamoDB local" Docker container for local testing.
|
// This is especially useful if you're running a "DynamoDB local" Docker container for local testing.
|
||||||
// Typical value for the Docker container: "http://localhost:8000".
|
// Typical value for the Docker container: "http://localhost:8000".
|
||||||
// See https://hub.docker.com/r/amazon/dynamodb-local/.
|
// See https://hub.docker.com/r/amazon/dynamodb-local/.
|
||||||
// Optional ("" by default)
|
// Optional ("" by default)
|
||||||
CustomEndpoint string
|
Endpoint string
|
||||||
|
|
||||||
|
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
|
||||||
|
//
|
||||||
|
// Optional. Default is Credentials{}
|
||||||
|
Credentials Credentials
|
||||||
|
|
||||||
|
// The maximum number of times requests that encounter retryable failures should be attempted.
|
||||||
|
//
|
||||||
|
// Optional. Default is 3
|
||||||
|
MaxAttempts int
|
||||||
|
|
||||||
|
// Reset clears any existing keys in existing Bucket
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
Reset bool
|
||||||
|
|
||||||
// ReadCapacityUnits of the table.
|
// ReadCapacityUnits of the table.
|
||||||
// Only required when the table doesn't exist yet and is created by gokv.
|
// Only required when the table doesn't exist yet and is created by gokv.
|
||||||
@@ -38,14 +43,16 @@ type Config struct {
|
|||||||
// 25 RCUs are included in the free tier (across all tables).
|
// 25 RCUs are included in the free tier (across all tables).
|
||||||
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
|
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
|
||||||
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
|
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
|
||||||
readCapacityUnits int64
|
ReadCapacityUnits int64
|
||||||
|
|
||||||
// ReadCapacityUnits of the table.
|
// ReadCapacityUnits of the table.
|
||||||
// Only required when the table doesn't exist yet and is created by gokv.
|
// Only required when the table doesn't exist yet and is created by gokv.
|
||||||
// Optional (5 by default, which is the same default value as when creating a table in the web console)
|
// Optional (5 by default, which is the same default value as when creating a table in the web console)
|
||||||
// 25 RCUs are included in the free tier (across all tables).
|
// 25 RCUs are included in the free tier (across all tables).
|
||||||
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
|
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
|
||||||
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
|
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
|
||||||
writeCapacityUnits int64
|
WriteCapacityUnits int64
|
||||||
|
|
||||||
// If the table doesn't exist yet, gokv creates it.
|
// If the table doesn't exist yet, gokv creates it.
|
||||||
// If WaitForTableCreation is true, gokv will block until the table is created, with a timeout of 15 seconds.
|
// If WaitForTableCreation is true, gokv will block until the table is created, with a timeout of 15 seconds.
|
||||||
// If the table still doesn't exist after 15 seconds, an error is returned.
|
// If the table still doesn't exist after 15 seconds, an error is returned.
|
||||||
@@ -53,15 +60,23 @@ type Config struct {
|
|||||||
// In the latter case you need to make sure that you don't read from or write to the table before it's created,
|
// In the latter case you need to make sure that you don't read from or write to the table before it's created,
|
||||||
// because otherwise you will get ResourceNotFoundException errors.
|
// because otherwise you will get ResourceNotFoundException errors.
|
||||||
// Optional (true by default).
|
// Optional (true by default).
|
||||||
waitForTableCreation *bool
|
WaitForTableCreation *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Credentials struct {
|
||||||
|
AccessKey string
|
||||||
|
SecretAccessKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigDefault is the default config
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
readCapacityUnits: 5,
|
Credentials: Credentials{},
|
||||||
writeCapacityUnits: 5,
|
MaxAttempts: 3,
|
||||||
waitForTableCreation: aws.Bool(true),
|
Reset: false,
|
||||||
|
ReadCapacityUnits: 5,
|
||||||
|
WriteCapacityUnits: 5,
|
||||||
|
WaitForTableCreation: aws.Bool(true),
|
||||||
}
|
}
|
||||||
|
|
||||||
// configDefault is a helper function to set default values
|
// configDefault is a helper function to set default values
|
||||||
@@ -78,5 +93,18 @@ func configDefault(config ...Config) Config {
|
|||||||
if cfg.Table == "" {
|
if cfg.Table == "" {
|
||||||
cfg.Table = ConfigDefault.Table
|
cfg.Table = ConfigDefault.Table
|
||||||
}
|
}
|
||||||
|
if cfg.MaxAttempts == 0 {
|
||||||
|
cfg.MaxAttempts = ConfigDefault.MaxAttempts
|
||||||
|
}
|
||||||
|
if cfg.ReadCapacityUnits == 0 {
|
||||||
|
cfg.ReadCapacityUnits = ConfigDefault.ReadCapacityUnits
|
||||||
|
}
|
||||||
|
if cfg.WriteCapacityUnits == 0 {
|
||||||
|
cfg.WriteCapacityUnits = ConfigDefault.WriteCapacityUnits
|
||||||
|
}
|
||||||
|
if cfg.WaitForTableCreation == nil {
|
||||||
|
cfg.WaitForTableCreation = ConfigDefault.WaitForTableCreation
|
||||||
|
}
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
@@ -3,90 +3,23 @@ package dynamodb
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
"github.com/aws/aws-sdk-go-v2/aws/retry"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||||
awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
|
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
|
||||||
|
awsdynamodb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Storage interface that is implemented by storage providers
|
// Storage interface that is implemented by storage providers
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
db *awsdynamodb.DynamoDB
|
db *awsdynamodb.Client
|
||||||
table string
|
table string
|
||||||
}
|
requestTimeout time.Duration
|
||||||
|
|
||||||
// New creates a new storage
|
|
||||||
func New(config Config) *Storage {
|
|
||||||
// Set default config
|
|
||||||
cfg := configDefault(config)
|
|
||||||
|
|
||||||
// Create db
|
|
||||||
var creds *credentials.Credentials
|
|
||||||
if (cfg.AWSaccessKeyID != "" && cfg.AWSsecretAccessKey == "") || (cfg.AWSaccessKeyID == "" && cfg.AWSsecretAccessKey != "") {
|
|
||||||
panic("[DynamoDB] You need to set BOTH AWSaccessKeyID AND AWSsecretAccessKey")
|
|
||||||
} else if cfg.AWSaccessKeyID != "" {
|
|
||||||
// Due to the previous check we can be sure that in this case AWSsecretAccessKey is not empty as well.
|
|
||||||
creds = credentials.NewStaticCredentials(cfg.AWSaccessKeyID, cfg.AWSsecretAccessKey, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set database options
|
|
||||||
opt := aws.NewConfig()
|
|
||||||
if cfg.Region != "" {
|
|
||||||
opt = opt.WithRegion(cfg.Region)
|
|
||||||
}
|
|
||||||
if creds != nil {
|
|
||||||
opt = opt.WithCredentials(creds)
|
|
||||||
}
|
|
||||||
if cfg.CustomEndpoint != "" {
|
|
||||||
opt = opt.WithEndpoint(cfg.CustomEndpoint)
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionOpt := session.Options{
|
|
||||||
SharedConfigState: session.SharedConfigEnable,
|
|
||||||
}
|
|
||||||
|
|
||||||
// ...but allow overwrite of region and credentials if they are set in the options.
|
|
||||||
sessionOpt.Config.MergeIn(opt)
|
|
||||||
session, err := session.NewSessionWithOptions(sessionOpt)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
svc := awsdynamodb.New(session)
|
|
||||||
|
|
||||||
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
describeTableInput := awsdynamodb.DescribeTableInput{
|
|
||||||
TableName: &cfg.Table,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = svc.DescribeTableWithContext(timeoutCtx, &describeTableInput)
|
|
||||||
if err != nil {
|
|
||||||
awsErr, ok := err.(awserr.Error)
|
|
||||||
if !ok {
|
|
||||||
panic(err)
|
|
||||||
} else if awsErr.Code() == awsdynamodb.ErrCodeResourceNotFoundException {
|
|
||||||
err = createTable(cfg.Table, cfg.readCapacityUnits, cfg.writeCapacityUnits, *cfg.waitForTableCreation, describeTableInput, svc)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create storage
|
|
||||||
store := &Storage{
|
|
||||||
db: svc,
|
|
||||||
table: cfg.Table,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start garbage collector
|
|
||||||
//go store.gc()
|
|
||||||
|
|
||||||
return store
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// "k" is used as table column name for the key.
|
// "k" is used as table column name for the key.
|
||||||
@@ -95,126 +28,182 @@ var keyAttrName = "k"
|
|||||||
// "v" is used as table column name for the value.
|
// "v" is used as table column name for the value.
|
||||||
var valAttrName = "v"
|
var valAttrName = "v"
|
||||||
|
|
||||||
|
type table struct {
|
||||||
|
K string
|
||||||
|
V []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new storage
|
||||||
|
func New(config Config) *Storage {
|
||||||
|
// Set default config
|
||||||
|
cfg := configDefault(config)
|
||||||
|
|
||||||
|
awscfg, err := returnAWSConfig(cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("unable to load SDK config, %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create db
|
||||||
|
sess := awsdynamodb.NewFromConfig(awscfg)
|
||||||
|
|
||||||
|
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
describeTableInput := awsdynamodb.DescribeTableInput{
|
||||||
|
TableName: &cfg.Table,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create storage
|
||||||
|
store := &Storage{
|
||||||
|
db: sess,
|
||||||
|
table: cfg.Table,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create table
|
||||||
|
_, err = sess.DescribeTable(timeoutCtx, &describeTableInput)
|
||||||
|
if err != nil {
|
||||||
|
var rnfe *types.ResourceNotFoundException
|
||||||
|
if errors.As(err, &rnfe) {
|
||||||
|
err := store.createTable(cfg, describeTableInput)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
// Get value by key
|
// Get value by key
|
||||||
func (s *Storage) Get(key string) ([]byte, error) {
|
func (s *Storage) Get(key string) ([]byte, error) {
|
||||||
k := make(map[string]*awsdynamodb.AttributeValue)
|
ctx, cancel := s.requestContext()
|
||||||
k[keyAttrName] = &awsdynamodb.AttributeValue{
|
defer cancel()
|
||||||
S: &key,
|
|
||||||
|
k := make(map[string]types.AttributeValue)
|
||||||
|
k[keyAttrName] = &types.AttributeValueMemberS{
|
||||||
|
Value: key,
|
||||||
}
|
}
|
||||||
getItemInput := awsdynamodb.GetItemInput{
|
getItemInput := awsdynamodb.GetItemInput{
|
||||||
TableName: &s.table,
|
TableName: &s.table,
|
||||||
Key: k,
|
Key: k,
|
||||||
}
|
}
|
||||||
getItemOutput, err := s.db.GetItem(&getItemInput)
|
getItemOutput, err := s.db.GetItem(ctx, &getItemInput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var rnfe *types.ResourceNotFoundException
|
||||||
|
if errors.As(err, &rnfe) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if getItemOutput.Item == nil {
|
} else if getItemOutput.Item == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
attributeVal := getItemOutput.Item[valAttrName]
|
|
||||||
if attributeVal == nil {
|
item := &table{}
|
||||||
return nil, nil
|
err = attributevalue.UnmarshalMap(getItemOutput.Item, &item)
|
||||||
}
|
|
||||||
return attributeVal.B, nil
|
return item.V, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set key with value
|
|
||||||
// Set key with value
|
// Set key with value
|
||||||
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
// Ain't Nobody Got Time For That
|
// Ain't Nobody Got Time For That
|
||||||
if len(key) <= 0 || len(val) <= 0 {
|
if len(key) <= 0 || len(val) <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
item := make(map[string]*awsdynamodb.AttributeValue)
|
|
||||||
item[keyAttrName] = &awsdynamodb.AttributeValue{
|
item := make(map[string]types.AttributeValue)
|
||||||
S: &key,
|
item[keyAttrName] = &types.AttributeValueMemberS{
|
||||||
|
Value: key,
|
||||||
}
|
}
|
||||||
item[valAttrName] = &awsdynamodb.AttributeValue{
|
item[valAttrName] = &types.AttributeValueMemberB{
|
||||||
B: val,
|
Value: val,
|
||||||
}
|
}
|
||||||
putItemInput := awsdynamodb.PutItemInput{
|
putItemInput := awsdynamodb.PutItemInput{
|
||||||
TableName: &s.table,
|
TableName: &s.table,
|
||||||
Item: item,
|
Item: item,
|
||||||
}
|
}
|
||||||
_, err := s.db.PutItem(&putItemInput)
|
|
||||||
|
_, err := s.db.PutItem(ctx, &putItemInput)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete entry by key
|
// Delete entry by key
|
||||||
func (s *Storage) Delete(key string) error {
|
func (s *Storage) Delete(key string) error {
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
// Ain't Nobody Got Time For That
|
// Ain't Nobody Got Time For That
|
||||||
if len(key) <= 0 {
|
if len(key) <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
k := make(map[string]*awsdynamodb.AttributeValue)
|
|
||||||
k[keyAttrName] = &awsdynamodb.AttributeValue{
|
k := make(map[string]types.AttributeValue)
|
||||||
S: &key,
|
k[keyAttrName] = &types.AttributeValueMemberS{
|
||||||
|
Value: key,
|
||||||
}
|
}
|
||||||
deleteItemInput := awsdynamodb.DeleteItemInput{
|
deleteItemInput := awsdynamodb.DeleteItemInput{
|
||||||
TableName: &s.table,
|
TableName: &s.table,
|
||||||
Key: k,
|
Key: k,
|
||||||
}
|
}
|
||||||
_, err := s.db.DeleteItem(&deleteItemInput)
|
|
||||||
|
_, err := s.db.DeleteItem(ctx, &deleteItemInput)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset all entries, including unexpired
|
// Reset all entries, including unexpired
|
||||||
func (s *Storage) Reset() error {
|
func (s *Storage) Reset() error {
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
deleteTableInput := awsdynamodb.DeleteTableInput{
|
deleteTableInput := awsdynamodb.DeleteTableInput{
|
||||||
TableName: &s.table,
|
TableName: &s.table,
|
||||||
}
|
}
|
||||||
_, err := s.db.DeleteTable(&deleteTableInput)
|
_, err := s.db.DeleteTable(ctx, &deleteTableInput)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the database
|
// Close the database
|
||||||
func (s *Storage) Close() error {
|
func (s *Storage) Close() error {
|
||||||
// In the DynamoDB implementation this doesn't have any effect.
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GC deletes all expired entries
|
func (s *Storage) createTable(cfg Config, describeTableInput awsdynamodb.DescribeTableInput) error {
|
||||||
// func (s *Storage) gc() {
|
ctx, cancel := s.requestContext()
|
||||||
// ticker := time.NewTicker(s.gcInterval)
|
defer cancel()
|
||||||
// defer ticker.Stop()
|
|
||||||
// for {
|
|
||||||
// select {
|
|
||||||
// case <-s.done:
|
|
||||||
// return
|
|
||||||
// case t := <-ticker.C:
|
|
||||||
// _, _ = s.db.Exec(s.sqlGC, t.Unix())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func createTable(tableName string, readCapacityUnits, writeCapacityUnits int64, waitForTableCreation bool, describeTableInput awsdynamodb.DescribeTableInput, svc *awsdynamodb.DynamoDB) error {
|
|
||||||
keyAttrType := "S" // For "string"
|
keyAttrType := "S" // For "string"
|
||||||
keyType := "HASH" // As opposed to "RANGE"
|
keyType := "HASH" // As opposed to "RANGE"
|
||||||
|
|
||||||
createTableInput := awsdynamodb.CreateTableInput{
|
createTableInput := awsdynamodb.CreateTableInput{
|
||||||
TableName: &tableName,
|
TableName: &s.table,
|
||||||
AttributeDefinitions: []*awsdynamodb.AttributeDefinition{{
|
AttributeDefinitions: []types.AttributeDefinition{{
|
||||||
AttributeName: &keyAttrName,
|
AttributeName: &keyAttrName,
|
||||||
AttributeType: &keyAttrType,
|
AttributeType: types.ScalarAttributeType(keyAttrType),
|
||||||
}},
|
}},
|
||||||
KeySchema: []*awsdynamodb.KeySchemaElement{{
|
KeySchema: []types.KeySchemaElement{{
|
||||||
AttributeName: &keyAttrName,
|
AttributeName: &keyAttrName,
|
||||||
KeyType: &keyType,
|
KeyType: types.KeyType(keyType),
|
||||||
}},
|
}},
|
||||||
ProvisionedThroughput: &awsdynamodb.ProvisionedThroughput{
|
ProvisionedThroughput: &types.ProvisionedThroughput{
|
||||||
ReadCapacityUnits: &readCapacityUnits,
|
ReadCapacityUnits: &cfg.ReadCapacityUnits,
|
||||||
WriteCapacityUnits: &writeCapacityUnits,
|
WriteCapacityUnits: &cfg.WriteCapacityUnits,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err := svc.CreateTable(&createTableInput)
|
_, err := s.db.CreateTable(ctx, &createTableInput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// If configured (true by default), block until the table is created.
|
// If configured (true by default), block until the table is created.
|
||||||
// Typical table creation duration is 10 seconds.
|
// Typical table creation duration is 10 seconds.
|
||||||
if waitForTableCreation {
|
if *cfg.WaitForTableCreation {
|
||||||
for try := 1; try < 16; try++ {
|
for try := 1; try < 16; try++ {
|
||||||
describeTableOutput, err := svc.DescribeTable(&describeTableInput)
|
describeTableOutput, err := s.db.DescribeTable(ctx, &describeTableInput)
|
||||||
if err != nil || *describeTableOutput.Table.TableStatus == "CREATING" {
|
if err != nil || describeTableOutput.Table.TableStatus == "CREATING" {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -222,14 +211,61 @@ func createTable(tableName string, readCapacityUnits, writeCapacityUnits int64,
|
|||||||
}
|
}
|
||||||
// Last try (16th) after 15 seconds of waiting.
|
// Last try (16th) after 15 seconds of waiting.
|
||||||
// Now handle error as such.
|
// Now handle error as such.
|
||||||
describeTableOutput, err := svc.DescribeTable(&describeTableInput)
|
describeTableOutput, err := s.db.DescribeTable(ctx, &describeTableInput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("The DynamoDB table couldn't be created")
|
return errors.New("dynamodb: the table couldn't be created")
|
||||||
}
|
}
|
||||||
if *describeTableOutput.Table.TableStatus == "CREATING" {
|
if describeTableOutput.Table.TableStatus == "CREATING" {
|
||||||
return errors.New("The DynamoDB table took too long to be created")
|
return errors.New("dynamodb: the table took too long to be created")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Context for making requests will timeout if a non-zero timeout is configured
|
||||||
|
func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
|
||||||
|
if s.requestTimeout > 0 {
|
||||||
|
return context.WithTimeout(context.Background(), s.requestTimeout)
|
||||||
|
}
|
||||||
|
return context.Background(), func() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func returnAWSConfig(cfg Config) (aws.Config, error) {
|
||||||
|
endpoint := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
||||||
|
if cfg.Endpoint != "" {
|
||||||
|
return aws.Endpoint{
|
||||||
|
PartitionID: "aws",
|
||||||
|
URL: cfg.Endpoint,
|
||||||
|
SigningRegion: cfg.Region,
|
||||||
|
HostnameImmutable: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
|
||||||
|
})
|
||||||
|
|
||||||
|
if cfg.Credentials != (Credentials{}) {
|
||||||
|
credentials := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
|
||||||
|
return awsconfig.LoadDefaultConfig(context.TODO(),
|
||||||
|
awsconfig.WithRegion(cfg.Region),
|
||||||
|
awsconfig.WithEndpointResolverWithOptions(endpoint),
|
||||||
|
awsconfig.WithCredentialsProvider(credentials),
|
||||||
|
awsconfig.WithRetryer(func() aws.Retryer {
|
||||||
|
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return awsconfig.LoadDefaultConfig(context.TODO(),
|
||||||
|
awsconfig.WithRegion(cfg.Region),
|
||||||
|
awsconfig.WithEndpointResolverWithOptions(endpoint),
|
||||||
|
awsconfig.WithRetryer(func() aws.Retryer {
|
||||||
|
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *awsdynamodb.Client {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -1 +1,111 @@
|
|||||||
package dynamodb
|
package dynamodb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gofiber/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testStore = New(
|
||||||
|
Config{
|
||||||
|
Table: "fiber_storage",
|
||||||
|
Endpoint: "http://localhost:8000/",
|
||||||
|
Region: "us-east-1",
|
||||||
|
Credentials: Credentials{
|
||||||
|
AccessKey: "dummy",
|
||||||
|
SecretAccessKey: "dummy",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_DynamoDB_Set(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Set_Override(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Get(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, val, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Get_NotExist(t *testing.T) {
|
||||||
|
|
||||||
|
result, err := testStore.Get("notexist")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Delete(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Delete(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Reset(t *testing.T) {
|
||||||
|
var (
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set("john1", val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Set("john2", val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Reset()
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get("john1")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
|
||||||
|
result, err = testStore.Get("john2")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Close(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_DynamoDB_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
@@ -2,4 +2,11 @@ module github.com/gofiber/storage/dynamodb
|
|||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require github.com/aws/aws-sdk-go v1.42.38
|
require (
|
||||||
|
github.com/aws/aws-sdk-go-v2 v1.16.16
|
||||||
|
github.com/aws/aws-sdk-go-v2/config v1.17.8
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.12.21
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.0
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.1
|
||||||
|
github.com/gofiber/utils v0.1.2
|
||||||
|
)
|
||||||
|
@@ -1,23 +1,50 @@
|
|||||||
github.com/aws/aws-sdk-go v1.42.38 h1:/fNQTB4ZUQOa8+cfX7C7F0zyXRdiN1jGKKXt3+5nmzM=
|
github.com/aws/aws-sdk-go-v2 v1.16.16 h1:M1fj4FE2lB4NzRb9Y0xdWsn2P0+2UHVxwKyOa4YJNjk=
|
||||||
github.com/aws/aws-sdk-go v1.42.38/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc=
|
github.com/aws/aws-sdk-go-v2 v1.16.16/go.mod h1:SwiyXi/1zTUZ6KIAmLK5V5ll8SiURNUYOqTerZPaF9k=
|
||||||
|
github.com/aws/aws-sdk-go-v2/config v1.17.8 h1:b9LGqNnOdg9vR4Q43tBTVWk4J6F+W774MSchvKJsqnE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/config v1.17.8/go.mod h1:UkCI3kb0sCdvtjiXYiU4Zx5h07BOpgBTtkPu/49r+kA=
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.12.21 h1:4tjlyCD0hRGNQivh5dN8hbP30qQhMLBE/FgQR1vHHWM=
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.12.21/go.mod h1:O+4XyAt4e+oBAoIwNUYkRg3CVMscaIJdmZBOcPgJ8D8=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.0 h1:bKbdstt7+PzIRSIXZ11Yo8Qh8t0AHn6jEYUfsbVcLjE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.0/go.mod h1:+CBJZMhsb1pTUcB/NTdS505bDX10xS4xnPMqDZj2Ptw=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.17 h1:r08j4sbZu/RVi+BNxkBJwPMUYY3P8mgSDuKkZ/ZN1lE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.17/go.mod h1:yIkQcCDYNsZfXpd5UX2Cy+sWA1jPgIhGTw9cOBzfVnQ=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.23 h1:s4g/wnzMf+qepSNgTvaQQHNxyMLKSawNhKCPNy++2xY=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.23/go.mod h1:2DFxAQ9pfIRy0imBCJv+vZ2X6RKxves6fbnEuSry6b4=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.17 h1:/K482T5A3623WJgWT8w1yRAFK4RzGzEl7y39yhtn9eA=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.17/go.mod h1:pRwaTYCJemADaqCbUAxltMoHKata7hmB5PjEXeu0kfg=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.24 h1:wj5Rwc05hvUSvKuOF29IYb9QrCLjU+rHAy/x/o0DK2c=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.24/go.mod h1:jULHjqqjDlbyTa7pfM7WICATnOv+iOhjletM3N0Xbu8=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.1 h1:1QpTkQIAaZpR387it1L+erjB5bStGFCJRvmXsodpPEU=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.1/go.mod h1:BZhn/C3z13ULTSstVi2Kymc62bgjFh/JwLO9Tm2OFYI=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.13.20 h1:V9q4A0qnUfDsfivspY1LQRQTOG3Y9FLHvXIaTbcU7XM=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.13.20/go.mod h1:7qWU48SMzlrfOlNhHpazW3psFWlOIWrq4SmOr2/ESmk=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.9 h1:Lh1AShsuIJTwMkoxVCAYPJgNG5H+eN6SmoUn8nOZ5wE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.9/go.mod h1:a9j48l6yL5XINLHLcOKInjdvknN+vWqPBxqeIDw7ktw=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.17 h1:o0Ia3nb56m8+8NvhbCDiSBiZRNUwIknVWobx5vks0Vk=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.17/go.mod h1:WJD9FbkwzM2a1bZ36ntH6+5Jc+x41Q4K2AcLeHDLAS8=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.17 h1:Jrd/oMh0PKQc6+BowB+pLEwLIgaQF29eYbe7E1Av9Ug=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.17/go.mod h1:4nYOrY41Lrbk2170/BGkcJKBhws9Pfn8MG3aGqjjeFI=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sso v1.11.23 h1:pwvCchFUEnlceKIgPUouBJwK81aCkQ8UDMORfeFtW10=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sso v1.11.23/go.mod h1:/w0eg9IhFGjGyyncHIQrXtU8wvNsTJOP0R6PPj0wf80=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.6 h1:OwhhKc1P9ElfWbMKPIbMMZBV6hzJlL2JKD76wNNVzgQ=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.6/go.mod h1:csZuQY65DAdFBt1oIjO5hhBR49kQqop4+lcuCjf2arA=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sts v1.16.19 h1:9pPi0PsFNAGILFfPCk8Y0iyEBGc6lu6OQ97U7hmdesg=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sts v1.16.19/go.mod h1:h4J3oPZQbxLhzGnk+j9dfYHi5qIOVJ5kczZd658/ydM=
|
||||||
|
github.com/aws/smithy-go v1.13.3 h1:l7LYxGuzK6/K+NzJ2mC+VvLUbae0sL3bXU//04MkmnA=
|
||||||
|
github.com/aws/smithy-go v1.13.3/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
|
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||||
|
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
|
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
|
||||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
|
|
||||||
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/gofiber/storage
|
module github.com/gofiber/storage
|
||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
@@ -17,6 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *mc.Client
|
||||||
```
|
```
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
@@ -122,3 +122,8 @@ func (s *Storage) releaseItem(item *mc.Item) {
|
|||||||
s.items.Put(item)
|
s.items.Put(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *mc.Client {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -119,3 +119,7 @@ func Test_Memcache_Reset(t *testing.T) {
|
|||||||
func Test_Memcache_Close(t *testing.T) {
|
func Test_Memcache_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Memcache_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
@@ -18,6 +18,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() map[string]entry
|
||||||
```
|
```
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
32
memory/internal/time.go
Normal file
32
memory/internal/time.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
timestampTimer sync.Once
|
||||||
|
// Timestamp please start the timer function before you use this value
|
||||||
|
// please load the value with atomic `atomic.LoadUint32(&utils.Timestamp)`
|
||||||
|
Timestamp uint32
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartTimeStampUpdater starts a concurrent function which stores the timestamp to an atomic value per second,
|
||||||
|
// which is much better for performance than determining it at runtime each time
|
||||||
|
func StartTimeStampUpdater() {
|
||||||
|
timestampTimer.Do(func() {
|
||||||
|
// set initial value
|
||||||
|
atomic.StoreUint32(&Timestamp, uint32(time.Now().Unix()))
|
||||||
|
go func(sleep time.Duration) {
|
||||||
|
ticker := time.NewTicker(sleep)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for t := range ticker.C {
|
||||||
|
// update timestamp
|
||||||
|
atomic.StoreUint32(&Timestamp, uint32(t.Unix()))
|
||||||
|
}
|
||||||
|
}(1 * time.Second) // duration
|
||||||
|
})
|
||||||
|
}
|
47
memory/internal/time_test.go
Normal file
47
memory/internal/time_test.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkTimeStamp(t testing.TB, expectedCurrent, actualCurrent uint32) {
|
||||||
|
// test with some buffer in front and back of the expectedCurrent time -> because of the timing on the work machine
|
||||||
|
utils.AssertEqual(t, true, actualCurrent >= expectedCurrent-1 || actualCurrent <= expectedCurrent+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TimeStampUpdater(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
StartTimeStampUpdater()
|
||||||
|
|
||||||
|
now := uint32(time.Now().Unix())
|
||||||
|
checkTimeStamp(t, now, atomic.LoadUint32(&Timestamp))
|
||||||
|
// one second later
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
checkTimeStamp(t, now+1, atomic.LoadUint32(&Timestamp))
|
||||||
|
// two seconds later
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
checkTimeStamp(t, now+2, atomic.LoadUint32(&Timestamp))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_CalculateTimestamp(b *testing.B) {
|
||||||
|
StartTimeStampUpdater()
|
||||||
|
|
||||||
|
var res uint32
|
||||||
|
b.Run("fiber", func(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
res = atomic.LoadUint32(&Timestamp)
|
||||||
|
}
|
||||||
|
checkTimeStamp(b, uint32(time.Now().Unix()), res)
|
||||||
|
})
|
||||||
|
b.Run("default", func(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
res = uint32(time.Now().Unix())
|
||||||
|
}
|
||||||
|
checkTimeStamp(b, uint32(time.Now().Unix()), res)
|
||||||
|
})
|
||||||
|
}
|
@@ -2,7 +2,10 @@ package memory
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/storage/memory/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Storage interface that is implemented by storage providers
|
// Storage interface that is implemented by storage providers
|
||||||
@@ -14,8 +17,9 @@ type Storage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type entry struct {
|
type entry struct {
|
||||||
data []byte
|
data []byte
|
||||||
expiry int64
|
// max value is 4294967295 -> Sun Feb 07 2106 06:28:15 GMT+0000
|
||||||
|
expiry uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new memory storage
|
// New creates a new memory storage
|
||||||
@@ -31,6 +35,7 @@ func New(config ...Config) *Storage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start garbage collector
|
// Start garbage collector
|
||||||
|
internal.StartTimeStampUpdater()
|
||||||
go store.gc()
|
go store.gc()
|
||||||
|
|
||||||
return store
|
return store
|
||||||
@@ -44,14 +49,13 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
|||||||
s.mux.RLock()
|
s.mux.RLock()
|
||||||
v, ok := s.db[key]
|
v, ok := s.db[key]
|
||||||
s.mux.RUnlock()
|
s.mux.RUnlock()
|
||||||
if !ok || v.expiry != 0 && v.expiry <= time.Now().Unix() {
|
if !ok || v.expiry != 0 && v.expiry <= atomic.LoadUint32(&internal.Timestamp) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return v.data, nil
|
return v.data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set key with value
|
|
||||||
// Set key with value
|
// Set key with value
|
||||||
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
||||||
// Ain't Nobody Got Time For That
|
// Ain't Nobody Got Time For That
|
||||||
@@ -59,9 +63,9 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var expire int64
|
var expire uint32
|
||||||
if exp != 0 {
|
if exp != 0 {
|
||||||
expire = time.Now().Add(exp).Unix()
|
expire = uint32(exp.Seconds()) + atomic.LoadUint32(&internal.Timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.mux.Lock()
|
s.mux.Lock()
|
||||||
@@ -99,20 +103,31 @@ func (s *Storage) Close() error {
|
|||||||
func (s *Storage) gc() {
|
func (s *Storage) gc() {
|
||||||
ticker := time.NewTicker(s.gcInterval)
|
ticker := time.NewTicker(s.gcInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
var expired []string
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-s.done:
|
case <-s.done:
|
||||||
return
|
return
|
||||||
case t := <-ticker.C:
|
case <-ticker.C:
|
||||||
now := t.Unix()
|
expired = expired[:0]
|
||||||
s.mux.Lock()
|
s.mux.RLock()
|
||||||
for id, v := range s.db {
|
for id, v := range s.db {
|
||||||
if v.expiry != 0 && v.expiry < now {
|
if v.expiry != 0 && v.expiry < atomic.LoadUint32(&internal.Timestamp) {
|
||||||
delete(s.db, id)
|
expired = append(expired, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s.mux.RUnlock()
|
||||||
|
s.mux.Lock()
|
||||||
|
for i := range expired {
|
||||||
|
delete(s.db, expired[i])
|
||||||
|
}
|
||||||
s.mux.Unlock()
|
s.mux.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() map[string]entry {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -119,3 +119,35 @@ func Test_Memory_Reset(t *testing.T) {
|
|||||||
func Test_Memory_Close(t *testing.T) {
|
func Test_Memory_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Memory_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// go test -v -run=^$ -bench=Benchmark_Memory -benchmem -count=4
|
||||||
|
func Benchmark_Memory(b *testing.B) {
|
||||||
|
keyLength := 1000
|
||||||
|
keys := make([]string, keyLength)
|
||||||
|
for i := 0; i < keyLength; i++ {
|
||||||
|
keys[i] = utils.UUID()
|
||||||
|
}
|
||||||
|
value := []byte("joe")
|
||||||
|
|
||||||
|
ttl := 2 * time.Second
|
||||||
|
b.Run("fiber_memory", func(b *testing.B) {
|
||||||
|
d := New()
|
||||||
|
b.ReportAllocs()
|
||||||
|
b.ResetTimer()
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
for _, key := range keys {
|
||||||
|
d.Set(key, value, ttl)
|
||||||
|
}
|
||||||
|
for _, key := range keys {
|
||||||
|
_, _ = d.Get(key)
|
||||||
|
}
|
||||||
|
for _, key := range keys {
|
||||||
|
d.Delete(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@@ -17,6 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *mongo.Database
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
MongoDB is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
MongoDB is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
|
@@ -3,12 +3,8 @@ module github.com/gofiber/storage/mongodb
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-stack/stack v1.8.1 // indirect
|
|
||||||
github.com/gofiber/utils v0.1.2
|
github.com/gofiber/utils v0.1.2
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||||
go.mongodb.org/mongo-driver v1.8.2
|
go.mongodb.org/mongo-driver v1.10.3
|
||||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
|
||||||
golang.org/x/text v0.3.7 // indirect
|
|
||||||
)
|
)
|
||||||
|
@@ -1,9 +1,6 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
|
||||||
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
|
|
||||||
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
|
|
||||||
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
@@ -18,6 +15,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
|||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
|
||||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
@@ -30,46 +28,39 @@ github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
|||||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||||
github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w=
|
github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
|
||||||
github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
|
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
|
||||||
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
|
github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
|
||||||
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
|
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
|
||||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk=
|
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk=
|
||||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4=
|
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4=
|
||||||
go.mongodb.org/mongo-driver v1.8.2 h1:8ssUXufb90ujcIvR6MyE1SchaNj0SFxsakiZgxIyrMk=
|
go.mongodb.org/mongo-driver v1.10.3 h1:XDQEvmh6z1EUsXuIkXE9TaVeqHw6SwS1uf93jFs0HBA=
|
||||||
go.mongodb.org/mongo-driver v1.8.2/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
|
go.mongodb.org/mongo-driver v1.10.3/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
|
||||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
|
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@@ -199,3 +199,8 @@ func (s *Storage) releaseItem(item *item) {
|
|||||||
s.items.Put(item)
|
s.items.Put(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *mongo.Database {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -121,3 +121,7 @@ func Test_MongoDB_Reset(t *testing.T) {
|
|||||||
func Test_MongoDB_Close(t *testing.T) {
|
func Test_MongoDB_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_MongoDB_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
@@ -17,6 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *sql.DB
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
MySQL is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
MySQL is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
@@ -48,11 +49,36 @@ store := mysql.New(mysql.Config{
|
|||||||
Reset: false,
|
Reset: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Initialize custom config using connection string
|
||||||
|
store := mysql.New(mysql.Config{
|
||||||
|
ConnectionURI: "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>"
|
||||||
|
Reset: false,
|
||||||
|
GCInterval: 10 * time.Second,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Initialize custom config using sql db connection
|
||||||
|
db, _ := sql.Open("mysql", "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")
|
||||||
|
store := mysql.New(mysql.Config{
|
||||||
|
Db: db,
|
||||||
|
Reset: false,
|
||||||
|
GCInterval: 10 * time.Second,
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Config
|
### Config
|
||||||
```go
|
```go
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// DB Will override ConnectionURI and all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is nil
|
||||||
|
Db *sql.DB
|
||||||
|
|
||||||
|
// Connection string to use for DB. Will override all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is ""
|
||||||
|
ConnectionURI string
|
||||||
|
|
||||||
// Host name where the DB is hosted
|
// Host name where the DB is hosted
|
||||||
//
|
//
|
||||||
// Optional. Default is "127.0.0.1"
|
// Optional. Default is "127.0.0.1"
|
||||||
@@ -98,6 +124,7 @@ type Config struct {
|
|||||||
### Default Config
|
### Default Config
|
||||||
```go
|
```go
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
|
ConnectionURI: "",
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 3306,
|
Port: 3306,
|
||||||
Database: "fiber",
|
Database: "fiber",
|
||||||
|
@@ -1,12 +1,23 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// DB Will override ConnectionURI and all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is nil
|
||||||
|
Db *sql.DB
|
||||||
|
|
||||||
|
// Connection string to use for DB. Will override all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is ""
|
||||||
|
ConnectionURI string
|
||||||
|
|
||||||
// Host name where the DB is hosted
|
// Host name where the DB is hosted
|
||||||
//
|
//
|
||||||
// Optional. Default is "127.0.0.1"
|
// Optional. Default is "127.0.0.1"
|
||||||
@@ -58,6 +69,8 @@ type Config struct {
|
|||||||
|
|
||||||
// ConfigDefault is the default config
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
|
Db: nil,
|
||||||
|
ConnectionURI: "",
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 3306,
|
Port: 3306,
|
||||||
Database: "fiber",
|
Database: "fiber",
|
||||||
@@ -70,6 +83,9 @@ var ConfigDefault = Config{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) dsn() string {
|
func (c Config) dsn() string {
|
||||||
|
if c.ConnectionURI != "" {
|
||||||
|
return c.ConnectionURI
|
||||||
|
}
|
||||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.Username, c.Password, c.Host, c.Port, c.Database)
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.Username, c.Password, c.Host, c.Port, c.Database)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -40,19 +40,27 @@ var (
|
|||||||
|
|
||||||
// New creates a new storage
|
// New creates a new storage
|
||||||
func New(config ...Config) *Storage {
|
func New(config ...Config) *Storage {
|
||||||
|
var err error
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
// Set default config
|
// Set default config
|
||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
// Create db
|
if cfg.Db != nil {
|
||||||
db, err := sql.Open("mysql", cfg.dsn())
|
// Use passed db
|
||||||
if err != nil {
|
db = cfg.Db
|
||||||
panic(err)
|
} else {
|
||||||
}
|
// Create db
|
||||||
|
db, err = sql.Open("mysql", cfg.dsn())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Set options
|
// Set options
|
||||||
db.SetMaxOpenConns(cfg.maxOpenConns)
|
db.SetMaxOpenConns(cfg.maxOpenConns)
|
||||||
db.SetMaxIdleConns(cfg.maxIdleConns)
|
db.SetMaxIdleConns(cfg.maxIdleConns)
|
||||||
db.SetConnMaxLifetime(cfg.connMaxLifetime)
|
db.SetConnMaxLifetime(cfg.connMaxLifetime)
|
||||||
|
}
|
||||||
|
|
||||||
// Ping database to ensure a connection has been made
|
// Ping database to ensure a connection has been made
|
||||||
if err := db.Ping(); err != nil {
|
if err := db.Ping(); err != nil {
|
||||||
@@ -165,6 +173,11 @@ func (s *Storage) Close() error {
|
|||||||
return s.db.Close()
|
return s.db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *sql.DB {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
|
||||||
// gcTicker starts the gc ticker
|
// gcTicker starts the gc ticker
|
||||||
func (s *Storage) gcTicker() {
|
func (s *Storage) gcTicker() {
|
||||||
ticker := time.NewTicker(s.gcInterval)
|
ticker := time.NewTicker(s.gcInterval)
|
||||||
|
@@ -2,11 +2,12 @@ package mysql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gofiber/utils"
|
"github.com/gofiber/utils"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testStore = New(Config{
|
var testStore = New(Config{
|
||||||
@@ -16,6 +17,36 @@ var testStore = New(Config{
|
|||||||
Reset: true,
|
Reset: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
func Test_MYSQL_New(t *testing.T) {
|
||||||
|
newConfigStore := New(Config{
|
||||||
|
Database: os.Getenv("MYSQL_DATABASE"),
|
||||||
|
Username: os.Getenv("MYSQL_USERNAME"),
|
||||||
|
Password: os.Getenv("MYSQL_PASSWORD"),
|
||||||
|
Reset: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
utils.AssertEqual(t, true, newConfigStore.db != nil)
|
||||||
|
newConfigStore.Close()
|
||||||
|
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", os.Getenv("MYSQL_USERNAME"), os.Getenv("MYSQL_PASSWORD"), "127.0.0.1", 3306, os.Getenv("MYSQL_DATABASE"))
|
||||||
|
newConfigStore = New(Config{
|
||||||
|
ConnectionURI: dsn,
|
||||||
|
Reset: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
utils.AssertEqual(t, true, newConfigStore.db != nil)
|
||||||
|
newConfigStore.Close()
|
||||||
|
|
||||||
|
db, _ := sql.Open("mysql", dsn)
|
||||||
|
newConfigStore = New(Config{
|
||||||
|
Db: db,
|
||||||
|
Reset: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
utils.AssertEqual(t, true, newConfigStore.db != nil)
|
||||||
|
newConfigStore.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func Test_MYSQL_Set(t *testing.T) {
|
func Test_MYSQL_Set(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
key = "john"
|
key = "john"
|
||||||
@@ -162,3 +193,7 @@ func Test_MYSQL_Non_UTF8(t *testing.T) {
|
|||||||
func Test_MYSQL_Close(t *testing.T) {
|
func Test_MYSQL_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_MYSQL_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
@@ -17,6 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *sql.DB
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
Postgres is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
Postgres is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
@@ -49,12 +50,24 @@ store := postgres.New(postgres.Config{
|
|||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
SslMode: "disable",
|
SslMode: "disable",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Initialize custom config using connection string
|
||||||
|
store := postgres.New(postgres.Config{
|
||||||
|
ConnectionURI: "postgresql://user:password@localhost:5432/fiber"
|
||||||
|
Reset: false,
|
||||||
|
GCInterval: 10 * time.Second,
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Config
|
### Config
|
||||||
```go
|
```go
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// Connection string to use for DB. Will override all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is ""
|
||||||
|
ConnectionURI string
|
||||||
|
|
||||||
// Host name where the DB is hosted
|
// Host name where the DB is hosted
|
||||||
//
|
//
|
||||||
// Optional. Default is "127.0.0.1"
|
// Optional. Default is "127.0.0.1"
|
||||||
@@ -105,6 +118,7 @@ type Config struct {
|
|||||||
### Default Config
|
### Default Config
|
||||||
```go
|
```go
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
|
ConnectionURI: "",
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 5432,
|
Port: 5432,
|
||||||
Database: "fiber",
|
Database: "fiber",
|
||||||
|
@@ -6,6 +6,11 @@ import (
|
|||||||
|
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// Connection string to use for DB. Will override all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is ""
|
||||||
|
ConnectionURI string
|
||||||
|
|
||||||
// Host name where the DB is hosted
|
// Host name where the DB is hosted
|
||||||
//
|
//
|
||||||
// Optional. Default is "127.0.0.1"
|
// Optional. Default is "127.0.0.1"
|
||||||
@@ -90,6 +95,7 @@ type Config struct {
|
|||||||
|
|
||||||
// ConfigDefault is the default config
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
|
ConnectionURI: "",
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 5432,
|
Port: 5432,
|
||||||
Database: "fiber",
|
Database: "fiber",
|
||||||
|
@@ -4,5 +4,5 @@ go 1.14
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gofiber/utils v0.1.2
|
github.com/gofiber/utils v0.1.2
|
||||||
github.com/lib/pq v1.10.4
|
github.com/lib/pq v1.10.7
|
||||||
)
|
)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
|
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||||
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
@@ -46,22 +46,31 @@ func New(config ...Config) *Storage {
|
|||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
// Create data source name
|
// Create data source name
|
||||||
var dsn string = "postgresql://"
|
var dsn string
|
||||||
if cfg.Username != "" {
|
if cfg.ConnectionURI != "" {
|
||||||
dsn += url.QueryEscape(cfg.Username)
|
dsn = cfg.ConnectionURI
|
||||||
|
} else {
|
||||||
|
dsn = "postgresql://"
|
||||||
|
if cfg.Username != "" {
|
||||||
|
dsn += url.QueryEscape(cfg.Username)
|
||||||
|
}
|
||||||
|
if cfg.Password != "" {
|
||||||
|
dsn += ":" + cfg.Password
|
||||||
|
}
|
||||||
|
if cfg.Username != "" || cfg.Password != "" {
|
||||||
|
dsn += "@"
|
||||||
|
}
|
||||||
|
// unix socket host path
|
||||||
|
if strings.HasPrefix(cfg.Host, "/") {
|
||||||
|
dsn += fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
||||||
|
} else {
|
||||||
|
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
|
||||||
|
}
|
||||||
|
dsn += fmt.Sprintf("/%s?connect_timeout=%d&sslmode=%s",
|
||||||
|
url.QueryEscape(cfg.Database),
|
||||||
|
int64(cfg.timeout.Seconds()),
|
||||||
|
cfg.SslMode)
|
||||||
}
|
}
|
||||||
if cfg.Password != "" {
|
|
||||||
dsn += ":" + cfg.Password
|
|
||||||
}
|
|
||||||
if cfg.Username != "" || cfg.Password != "" {
|
|
||||||
dsn += "@"
|
|
||||||
}
|
|
||||||
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
|
|
||||||
dsn += fmt.Sprintf("/%s?connect_timeout=%d&sslmode=%s",
|
|
||||||
url.QueryEscape(cfg.Database),
|
|
||||||
int64(cfg.timeout.Seconds()),
|
|
||||||
cfg.SslMode,
|
|
||||||
)
|
|
||||||
|
|
||||||
// Create db
|
// Create db
|
||||||
db, err := sql.Open("postgres", dsn)
|
db, err := sql.Open("postgres", dsn)
|
||||||
@@ -180,6 +189,11 @@ func (s *Storage) Close() error {
|
|||||||
return s.db.Close()
|
return s.db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *sql.DB {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
|
||||||
// gcTicker starts the gc ticker
|
// gcTicker starts the gc ticker
|
||||||
func (s *Storage) gcTicker() {
|
func (s *Storage) gcTicker() {
|
||||||
ticker := time.NewTicker(s.gcInterval)
|
ticker := time.NewTicker(s.gcInterval)
|
||||||
|
@@ -177,3 +177,7 @@ func Test_SslRequiredMode(t *testing.T) {
|
|||||||
func Test_Postgres_Close(t *testing.T) {
|
func Test_Postgres_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Postgres_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
@@ -17,6 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *redis.Client
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
Redis is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
Redis is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
@@ -49,7 +50,8 @@ store := redis.New(redis.Config{
|
|||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
TLSConfig: nil,
|
TLSConfig: nil,
|
||||||
}
|
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
||||||
|
})
|
||||||
|
|
||||||
// or just the url with all information
|
// or just the url with all information
|
||||||
store = redis.New(redis.Config{
|
store = redis.New(redis.Config{
|
||||||
@@ -68,7 +70,7 @@ type Config struct {
|
|||||||
|
|
||||||
// Port where the DB is listening on
|
// Port where the DB is listening on
|
||||||
//
|
//
|
||||||
// Optional. Default is 3306
|
// Optional. Default is 6379
|
||||||
Port int
|
Port int
|
||||||
|
|
||||||
// Server username
|
// Server username
|
||||||
@@ -86,11 +88,11 @@ type Config struct {
|
|||||||
// Optional. Default is 0
|
// Optional. Default is 0
|
||||||
Database int
|
Database int
|
||||||
|
|
||||||
// URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
|
// URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
|
||||||
//
|
//
|
||||||
// Example: redis://<user>:<pass>@localhost:6379/<db>
|
// Example: redis://<user>:<pass>@localhost:6379/<db>
|
||||||
// Optional. Default is ""
|
// Optional. Default is ""
|
||||||
URL string
|
URL string
|
||||||
|
|
||||||
// Reset clears any existing keys in existing Collection
|
// Reset clears any existing keys in existing Collection
|
||||||
//
|
//
|
||||||
@@ -101,6 +103,11 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Optional. Default is nil
|
// Optional. Default is nil
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
|
|
||||||
|
// Maximum number of socket connections.
|
||||||
|
//
|
||||||
|
// Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
|
||||||
|
PoolSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -116,5 +123,6 @@ var ConfigDefault = Config{
|
|||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
TLSConfig: nil,
|
TLSConfig: nil,
|
||||||
|
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import "crypto/tls"
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -11,7 +14,7 @@ type Config struct {
|
|||||||
|
|
||||||
// Port where the DB is listening on
|
// Port where the DB is listening on
|
||||||
//
|
//
|
||||||
// Optional. Default is 3306
|
// Optional. Default is 6379
|
||||||
Port int
|
Port int
|
||||||
|
|
||||||
// Server username
|
// Server username
|
||||||
@@ -43,6 +46,11 @@ type Config struct {
|
|||||||
// TLS Config to use. When set TLS will be negotiated.
|
// TLS Config to use. When set TLS will be negotiated.
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
|
|
||||||
|
// Maximum number of socket connections.
|
||||||
|
//
|
||||||
|
// Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
|
||||||
|
PoolSize int
|
||||||
|
|
||||||
////////////////////////////////////
|
////////////////////////////////////
|
||||||
// Adaptor related config options //
|
// Adaptor related config options //
|
||||||
////////////////////////////////////
|
////////////////////////////////////
|
||||||
@@ -60,6 +68,7 @@ var ConfigDefault = Config{
|
|||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
TLSConfig: nil,
|
TLSConfig: nil,
|
||||||
|
PoolSize: 10 * runtime.GOMAXPROCS(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to set default values
|
// Helper function to set default values
|
||||||
|
@@ -3,6 +3,7 @@ module github.com/gofiber/storage/redis
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-redis/redis/v8 v8.11.4
|
github.com/go-redis/redis/v8 v8.11.5
|
||||||
github.com/gofiber/utils v0.1.2
|
github.com/gofiber/utils v0.1.2
|
||||||
|
github.com/google/go-cmp v0.5.6 // indirect
|
||||||
)
|
)
|
||||||
|
23
redis/go.sum
23
redis/go.sum
@@ -1,5 +1,8 @@
|
|||||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||||
|
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
@@ -7,8 +10,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
|
|||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg=
|
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||||
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
|
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||||
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
|
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
|
||||||
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
@@ -28,18 +31,24 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
|
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
||||||
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
|
|
||||||
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
|
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
|
||||||
|
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||||
|
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
|
||||||
|
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
|
||||||
|
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
|
||||||
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
||||||
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||||
github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c=
|
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
|
||||||
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
|
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
||||||
|
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
@@ -64,12 +73,14 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
@@ -19,16 +19,18 @@ func New(config ...Config) *Storage {
|
|||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
// Create new redis client
|
// Create new redis client
|
||||||
|
|
||||||
var options *redis.Options
|
var options *redis.Options
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if cfg.URL != "" {
|
if cfg.URL != "" {
|
||||||
options, err = redis.ParseURL(cfg.URL)
|
options, err = redis.ParseURL(cfg.URL)
|
||||||
options.TLSConfig = cfg.TLSConfig
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.TLSConfig = cfg.TLSConfig
|
||||||
|
options.PoolSize = cfg.PoolSize
|
||||||
} else {
|
} else {
|
||||||
options = &redis.Options{
|
options = &redis.Options{
|
||||||
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
|
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
|
||||||
@@ -36,6 +38,7 @@ func New(config ...Config) *Storage {
|
|||||||
Username: cfg.Username,
|
Username: cfg.Username,
|
||||||
Password: cfg.Password,
|
Password: cfg.Password,
|
||||||
TLSConfig: cfg.TLSConfig,
|
TLSConfig: cfg.TLSConfig,
|
||||||
|
PoolSize: cfg.PoolSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +74,6 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
|||||||
return val, err
|
return val, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set key with value
|
|
||||||
// Set key with value
|
// Set key with value
|
||||||
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
||||||
// Ain't Nobody Got Time For That
|
// Ain't Nobody Got Time For That
|
||||||
@@ -99,3 +101,8 @@ func (s *Storage) Reset() error {
|
|||||||
func (s *Storage) Close() error {
|
func (s *Storage) Close() error {
|
||||||
return s.db.Close()
|
return s.db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *redis.Client {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -123,6 +123,10 @@ func Test_Redis_Close(t *testing.T) {
|
|||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Redis_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Redis_Initalize_WithURL(t *testing.T) {
|
func Test_Redis_Initalize_WithURL(t *testing.T) {
|
||||||
testStoreUrl := New(Config{
|
testStoreUrl := New(Config{
|
||||||
URL: "redis://localhost:6379",
|
URL: "redis://localhost:6379",
|
||||||
|
@@ -18,6 +18,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *ristretto.Cache
|
||||||
```
|
```
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
@@ -89,3 +89,8 @@ func (s *Storage) Close() error {
|
|||||||
s.cache.Close()
|
s.cache.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *ristretto.Cache {
|
||||||
|
return s.cache
|
||||||
|
}
|
||||||
|
@@ -42,6 +42,9 @@ func Test_Ristretto_Get(t *testing.T) {
|
|||||||
err := testStore.Set(key, val, 0)
|
err := testStore.Set(key, val, 0)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
// stabilize with some delay in between -> bug already communicated
|
||||||
|
time.Sleep(10000)
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
result, err := testStore.Get(key)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, val, result)
|
utils.AssertEqual(t, val, result)
|
||||||
@@ -86,6 +89,9 @@ func Test_Ristretto_Delete(t *testing.T) {
|
|||||||
err := testStore.Set(key, val, 0)
|
err := testStore.Set(key, val, 0)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
// stabilize with some delay in between -> bug already communicated
|
||||||
|
time.Sleep(10000)
|
||||||
|
|
||||||
err = testStore.Delete(key)
|
err = testStore.Delete(key)
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
@@ -120,3 +126,7 @@ func Test_Ristretto_Reset(t *testing.T) {
|
|||||||
func Test_Ristretto_Close(t *testing.T) {
|
func Test_Ristretto_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Ristretto_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
||||||
|
109
s3/README.md
Normal file
109
s3/README.md
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
# S3
|
||||||
|
|
||||||
|
A S3 storage driver using [aws/aws-sdk-go-v2](https://github.com/aws/aws-sdk-go-v2).
|
||||||
|
|
||||||
|
**Note:** If config fields of credentials not given, credentials are using from the environment variables, ~/.aws/credentials, or EC2 instance role. If config fields of credentials given, credentials are using from config. Look at: [specifying credentials](https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials)
|
||||||
|
|
||||||
|
|
||||||
|
### Table of Contents
|
||||||
|
- [Signatures](#signatures)
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Config](#config)
|
||||||
|
- [Default Config](#default-config)
|
||||||
|
|
||||||
|
### Signatures
|
||||||
|
```go
|
||||||
|
func New(config ...Config) Storage
|
||||||
|
func (s *Storage) Get(key string) ([]byte, error)
|
||||||
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
||||||
|
func (s *Storage) Delete(key string) error
|
||||||
|
func (s *Storage) Reset() error
|
||||||
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *s3.Client
|
||||||
|
```
|
||||||
|
### Installation
|
||||||
|
S3 is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
|
```bash
|
||||||
|
go mod init github.com/<user>/<repo>
|
||||||
|
```
|
||||||
|
And then install the s3 implementation:
|
||||||
|
```bash
|
||||||
|
go get github.com/gofiber/storage/s3
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Import the storage package.
|
||||||
|
```go
|
||||||
|
import "github.com/gofiber/storage/s3"
|
||||||
|
```
|
||||||
|
|
||||||
|
You can use the following possibilities to create a storage:
|
||||||
|
```go
|
||||||
|
// Initialize default config
|
||||||
|
store := s3.New()
|
||||||
|
|
||||||
|
// Initialize custom config
|
||||||
|
store := s3.New(s3.Config{
|
||||||
|
Bucket: "my-bucket-url",
|
||||||
|
Endpoint: "my-endpoint",
|
||||||
|
Region: "my-region",
|
||||||
|
Reset: false,
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Config
|
||||||
|
```go
|
||||||
|
// Config defines the config for storage.
|
||||||
|
type Config struct {
|
||||||
|
// S3 bucket name
|
||||||
|
Bucket string
|
||||||
|
|
||||||
|
// AWS endpoint
|
||||||
|
Endpoint string
|
||||||
|
|
||||||
|
// AWS region
|
||||||
|
Region string
|
||||||
|
|
||||||
|
// Request timeout
|
||||||
|
//
|
||||||
|
// Optional. Default is 0 (no timeout)
|
||||||
|
RequestTimeout time.Duration
|
||||||
|
|
||||||
|
// Reset clears any existing keys in existing Bucket
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
Reset bool
|
||||||
|
|
||||||
|
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
|
||||||
|
//
|
||||||
|
// Optional. Default is Credentials{}
|
||||||
|
Credentials Credentials
|
||||||
|
|
||||||
|
// The maximum number of times requests that encounter retryable failures should be attempted.
|
||||||
|
//
|
||||||
|
// Optional. Default is 3
|
||||||
|
MaxAttempts int
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
type Credentials struct {
|
||||||
|
AccessKey string
|
||||||
|
SecretAccessKey string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Default Config
|
||||||
|
The default configuration lacks Bucket, Region, and Endpoint which are all required and must be overwritten:
|
||||||
|
```go
|
||||||
|
// ConfigDefault is the default config
|
||||||
|
var ConfigDefault = Config{
|
||||||
|
Bucket: "",
|
||||||
|
Region: "",
|
||||||
|
Endpoint: "",
|
||||||
|
Credentials: Credentials{},
|
||||||
|
MaxAttempts: 3,
|
||||||
|
RequestTimeout: 0,
|
||||||
|
Reset: false,
|
||||||
|
}
|
||||||
|
```
|
69
s3/config.go
Normal file
69
s3/config.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package s3
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Config defines the config for storage.
|
||||||
|
type Config struct {
|
||||||
|
// S3 bucket name
|
||||||
|
Bucket string
|
||||||
|
|
||||||
|
// AWS endpoint
|
||||||
|
Endpoint string
|
||||||
|
|
||||||
|
// AWS region
|
||||||
|
Region string
|
||||||
|
|
||||||
|
// Request timeout
|
||||||
|
//
|
||||||
|
// Optional. Default is 0 (no timeout)
|
||||||
|
RequestTimeout time.Duration
|
||||||
|
|
||||||
|
// Reset clears any existing keys in existing Bucket
|
||||||
|
//
|
||||||
|
// Optional. Default is false
|
||||||
|
Reset bool
|
||||||
|
|
||||||
|
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
|
||||||
|
//
|
||||||
|
// Optional. Default is Credentials{}
|
||||||
|
Credentials Credentials
|
||||||
|
|
||||||
|
// The maximum number of times requests that encounter retryable failures should be attempted.
|
||||||
|
//
|
||||||
|
// Optional. Default is 3
|
||||||
|
MaxAttempts int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Credentials struct {
|
||||||
|
AccessKey string
|
||||||
|
SecretAccessKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigDefault is the default config
|
||||||
|
var ConfigDefault = Config{
|
||||||
|
Bucket: "",
|
||||||
|
Region: "",
|
||||||
|
Endpoint: "",
|
||||||
|
Credentials: Credentials{},
|
||||||
|
MaxAttempts: 3,
|
||||||
|
RequestTimeout: 0,
|
||||||
|
Reset: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to set default values
|
||||||
|
func configDefault(config ...Config) Config {
|
||||||
|
// Return default config if nothing provided
|
||||||
|
if len(config) < 1 {
|
||||||
|
return ConfigDefault
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override default config
|
||||||
|
cfg := config[0]
|
||||||
|
|
||||||
|
// Set default values
|
||||||
|
if cfg.Bucket == "" {
|
||||||
|
cfg.Bucket = ConfigDefault.Bucket
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
12
s3/go.mod
Normal file
12
s3/go.mod
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module github.com/gofiber/storage/s3
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/aws/aws-sdk-go-v2 v1.16.16
|
||||||
|
github.com/aws/aws-sdk-go-v2/config v1.17.8
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.12.21
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.34
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.27.11
|
||||||
|
github.com/gofiber/utils v0.1.2
|
||||||
|
)
|
54
s3/go.sum
Normal file
54
s3/go.sum
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
github.com/aws/aws-sdk-go-v2 v1.16.16 h1:M1fj4FE2lB4NzRb9Y0xdWsn2P0+2UHVxwKyOa4YJNjk=
|
||||||
|
github.com/aws/aws-sdk-go-v2 v1.16.16/go.mod h1:SwiyXi/1zTUZ6KIAmLK5V5ll8SiURNUYOqTerZPaF9k=
|
||||||
|
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.8 h1:tcFliCWne+zOuUfKNRn8JdFBuWPDuISDH08wD2ULkhk=
|
||||||
|
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.8/go.mod h1:JTnlBSot91steJeti4ryyu/tLd4Sk84O5W22L7O2EQU=
|
||||||
|
github.com/aws/aws-sdk-go-v2/config v1.17.8 h1:b9LGqNnOdg9vR4Q43tBTVWk4J6F+W774MSchvKJsqnE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/config v1.17.8/go.mod h1:UkCI3kb0sCdvtjiXYiU4Zx5h07BOpgBTtkPu/49r+kA=
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.12.21 h1:4tjlyCD0hRGNQivh5dN8hbP30qQhMLBE/FgQR1vHHWM=
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.12.21/go.mod h1:O+4XyAt4e+oBAoIwNUYkRg3CVMscaIJdmZBOcPgJ8D8=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.17 h1:r08j4sbZu/RVi+BNxkBJwPMUYY3P8mgSDuKkZ/ZN1lE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.17/go.mod h1:yIkQcCDYNsZfXpd5UX2Cy+sWA1jPgIhGTw9cOBzfVnQ=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.34 h1:1PNtaCM+2ruo1dfYL2RweUdtbuPvinjAejjNcPa/RQY=
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.34/go.mod h1:+Six+CXNHYllXam32j+YW8ixk82+am345ei89kEz8p4=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.23 h1:s4g/wnzMf+qepSNgTvaQQHNxyMLKSawNhKCPNy++2xY=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.23/go.mod h1:2DFxAQ9pfIRy0imBCJv+vZ2X6RKxves6fbnEuSry6b4=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.17 h1:/K482T5A3623WJgWT8w1yRAFK4RzGzEl7y39yhtn9eA=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.17/go.mod h1:pRwaTYCJemADaqCbUAxltMoHKata7hmB5PjEXeu0kfg=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.24 h1:wj5Rwc05hvUSvKuOF29IYb9QrCLjU+rHAy/x/o0DK2c=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.24/go.mod h1:jULHjqqjDlbyTa7pfM7WICATnOv+iOhjletM3N0Xbu8=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.14 h1:ZSIPAkAsCCjYrhqfw2+lNzWDzxzHXEckFkTePL5RSWQ=
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.14/go.mod h1:AyGgqiKv9ECM6IZeNQtdT8NnMvUb3/2wokeq2Fgryto=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.9 h1:Lh1AShsuIJTwMkoxVCAYPJgNG5H+eN6SmoUn8nOZ5wE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.9/go.mod h1:a9j48l6yL5XINLHLcOKInjdvknN+vWqPBxqeIDw7ktw=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.18 h1:BBYoNQt2kUZUUK4bIPsKrCcjVPUMNsgQpNAwhznK/zo=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.18/go.mod h1:NS55eQ4YixUJPTC+INxi2/jCqe1y2Uw3rnh9wEOVJxY=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.17 h1:Jrd/oMh0PKQc6+BowB+pLEwLIgaQF29eYbe7E1Av9Ug=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.17/go.mod h1:4nYOrY41Lrbk2170/BGkcJKBhws9Pfn8MG3aGqjjeFI=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.17 h1:HfVVR1vItaG6le+Bpw6P4midjBDMKnjMyZnw9MXYUcE=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.17/go.mod h1:YqMdV+gEKCQ59NrB7rzrJdALeBIsYiVi8Inj3+KcqHI=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.27.11 h1:3/gm/JTX9bX8CpzTgIlrtYpB3EVBDxyg/GY/QdcIEZw=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.27.11/go.mod h1:fmgDANqTUCxciViKl9hb/zD5LFbvPINFRgWhDbR+vZo=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sso v1.11.23 h1:pwvCchFUEnlceKIgPUouBJwK81aCkQ8UDMORfeFtW10=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sso v1.11.23/go.mod h1:/w0eg9IhFGjGyyncHIQrXtU8wvNsTJOP0R6PPj0wf80=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.6 h1:OwhhKc1P9ElfWbMKPIbMMZBV6hzJlL2JKD76wNNVzgQ=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.6/go.mod h1:csZuQY65DAdFBt1oIjO5hhBR49kQqop4+lcuCjf2arA=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sts v1.16.19 h1:9pPi0PsFNAGILFfPCk8Y0iyEBGc6lu6OQ97U7hmdesg=
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/sts v1.16.19/go.mod h1:h4J3oPZQbxLhzGnk+j9dfYHi5qIOVJ5kczZd658/ydM=
|
||||||
|
github.com/aws/smithy-go v1.13.3 h1:l7LYxGuzK6/K+NzJ2mC+VvLUbae0sL3bXU//04MkmnA=
|
||||||
|
github.com/aws/smithy-go v1.13.3/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
|
||||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
|
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||||
|
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||||
|
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
|
||||||
|
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
207
s3/s3.go
Normal file
207
s3/s3.go
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
package s3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/aws/retry"
|
||||||
|
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Storage interface that is implemented by storage providers
|
||||||
|
type Storage struct {
|
||||||
|
svc *s3.Client
|
||||||
|
downloader *manager.Downloader
|
||||||
|
uploader *manager.Uploader
|
||||||
|
requestTimeout time.Duration
|
||||||
|
bucket string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new storage
|
||||||
|
func New(config ...Config) *Storage {
|
||||||
|
// Set default config
|
||||||
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
|
// Create s3 session
|
||||||
|
// If config fields of credentials not given, credentials are using from the environment variables, ~/.aws/credentials, or EC2 instance role.
|
||||||
|
// If config fields of credentials given, credentials are using from config.
|
||||||
|
//
|
||||||
|
// Look at: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials
|
||||||
|
awscfg, err := returnAWSConfig(cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("unable to load SDK config, %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
sess := s3.NewFromConfig(awscfg)
|
||||||
|
storage := &Storage{
|
||||||
|
svc: sess,
|
||||||
|
downloader: manager.NewDownloader(sess),
|
||||||
|
uploader: manager.NewUploader(sess),
|
||||||
|
requestTimeout: cfg.RequestTimeout,
|
||||||
|
bucket: cfg.Bucket,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset all entries if set to true
|
||||||
|
if cfg.Reset {
|
||||||
|
if err := storage.Reset(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return storage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get value by key
|
||||||
|
func (s *Storage) Get(key string) ([]byte, error) {
|
||||||
|
var nsk *types.NoSuchKey
|
||||||
|
|
||||||
|
if len(key) <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
buf := manager.NewWriteAtBuffer([]byte{})
|
||||||
|
|
||||||
|
_, err := s.downloader.Download(ctx, buf, &s3.GetObjectInput{
|
||||||
|
Bucket: &s.bucket,
|
||||||
|
Key: aws.String(key),
|
||||||
|
})
|
||||||
|
if errors.As(err, &nsk) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set key with value
|
||||||
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
||||||
|
if len(key) <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := s.uploader.Upload(ctx, &s3.PutObjectInput{
|
||||||
|
Bucket: &s.bucket,
|
||||||
|
Key: aws.String(key),
|
||||||
|
Body: bytes.NewReader(val),
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete entry by key
|
||||||
|
func (s *Storage) Delete(key string) error {
|
||||||
|
if len(key) <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := s.svc.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||||
|
Bucket: &s.bucket,
|
||||||
|
Key: aws.String(key),
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset all entries, including unexpired
|
||||||
|
func (s *Storage) Reset() error {
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
paginator := s3.NewListObjectsV2Paginator(s.svc, &s3.ListObjectsV2Input{
|
||||||
|
Bucket: &s.bucket,
|
||||||
|
})
|
||||||
|
|
||||||
|
for paginator.HasMorePages() {
|
||||||
|
page, err := paginator.NextPage(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var objects []types.ObjectIdentifier
|
||||||
|
for _, object := range page.Contents {
|
||||||
|
objects = append(objects, types.ObjectIdentifier{
|
||||||
|
Key: object.Key,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.svc.DeleteObjects(ctx, &s3.DeleteObjectsInput{
|
||||||
|
Bucket: &s.bucket,
|
||||||
|
Delete: &types.Delete{
|
||||||
|
Objects: objects,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the database
|
||||||
|
func (s *Storage) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *s3.Client {
|
||||||
|
return s.svc
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context for making requests will timeout if a non-zero timeout is configured
|
||||||
|
func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
|
||||||
|
if s.requestTimeout > 0 {
|
||||||
|
return context.WithTimeout(context.Background(), s.requestTimeout)
|
||||||
|
}
|
||||||
|
return context.Background(), func() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func returnAWSConfig(cfg Config) (aws.Config, error) {
|
||||||
|
endpoint := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
||||||
|
if cfg.Endpoint != "" {
|
||||||
|
return aws.Endpoint{
|
||||||
|
PartitionID: "aws",
|
||||||
|
URL: cfg.Endpoint,
|
||||||
|
SigningRegion: cfg.Region,
|
||||||
|
HostnameImmutable: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
|
||||||
|
})
|
||||||
|
|
||||||
|
if cfg.Credentials != (Credentials{}) {
|
||||||
|
credentials := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
|
||||||
|
return awsconfig.LoadDefaultConfig(context.TODO(),
|
||||||
|
awsconfig.WithRegion(cfg.Region),
|
||||||
|
awsconfig.WithEndpointResolverWithOptions(endpoint),
|
||||||
|
awsconfig.WithCredentialsProvider(credentials),
|
||||||
|
awsconfig.WithRetryer(func() aws.Retryer {
|
||||||
|
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return awsconfig.LoadDefaultConfig(context.TODO(),
|
||||||
|
awsconfig.WithRegion(cfg.Region),
|
||||||
|
awsconfig.WithEndpointResolverWithOptions(endpoint),
|
||||||
|
awsconfig.WithRetryer(func() aws.Retryer {
|
||||||
|
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
111
s3/s3_test.go
Normal file
111
s3/s3_test.go
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package s3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gofiber/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testStore = New(
|
||||||
|
Config{
|
||||||
|
Bucket: "testbucket",
|
||||||
|
Endpoint: "http://127.0.0.1:9000/",
|
||||||
|
Region: "us-east-1",
|
||||||
|
Credentials: Credentials{
|
||||||
|
AccessKey: "minioadmin",
|
||||||
|
SecretAccessKey: "minioadmin",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_S3_Set(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Set_Override(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Get(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, val, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Get_NotExist(t *testing.T) {
|
||||||
|
|
||||||
|
result, err := testStore.Get("notexist")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Delete(t *testing.T) {
|
||||||
|
var (
|
||||||
|
key = "john"
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set(key, val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Delete(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Reset(t *testing.T) {
|
||||||
|
var (
|
||||||
|
val = []byte("doe")
|
||||||
|
)
|
||||||
|
|
||||||
|
err := testStore.Set("john1", val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Set("john2", val, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
err = testStore.Reset()
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
result, err := testStore.Get("john1")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
|
||||||
|
result, err = testStore.Get("john2")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, len(result) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Close(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_S3_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
@@ -17,6 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
func (s *Storage) Conn() *sql.DB
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
SQLite3 is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
SQLite3 is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
|
@@ -4,5 +4,5 @@ go 1.14
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gofiber/utils v0.1.2
|
github.com/gofiber/utils v0.1.2
|
||||||
github.com/mattn/go-sqlite3 v1.14.10
|
github.com/mattn/go-sqlite3 v1.14.15
|
||||||
)
|
)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
|
||||||
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
||||||
github.com/mattn/go-sqlite3 v1.14.10 h1:MLn+5bFRlWMGoSRmJour3CL1w/qL96mvipqpwQW/Sfk=
|
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
|
||||||
github.com/mattn/go-sqlite3 v1.14.10/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
|
@@ -169,3 +169,8 @@ func (s *Storage) gcTicker() {
|
|||||||
func (s *Storage) gc(t time.Time) {
|
func (s *Storage) gc(t time.Time) {
|
||||||
_, _ = s.db.Exec(s.sqlGC, t.Unix())
|
_, _ = s.db.Exec(s.sqlGC, t.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return database client
|
||||||
|
func (s *Storage) Conn() *sql.DB {
|
||||||
|
return s.db
|
||||||
|
}
|
||||||
|
@@ -159,3 +159,7 @@ func Test_SQLite3_Non_UTF8(t *testing.T) {
|
|||||||
func Test_SQLite3_Close(t *testing.T) {
|
func Test_SQLite3_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_SQLite3_Conn(t *testing.T) {
|
||||||
|
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||||
|
}
|
Reference in New Issue
Block a user