// 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.ObjectStore } 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") }