summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPablo M. Bermudo Garay <pablombg@gmail.com>2022-02-07 22:43:32 +0100
committerPablo M. Bermudo Garay <pablombg@gmail.com>2022-02-10 13:14:04 +0100
commita30c96160333edb002e5b1cc18ef20b07ddc78ee (patch)
tree0d360a51a1ade7b35789a86fc58f1e6e307c6ff6
Initial commit
Basic in-memory object store.
-rw-r--r--.gitignore1
-rw-r--r--LICENSE121
-rw-r--r--README.md3
-rw-r--r--go.mod16
-rw-r--r--go.sum40
-rw-r--r--main.go83
-rw-r--r--objectstore/memory_backend.go58
-rw-r--r--objectstore/memory_backend_test.go59
-rw-r--r--responses.go17
9 files changed, 398 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c5a22e2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+storehouse
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..0e259d4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,121 @@
+Creative Commons Legal Code
+
+CC0 1.0 Universal
+
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
+ HEREUNDER.
+
+Statement of Purpose
+
+The laws of most jurisdictions throughout the world automatically confer
+exclusive Copyright and Related Rights (defined below) upon the creator
+and subsequent owner(s) (each and all, an "owner") of an original work of
+authorship and/or a database (each, a "Work").
+
+Certain owners wish to permanently relinquish those rights to a Work for
+the purpose of contributing to a commons of creative, cultural and
+scientific works ("Commons") that the public can reliably and without fear
+of later claims of infringement build upon, modify, incorporate in other
+works, reuse and redistribute as freely as possible in any form whatsoever
+and for any purposes, including without limitation commercial purposes.
+These owners may contribute to the Commons to promote the ideal of a free
+culture and the further production of creative, cultural and scientific
+works, or to gain reputation or greater distribution for their Work in
+part through the use and efforts of others.
+
+For these and/or other purposes and motivations, and without any
+expectation of additional consideration or compensation, the person
+associating CC0 with a Work (the "Affirmer"), to the extent that he or she
+is an owner of Copyright and Related Rights in the Work, voluntarily
+elects to apply CC0 to the Work and publicly distribute the Work under its
+terms, with knowledge of his or her Copyright and Related Rights in the
+Work and the meaning and intended legal effect of CC0 on those rights.
+
+1. Copyright and Related Rights. A Work made available under CC0 may be
+protected by copyright and related or neighboring rights ("Copyright and
+Related Rights"). Copyright and Related Rights include, but are not
+limited to, the following:
+
+ i. the right to reproduce, adapt, distribute, perform, display,
+ communicate, and translate a Work;
+ ii. moral rights retained by the original author(s) and/or performer(s);
+iii. publicity and privacy rights pertaining to a person's image or
+ likeness depicted in a Work;
+ iv. rights protecting against unfair competition in regards to a Work,
+ subject to the limitations in paragraph 4(a), below;
+ v. rights protecting the extraction, dissemination, use and reuse of data
+ in a Work;
+ vi. database rights (such as those arising under Directive 96/9/EC of the
+ European Parliament and of the Council of 11 March 1996 on the legal
+ protection of databases, and under any national implementation
+ thereof, including any amended or successor version of such
+ directive); and
+vii. other similar, equivalent or corresponding rights throughout the
+ world based on applicable law or treaty, and any national
+ implementations thereof.
+
+2. Waiver. To the greatest extent permitted by, but not in contravention
+of, applicable law, Affirmer hereby overtly, fully, permanently,
+irrevocably and unconditionally waives, abandons, and surrenders all of
+Affirmer's Copyright and Related Rights and associated claims and causes
+of action, whether now known or unknown (including existing as well as
+future claims and causes of action), in the Work (i) in all territories
+worldwide, (ii) for the maximum duration provided by applicable law or
+treaty (including future time extensions), (iii) in any current or future
+medium and for any number of copies, and (iv) for any purpose whatsoever,
+including without limitation commercial, advertising or promotional
+purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
+member of the public at large and to the detriment of Affirmer's heirs and
+successors, fully intending that such Waiver shall not be subject to
+revocation, rescission, cancellation, termination, or any other legal or
+equitable action to disrupt the quiet enjoyment of the Work by the public
+as contemplated by Affirmer's express Statement of Purpose.
+
+3. Public License Fallback. Should any part of the Waiver for any reason
+be judged legally invalid or ineffective under applicable law, then the
+Waiver shall be preserved to the maximum extent permitted taking into
+account Affirmer's express Statement of Purpose. In addition, to the
+extent the Waiver is so judged Affirmer hereby grants to each affected
+person a royalty-free, non transferable, non sublicensable, non exclusive,
+irrevocable and unconditional license to exercise Affirmer's Copyright and
+Related Rights in the Work (i) in all territories worldwide, (ii) for the
+maximum duration provided by applicable law or treaty (including future
+time extensions), (iii) in any current or future medium and for any number
+of copies, and (iv) for any purpose whatsoever, including without
+limitation commercial, advertising or promotional purposes (the
+"License"). The License shall be deemed effective as of the date CC0 was
+applied by Affirmer to the Work. Should any part of the License for any
+reason be judged legally invalid or ineffective under applicable law, such
+partial invalidity or ineffectiveness shall not invalidate the remainder
+of the License, and in such case Affirmer hereby affirms that he or she
+will not (i) exercise any of his or her remaining Copyright and Related
+Rights in the Work or (ii) assert any associated claims and causes of
+action with respect to the Work, in either case contrary to Affirmer's
+express Statement of Purpose.
+
+4. Limitations and Disclaimers.
+
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
+ surrendered, licensed or otherwise affected by this document.
+ b. Affirmer offers the Work as-is and makes no representations or
+ warranties of any kind concerning the Work, express, implied,
+ statutory or otherwise, including without limitation warranties of
+ title, merchantability, fitness for a particular purpose, non
+ infringement, or the absence of latent or other defects, accuracy, or
+ the present or absence of errors, whether or not discoverable, all to
+ the greatest extent permissible under applicable law.
+ c. Affirmer disclaims responsibility for clearing rights of other persons
+ that may apply to the Work or any use thereof, including without
+ limitation any person's Copyright and Related Rights in the Work.
+ Further, Affirmer disclaims responsibility for obtaining any necessary
+ consents, permissions or other rights required for any use of the
+ Work.
+ d. Affirmer understands and acknowledges that Creative Commons is not a
+ party to this document and has no duty or obligation with respect to
+ this CC0 or use of the Work.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a41d042
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# Storehouse: Object storage HTTP server
+
+
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..5fcec8a
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,16 @@
+module github.com/pablombg/storehouse
+
+go 1.17
+
+require (
+ github.com/labstack/echo/v4 v4.6.3 // indirect
+ github.com/labstack/gommon v0.3.1 // indirect
+ github.com/mattn/go-colorable v0.1.11 // indirect
+ github.com/mattn/go-isatty v0.0.14 // indirect
+ github.com/valyala/bytebufferpool v1.0.0 // indirect
+ github.com/valyala/fasttemplate v1.2.1 // indirect
+ golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
+ golang.org/x/net v0.0.0-20210913180222-943fd674d43e // indirect
+ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
+ golang.org/x/text v0.3.7 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..0c76d4d
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,40 @@
+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/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
+github.com/labstack/echo/v4 v4.6.3 h1:VhPuIZYxsbPmo4m9KAkMU/el2442eB7EBFFhNTTT9ac=
+github.com/labstack/echo/v4 v4.6.3/go.mod h1:Hk5OiHj0kDqmFq7aHe7eDqI7CUhuCrfpupQtLGGLm7A=
+github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
+github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
+github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
+github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
+github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
+github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
+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/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
+github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
+github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
+github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
+golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
+golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20210913180222-943fd674d43e h1:+b/22bPvDYt4NPDcy4xAGCmON713ONAWFeY3Z7I3tR8=
+golang.org/x/net v0.0.0-20210913180222-943fd674d43e/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/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
+golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/text v0.3.3/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/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..268c689
--- /dev/null
+++ b/main.go
@@ -0,0 +1,83 @@
+// Storehouse: HTTP object store server
+//
+// Licensed under CC0 1.0 Universal:
+// https://creativecommons.org/publicdomain/zero/1.0/legalcode
+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+ "github.com/pablombg/storehouse/objectstore"
+)
+
+type storehouse struct {
+ objects *objectstore.MemObjectStore
+}
+
+func InitStorehouse() *storehouse {
+ objects := objectstore.NewMemBackend()
+ return &storehouse{objects: objects}
+}
+
+func (s *storehouse) putObject(c echo.Context) error {
+ bucketId := c.Param("bucketId")
+ objectId := c.Param("objectId")
+
+ bodyBytes, err := ioutil.ReadAll(c.Request().Body)
+ if err != nil {
+ log.Printf("Error reading body: %v", err)
+ m := &errorJSON{Error: "Error reading body"}
+ return c.JSON(http.StatusInternalServerError, m)
+ }
+ content := string(bodyBytes)
+
+ s.objects.CreateObject(bucketId, objectId, content)
+ m := &objectIdJSON{Id: objectId}
+ return c.JSON(http.StatusCreated, m)
+}
+
+func (s *storehouse) getObject(c echo.Context) error {
+ bucketId := c.Param("bucketId")
+ objectId := c.Param("objectId")
+
+ object, err := s.objects.GetObject(bucketId, objectId)
+ if err != nil {
+ log.Println(err)
+ m := &errorJSON{Error: "Object not found"}
+ return c.JSON(http.StatusNotFound, m)
+ }
+
+ return c.String(http.StatusOK, object)
+}
+
+func (s *storehouse) deleteObject(c echo.Context) error {
+ bucketId := c.Param("bucketId")
+ objectId := c.Param("objectId")
+
+ err := s.objects.DeleteObject(bucketId, objectId)
+ if err != nil {
+ log.Println(err)
+ m := &errorJSON{Error: "Object not found"}
+ return c.JSON(http.StatusNotFound, m)
+ }
+
+ m := &infoJSON{Info: "Object deleted"}
+ return c.JSON(http.StatusOK, m)
+}
+
+func main() {
+ // Initialization
+ s := InitStorehouse()
+ e := echo.New()
+
+ // Routes
+ e.GET("/objects/:bucketId/:objectId", s.getObject)
+ e.PUT("/objects/:bucketId/:objectId", s.putObject)
+ e.DELETE("/objects/:bucketId/:objectId", s.deleteObject)
+
+ // Start server
+ e.Start(":8080")
+}
diff --git a/objectstore/memory_backend.go b/objectstore/memory_backend.go
new file mode 100644
index 0000000..a21abca
--- /dev/null
+++ b/objectstore/memory_backend.go
@@ -0,0 +1,58 @@
+// Basic in-memory object store
+//
+// Licensed under CC0 1.0 Universal:
+// https://creativecommons.org/publicdomain/zero/1.0/legalcode
+package objectstore
+
+import "fmt"
+
+type memBucket struct {
+ objects map[string]string
+}
+
+type MemObjectStore struct {
+ buckets map[string]memBucket
+}
+
+func NewMemBackend() *MemObjectStore {
+ os := &MemObjectStore{}
+ os.buckets = make(map[string]memBucket)
+ return os
+}
+
+func (os *MemObjectStore) CreateObject(bucketId string, objectId string, object string) {
+ if bucket, ok := os.buckets[bucketId]; ok {
+ bucket.objects[objectId] = object
+ } else {
+ bucket := memBucket{}
+ bucket.objects = make(map[string]string)
+
+ bucket.objects[objectId] = object
+ os.buckets[bucketId] = bucket
+ }
+}
+
+func (os *MemObjectStore) GetObject(bucketId string, objectId string) (string, error) {
+ if bucket, ok := os.buckets[bucketId]; ok {
+ if object, ok := bucket.objects[objectId]; ok {
+ return object, nil
+ } else {
+ return "", fmt.Errorf("Object not found")
+ }
+ } else {
+ return "", fmt.Errorf("Bucket not found")
+ }
+}
+
+func (os *MemObjectStore) DeleteObject(bucketId string, objectId string) error {
+ if bucket, ok := os.buckets[bucketId]; ok {
+ if _, ok := bucket.objects[objectId]; ok {
+ delete(bucket.objects, objectId)
+ return nil
+ } else {
+ return fmt.Errorf("Object not found")
+ }
+ } else {
+ return fmt.Errorf("Bucket not found")
+ }
+}
diff --git a/objectstore/memory_backend_test.go b/objectstore/memory_backend_test.go
new file mode 100644
index 0000000..3fc2852
--- /dev/null
+++ b/objectstore/memory_backend_test.go
@@ -0,0 +1,59 @@
+// Memory backend tests
+//
+// Licensed under CC0 1.0 Universal:
+// https://creativecommons.org/publicdomain/zero/1.0/legalcode
+package objectstore
+
+import "testing"
+
+func TestCreateAndGet(t *testing.T) {
+ objects := NewMemBackend()
+
+ // Create the object
+ bucketId := "foo"
+ objectId := "bar"
+ objectContent := "Lorem ipsum"
+ objects.CreateObject(bucketId, objectId, objectContent)
+
+ // Retrieve the object
+ object, err := objects.GetObject(bucketId, objectId)
+ if err != nil {
+ t.Fatalf("Get test object: %v", err)
+ }
+
+ // Check the object content
+ if object != objectContent {
+ t.Errorf("Check test object. Got (%v), want (%v)", object, objectContent)
+ }
+
+ // Try to get a non-existent object
+ _, err = objects.GetObject(bucketId, "baz")
+ if err == nil {
+ t.Error("Get non-existent object. Got nil, want error")
+ }
+}
+
+func TestCreateAndDelete(t *testing.T) {
+ objects := NewMemBackend()
+
+ // Create the object
+ bucketId := "foo"
+ objectId := "bar"
+ objectContent := "Lorem ipsum"
+ objects.CreateObject(bucketId, objectId, objectContent)
+
+ // Try to delete a non-existent object
+ if err := objects.DeleteObject(bucketId, "baz"); err == nil {
+ t.Error("Delete non-existent object. Got nil, want error")
+ }
+
+ // Delete the object
+ if err := objects.DeleteObject(bucketId, objectId); err != nil {
+ t.Fatalf("Delete test object: %v", err)
+ }
+
+ // Try to re-delete the object
+ if err := objects.DeleteObject(bucketId, objectId); err == nil {
+ t.Error("Delete already deleted object. Got nil, want error")
+ }
+}
diff --git a/responses.go b/responses.go
new file mode 100644
index 0000000..b5234c1
--- /dev/null
+++ b/responses.go
@@ -0,0 +1,17 @@
+// JSON objects used in the responses
+//
+// Licensed under CC0 1.0 Universal:
+// https://creativecommons.org/publicdomain/zero/1.0/legalcode
+package main
+
+type errorJSON struct {
+ Error string `json:"error"`
+}
+
+type infoJSON struct {
+ Info string `json:"info"`
+}
+
+type objectIdJSON struct {
+ Id string `json:"id"`
+}