From ed0024efa1108358086c218aa9fbacb6175746cb Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 24 Sep 2014 21:46:09 -0700 Subject: [PATCH] added blobstore and capability packages --- server/blobstore/blobsql/blob.go | 9 +++++ server/blobstore/blobsql/blobstore.go | 55 +++++++++++++++++++++++++++ server/blobstore/blobsql/const.go | 18 +++++++++ server/blobstore/blobsql/context.go | 11 ++++++ server/blobstore/blobstore.go | 55 +++++++++++++++++++++++++++ server/blobstore/context.go | 31 +++++++++++++++ server/capability/capability.go | 23 +++++++++++ server/capability/context.go | 32 ++++++++++++++++ 8 files changed, 234 insertions(+) create mode 100644 server/blobstore/blobsql/blob.go create mode 100644 server/blobstore/blobsql/blobstore.go create mode 100644 server/blobstore/blobsql/const.go create mode 100644 server/blobstore/blobsql/context.go create mode 100644 server/blobstore/blobstore.go create mode 100644 server/blobstore/context.go create mode 100644 server/capability/capability.go create mode 100644 server/capability/context.go diff --git a/server/blobstore/blobsql/blob.go b/server/blobstore/blobsql/blob.go new file mode 100644 index 00000000..0cd226b1 --- /dev/null +++ b/server/blobstore/blobsql/blob.go @@ -0,0 +1,9 @@ +package blobsql + +type Blob struct { + ID int64 `meddler:"blob_id,pk" orm:"column(blob_id);pk;auto"` + Path string `meddler:"blob_path" orm:"column(blob_path);size(2000);unique"` + Data string `meddler:"blob_data,gobgzip" orm:"column(blob_data);type(text)"` +} + +func (b *Blob) TableName() string { return "blobs" } diff --git a/server/blobstore/blobsql/blobstore.go b/server/blobstore/blobsql/blobstore.go new file mode 100644 index 00000000..708e2cb9 --- /dev/null +++ b/server/blobstore/blobsql/blobstore.go @@ -0,0 +1,55 @@ +package blobsql + +import ( + "bytes" + "io" + "io/ioutil" + + "github.com/russross/meddler" +) + +type Blobstore struct { + meddler.DB +} + +// Del removes an object from the blobstore. +func (b *Blobstore) Del(path string) error { + var _, err = b.Exec(deleteBlob, path) + return err +} + +// Get retrieves an object from the blobstore. +func (b *Blobstore) Get(path string) ([]byte, error) { + var blob = Blob{} + var err = meddler.QueryRow(b, &blob, queryBlob, path) + return []byte(blob.Data), err +} + +// GetReader retrieves an object from the blobstore. +// It is the caller's responsibility to call Close on +// the ReadCloser when finished reading. +func (b *Blobstore) GetReader(path string) (io.ReadCloser, error) { + var blob, err = b.Get(path) + var buf = bytes.NewBuffer(blob) + return ioutil.NopCloser(buf), err +} + +// Put inserts an object into the blobstore. +func (b *Blobstore) Put(path string, data []byte) error { + var blob = Blob{} + meddler.QueryRow(b, &blob, queryBlob, path) + blob.Path = path + blob.Data = string(data) + return meddler.Save(b, tableBlob, &blob) +} + +// PutReader inserts an object into the blobstore by +// consuming data from r until EOF. +func (b *Blobstore) PutReader(path string, r io.Reader) error { + var data, _ = ioutil.ReadAll(r) + return b.Put(path, data) +} + +func New(db meddler.DB) *Blobstore { + return &Blobstore{db} +} diff --git a/server/blobstore/blobsql/const.go b/server/blobstore/blobsql/const.go new file mode 100644 index 00000000..d675ecd8 --- /dev/null +++ b/server/blobstore/blobsql/const.go @@ -0,0 +1,18 @@ +package blobsql + +const ( + tableBlob = "blobs" +) + +const ( + queryBlob = ` + SELECT * + FROM blobs + WHERE blob_path = ?; + ` + + deleteBlob = ` + DELETE FROM blobs + WHERE blob_path = ?; + ` +) diff --git a/server/blobstore/blobsql/context.go b/server/blobstore/blobsql/context.go new file mode 100644 index 00000000..2d8dc5df --- /dev/null +++ b/server/blobstore/blobsql/context.go @@ -0,0 +1,11 @@ +package blobsql + +import ( + "code.google.com/p/go.net/context" + "github.com/drone/drone/server/blobstore" + "github.com/russross/meddler" +) + +func NewContext(parent context.Context, db meddler.DB) context.Context { + return blobstore.NewContext(parent, New(db)) +} diff --git a/server/blobstore/blobstore.go b/server/blobstore/blobstore.go new file mode 100644 index 00000000..20044606 --- /dev/null +++ b/server/blobstore/blobstore.go @@ -0,0 +1,55 @@ +package blobstore + +import ( + "io" + + "code.google.com/p/go.net/context" +) + +type Blobstore interface { + // Del removes an object from the blobstore. + Del(path string) error + + // Get retrieves an object from the blobstore. + Get(path string) ([]byte, error) + + // GetReader retrieves an object from the blobstore. + // It is the caller's responsibility to call Close on + // the ReadCloser when finished reading. + GetReader(path string) (io.ReadCloser, error) + + // Put inserts an object into the blobstore. + Put(path string, data []byte) error + + // PutReader inserts an object into the blobstore by + // consuming data from r until EOF. + PutReader(path string, r io.Reader) error +} + +// Del removes an object from the blobstore. +func Del(c context.Context, path string) error { + return FromContext(c).Del(path) +} + +// Get retrieves an object from the blobstore. +func Get(c context.Context, path string) ([]byte, error) { + return FromContext(c).Get(path) +} + +// GetReader retrieves an object from the blobstore. +// It is the caller's responsibility to call Close on +// the ReadCloser when finished reading. +func GetReader(c context.Context, path string) (io.ReadCloser, error) { + return FromContext(c).GetReader(path) +} + +// Put inserts an object into the blobstore. +func Put(c context.Context, path string, data []byte) error { + return FromContext(c).Put(path, data) +} + +// PutReader inserts an object into the blobstore by +// consuming data from r until EOF. +func PutReader(c context.Context, path string, r io.Reader) error { + return FromContext(c).PutReader(path, r) +} diff --git a/server/blobstore/context.go b/server/blobstore/context.go new file mode 100644 index 00000000..519deb11 --- /dev/null +++ b/server/blobstore/context.go @@ -0,0 +1,31 @@ +package blobstore + +import ( + "code.google.com/p/go.net/context" +) + +const reqkey = "blobstore" + +// NewContext returns a Context whose Value method returns the +// application's Blobstore data. +func NewContext(parent context.Context, store Blobstore) context.Context { + return &wrapper{parent, store} +} + +type wrapper struct { + context.Context + store Blobstore +} + +// Value returns the named key from the context. +func (c *wrapper) Value(key interface{}) interface{} { + if key == reqkey { + return c.store + } + return c.Context.Value(key) +} + +// FromContext returns the Blobstore associated with this context. +func FromContext(c context.Context) Blobstore { + return c.Value(reqkey).(Blobstore) +} diff --git a/server/capability/capability.go b/server/capability/capability.go new file mode 100644 index 00000000..8ceb0a4b --- /dev/null +++ b/server/capability/capability.go @@ -0,0 +1,23 @@ +package capability + +import ( + "code.google.com/p/go.net/context" +) + +type Capability map[string]bool + +// Get the capability value from the map. +func (c Capability) Get(key string) bool { + return c.Get(key) +} + +// Sets the capability value in the map. +func (c Capability) Set(key string, value bool) { + c[key] = value +} + +// Enabled returns true if the capability is +// enabled in the system. +func Enabled(c context.Context, key string) bool { + return FromContext(c).Get(key) +} diff --git a/server/capability/context.go b/server/capability/context.go new file mode 100644 index 00000000..1f225be1 --- /dev/null +++ b/server/capability/context.go @@ -0,0 +1,32 @@ +package capability + +import ( + "code.google.com/p/go.net/context" +) + +const reqkey = "capability" + +// NewContext returns a Context whose Value method returns the +// application's Blobstore data. +func NewContext(parent context.Context, caps Capability) context.Context { + return &wrapper{parent, caps} +} + +type wrapper struct { + context.Context + caps Capability +} + +// Value returns the named key from the context. +func (c *wrapper) Value(key interface{}) interface{} { + if key == reqkey { + return c.caps + } + return c.Context.Value(key) +} + +// FromContext returns the capability map for the +// current context. +func FromContext(c context.Context) Capability { + return c.Value(reqkey).(Capability) +}