Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new storage implementation: Couchbase #841

Merged
merged 22 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a5b87a0
add couchbase storage
mstrYoda May 29, 2023
ae6582d
Merge branch 'main' into main
gaby May 30, 2023
544eab8
update go.mod for couchbase & add security workflow for couchbase
mstrYoda May 31, 2023
c90677d
Merge branch 'main' of github.com:mstrYoda/fiber-storage
mstrYoda May 31, 2023
b031cab
add couchbase test docker couchbase setup step
mstrYoda Jun 24, 2023
1af4aa7
add couchbase tests to read me
mstrYoda Jun 24, 2023
6a8f254
add exclude-dir=couchbase to github workflow security
mstrYoda Jun 24, 2023
821232a
resolve conflicts
mstrYoda Jun 24, 2023
426e826
marge upstream main into current
mstrYoda Jun 24, 2023
e1214a9
add couchbase to gosec & change docker run tty
mstrYoda Jun 24, 2023
cdd6008
add sleep between couchbase commands to wait for cluster init
mstrYoda Jun 25, 2023
b892818
increase cb test wait time
mstrYoda Jun 26, 2023
a276545
Merge branch 'main' into main
ReneWerner87 Jun 26, 2023
19ee2e6
increase couchbase test wait time
mstrYoda Jun 26, 2023
cd292b6
Merge branch 'main' of github.com:mstrYoda/fiber-storage
mstrYoda Jun 26, 2023
448c43f
Update test-couchbase.yml
gaby Jun 28, 2023
201e025
Remove cache, fix path to test files
gaby Jun 28, 2023
e19782f
fix couchbase_test delete err not found is already handled by code so…
mstrYoda Jun 28, 2023
c651866
Merge branch 'main' of github.com:mstrYoda/fiber-storage
mstrYoda Jun 28, 2023
cc87dce
add enable flush to create bucket for couchbase test
mstrYoda Jun 28, 2023
15d7b9e
test couchbase reset test for doc not found
mstrYoda Jun 28, 2023
3a98e86
upgrade to go 1.19 couchbase
mstrYoda Jun 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ updates:
- "🤖 Dependencies"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/couchbase/" # Location of package manifests
labels:
- "🤖 Dependencies"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/pebble/" # Location of package manifests
labels:
Expand Down
43 changes: 43 additions & 0 deletions .github/release-drafter-couchbase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name-template: 'Couchbase - v$RESOLVED_VERSION'
tag-template: 'couchbase/v$RESOLVED_VERSION'
tag-prefix: couchbase/v
include-paths:
- couchbase
categories:
- title: '🚀 New'
labels:
- '✏️ Feature'
- title: '🧹 Updates'
labels:
- '🧹 Updates'
- '🤖 Dependencies'
- title: '🐛 Fixes'
labels:
- '☢️ Bug'
- title: '📚 Documentation'
labels:
- '📒 Documentation'
change-template: '- $TITLE (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
version-resolver:
major:
labels:
- 'major'
minor:
labels:
- 'minor'
- '✏️ Feature'
patch:
labels:
- 'patch'
- '📒 Documentation'
- '☢️ Bug'
- '🤖 Dependencies'
- '🧹 Updates'
default: patch
template: |
$CHANGES

**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...couchbase/v$RESOLVED_VERSION

Thank you $CONTRIBUTORS for making this update possible.
6 changes: 5 additions & 1 deletion .github/workflows/gosec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Run Gosec (root)
working-directory: .
run: |
gosec -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 -exclude-dir=azureblob -exclude-dir=mssql -exclude-dir=pebble ./....
gosec -exclude-dir=arangodb -exclude-dir=badger -exclude-dir=couchbase -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 -exclude-dir=azureblob -exclude-dir=mssql -exclude-dir=pebble ./....
# -----
- name: Run Gosec (arangodb)
working-directory: ./arangodb
Expand All @@ -48,6 +48,10 @@ jobs:
working-directory: ./badger
run: gosec ./...
# -----
- name: Run Gosec (couchbase)
working-directory: ./couchbase
run: gosec ./...
# -----
- name: Run Gosec (dynamodb)
working-directory: ./dynamodb
run: gosec ./...
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/release-drafter-couchbase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release Drafter Couchbase
on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- master
- main
paths:
- 'couchbase/**'
jobs:
draft_release_couchbase:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: release-drafter/release-drafter@v5
with:
config-name: release-drafter-couchbase.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Empty file.
35 changes: 35 additions & 0 deletions .github/workflows/test-couchbase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
on:
push:
branches:
- master
- main
paths:
- 'couchbase/**'
pull_request:
paths:
- 'couchbase/**'
name: "Tests Couchbase"
jobs:
Tests:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- 1.19.x
- 1.20.x
steps:
- name: Fetch Repository
uses: actions/checkout@v3
- name: Install couchbase
run: |
docker run --name couchbase -d -p 8091-8097:8091-8097 -p 9123:9123 -p 11207:11207 -p 11210:11210 -p 11280:11280 -p 18091-18097:18091-18097 couchbase:enterprise-7.1.1
sleep 10
docker exec --tty couchbase couchbase-cli cluster-init -c localhost:8091 --cluster-username admin --cluster-password 123456 --cluster-ramsize 256 --services data
sleep 10
docker exec --tty couchbase couchbase-cli bucket-create -c localhost:8091 --username admin --password 123456 --bucket fiber_storage --bucket-type couchbase --bucket-ramsize 100
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '${{ matrix.go-version }}'
- name: Run Test
run: cd ./couchbase && go test ./... -v -race
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type Storage interface {
- [Bbolt](/bbolt) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Bbolt%22">
<img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-bbolt.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
- [Couchbase](/couchbase) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Couchbase%22">
<img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-couchbase.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
- [DynamoDB](/dynamodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+DynamoDB%22">
<img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-dynamodb.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
Expand Down
83 changes: 83 additions & 0 deletions couchbase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Couchbase

A Couchbase storage driver using [couchbase/gocb](https://github.com/couchbase/gocb).

### 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() *gocb.Cluster
```
### Installation
Couchbase 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 Couchbase implementation:
```bash
go get github.com/gofiber/storage/couchbase
```

### Examples
Import the storage package.
```go
import "github.com/gofiber/storage/couchbase"
```

You can use the following possibilities to create a storage:
```go
// Initialize default config
store := couchbase.New()

// Initialize Couchbase storage with custom config
store := couchbase.New(couchbase.Config{
Host: "127.0.0.1:8091",
Username: "",
Password: "",
Bucket: 0,
ConnectionTimeout: 3* time.Second,
KVTimeout: 1* time.Second,
})
```

### Config
```go
type Config struct {
// The application username to Connect to the Couchbase cluster
Username string
// The application password to Connect to the Couchbase cluster
Password string
// The connection string for the Couchbase cluster
Host string
// The name of the bucket to Connect to
Bucket string
// The timeout for connecting to the Couchbase cluster
ConnectionTimeout time.Duration
// The timeout for performing operations on the Couchbase cluster
KVTimeout time.Duration
}
```

### Default Config
```go
// ConfigDefault is the default config
var ConfigDefault = Config{
Host: "127.0.0.1:8091",
Username: "admin",
Password: "123456",
Bucket: "fiber_storage",
ConnectionTimeout: 3 * time.Second,
KVTimeout: 1 * time.Second,
}
```
62 changes: 62 additions & 0 deletions couchbase/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package couchbase

import (
"time"
)

type Config struct {
// The application username to Connect to the Couchbase cluster
Username string
// The application password to Connect to the Couchbase cluster
Password string
// The connection string for the Couchbase cluster
Host string
// The name of the bucket to Connect to
Bucket string
// The timeout for connecting to the Couchbase cluster
ConnectionTimeout time.Duration
// The timeout for performing operations on the Couchbase cluster
KVTimeout time.Duration
}

// ConfigDefault is the default config
var ConfigDefault = Config{
Host: "127.0.0.1:8091",
Username: "admin",
Password: "123456",
Bucket: "fiber_storage",
ConnectionTimeout: 3 * time.Second,
KVTimeout: 1 * time.Second,
}

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.Username == "" {
cfg.Username = ConfigDefault.Username
}
if cfg.Password == "" {
cfg.Password = ConfigDefault.Password
}
if cfg.Host == "" {
cfg.Host = ConfigDefault.Host
}
if cfg.Bucket == "" {
cfg.Bucket = ConfigDefault.Bucket
}
if cfg.ConnectionTimeout == 0 {
cfg.ConnectionTimeout = ConfigDefault.ConnectionTimeout
}
if cfg.KVTimeout == 0 {
cfg.KVTimeout = ConfigDefault.KVTimeout
}

return cfg
}
97 changes: 97 additions & 0 deletions couchbase/couchbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package couchbase

import (
"time"

"github.com/couchbase/gocb/v2"
)

type Storage struct {
cb *gocb.Cluster
bucket *gocb.Bucket
}

func New(config ...Config) *Storage {
// Set default config
cfg := configDefault(config...)

cb, err := gocb.Connect(cfg.Host, gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: cfg.Username,
Password: cfg.Password,
},
TimeoutsConfig: gocb.TimeoutsConfig{
ConnectTimeout: cfg.ConnectionTimeout,
KVTimeout: cfg.KVTimeout,
},
Transcoder: gocb.NewLegacyTranscoder(),
})

if err != nil {
panic(err)
}

_, err = cb.Ping(&gocb.PingOptions{
Timeout: cfg.ConnectionTimeout,
})

if err != nil {
panic(err)
}

b := cb.Bucket(cfg.Bucket)

return &Storage{cb: cb, bucket: b}
}

func (s *Storage) Get(key string) ([]byte, error) {
out, err := s.bucket.DefaultCollection().Get(key, nil)
if err != nil {
switch e := err.(type) {
case *gocb.KeyValueError:
if e.InnerError.Error() == gocb.ErrDocumentNotFound.Error() {
return nil, nil
}
default: //*gocb.TimeoutError,...
return nil, err
}

return nil, err
mstrYoda marked this conversation as resolved.
Show resolved Hide resolved
}

var value []byte
if err := out.Content(&value); err != nil {
return nil, err
}

return value, nil
}

func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
if _, err := s.bucket.DefaultCollection().Upsert(key, val, &gocb.UpsertOptions{
Expiry: exp,
}); err != nil {
return err
}

return nil
}

func (s *Storage) Delete(key string) error {
if _, err := s.bucket.DefaultCollection().Remove(key, nil); err != nil {
return err
}
return nil
}

func (s *Storage) Reset() error {
return s.cb.Buckets().FlushBucket(s.bucket.Name(), nil)
}

func (s *Storage) Close() error {
return s.cb.Close(nil)
}

func (s *Storage) Conn() *gocb.Cluster {
efectn marked this conversation as resolved.
Show resolved Hide resolved
return s.cb
}
Loading