Skip to content

Commit

Permalink
aws, minio and swagger implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Feb 26, 2024
1 parent c59ae8b commit 5037762
Show file tree
Hide file tree
Showing 9 changed files with 739 additions and 154 deletions.
85 changes: 4 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,11 @@ Now, you can access the following services:
* MinIO: http://localhost:9001
* Go API: http://localhost:9090

### Postman Collection
You can find a [Postman Collection](go-minio-cdn.postman_collection.json) for this project in the go-minio-cdn.postman_collection.json file.


### Image Upload

#### Upload to MinIO

* HTTP POST: http://localhost:9090/upload
* Headers:
* Authorization (from .env)
* Body (form-data):

| KEY | VALUE |
|--------|--------------------|
| bucket | bucket name |
| path | slider |
| file | choose file |
| width | number (optional) |
| height | number (optional) |

Note: You can submit width and height if you want it to be resized during installation. If you send only width, the height will be assigned proportionally. If you send only the height, the width will be assigned proportionally. The resizing process is optional.


#### Upload to MinIO and AWS S3

If you want s3 glacier to be installed in your account, you should use this url.

* HTTP POST: http://localhost:9090/upload-with-aws
* Headers:
* Authorization (from .env)
* Body (form-data):

| KEY | VALUE |
|--------|-------------------|
| bucket | bucket name |
| path | slider |
| file | choose file |
| width | number (optional) |
| height | number (optional) |

Note: You can submit width and height if you want it to be resized during installation. If you send only width, the height will be assigned proportionally. If you send only the height, the width will be assigned proportionally. The resizing process is optional.


#### Image Resize

* HTTP POST: http://localhost:9090/resize
* Headers:
* Authorization (from .env)
* Body (form-data):

| KEY | VALUE |
|--------|-------------|
| width | 500 |
| height | 400 |
| file | choose file |

### Image Get

#### Get Image
* HTTP GET: http://localhost:9090/bucket-name/object-name

#### Get Image with Custom Width and Height
* HTTP GET (width and height): http://localhost:9090/bucket-name/w:300/h:200/object-name
* HTTP GET (width): http://localhost:9090/bucket-name/w:300/object-name
* HTTP GET (height): http://localhost:9090/bucket-name/h:200/object-name
##### Notes:
* if you use only width, height = will be proportioned according to the original size.
* if you use only height, width = will be proportioned according to the original size.

### Image Delete

#### Delete from MinIO
* HTTP DELETE: http://localhost:9090/bucket-name/object-name
* Headers:
* Authorization (from .env)


#### Delete from MinIO and AWS S3
* HTTP DELETE: http://localhost:9090/with-aws/bucket-name/object-name
* Headers:
* Authorization (from .env)
### Usage
- All usage: From the [Swagger UI](http://localhost:9090/swagger) user interface you can see and try all the uses. Or you can review [swagger.yaml](/public/swagger.yaml).
- You can submit width and height if you want it to be resized during upload. If you send only width, the height will be assigned proportionally. If you send only the height, the width will be assigned proportionally. The resizing process is optional.
- Get: if you use only width, height = will be proportioned according to the original size. If you use only height, width = will be proportioned according to the original size.


### Contributing
Expand Down
49 changes: 37 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
minioClient *minio.Client
imageHandler handler.Image
awsHandler handler.AwsHandler
minioHandler handler.MinioHandler
)

func main() {
Expand All @@ -30,6 +31,7 @@ func main() {
minioClient = service.MinioClient()
imageHandler = handler.NewImage(minioClient, awsService)
awsHandler = handler.NewAwsHandler(awsService)
minioHandler = handler.NewMinioHandler(minioClient)

app := fiber.New(fiber.Config{
BodyLimit: 25 * 1024 * 2014,
Expand All @@ -40,8 +42,6 @@ func main() {
AllowHeaders: "*",
}))

app.Static("/", "./public")

app.Use(favicon.New(favicon.Config{
File: "./public/favicon.png",
}))
Expand All @@ -50,9 +50,29 @@ func main() {
disableUpload := service.GetBool("DISABLE_UPLOAD")
disableGet := service.GetBool("DISABLE_GET")

// Swagger
app.Get("/swagger", func(c *fiber.Ctx) error {
return c.SendFile("./public/swagger.html")
})
app.Get("/swagger.yaml", func(c *fiber.Ctx) error {
return c.SendFile("./public/swagger.yaml")
})

// Aws
app.Get("/aws/bucket-list", awsHandler.BucketList)
app.Get("/aws/get-vault-list", awsHandler.GlacierVaultList)
aws := app.Group("/aws", AuthMiddleware)
aws.Get("/bucket-list", awsHandler.BucketList)
aws.Get("/:bucket/exists", awsHandler.BucketExists)
aws.Get("/vault-list", awsHandler.GlacierVaultList)

// Minio
io := app.Group("/minio", AuthMiddleware)
io.Get("/bucket-list", minioHandler.BucketList)
io.Get("/:bucket/exists", minioHandler.BucketExists)
io.Get("/:bucket/create", minioHandler.CreateBucket)
io.Get("/:bucket/delete", minioHandler.RemoveBucket)

// resize
app.Post("/resize", imageHandler.ResizeImage)

// Minio
if !disableGet {
Expand All @@ -63,23 +83,28 @@ func main() {
}

if !disableDelete {
app.Delete("/with-aws/:bucket/*", imageHandler.DeleteImageWithAws)
app.Delete("/:bucket/*", imageHandler.DeleteImage)
app.Delete("/with-aws/:bucket/*", AuthMiddleware, imageHandler.DeleteImageWithAws)
app.Delete("/:bucket/*", AuthMiddleware, imageHandler.DeleteImage)
}

if !disableUpload {
app.Post("/upload", imageHandler.UploadImage)
app.Post("/upload-with-aws", imageHandler.UploadImageWithAws)
app.Post("/upload-url", imageHandler.UploadImageWithUrl)
app.Post("/upload", AuthMiddleware, imageHandler.UploadImage)
app.Post("/upload-with-aws", AuthMiddleware, imageHandler.UploadImageWithAws)
app.Post("/upload-url", AuthMiddleware, imageHandler.UploadImageWithUrl)
}

app.Post("/resize", imageHandler.ResizeImage)

// Index
app.Get("/", func(c *fiber.Ctx) error {
return c.SendFile("index.html")
return c.SendFile("./public/index.html")
})

log.Fatal(app.Listen(":9090"))

}

func AuthMiddleware(c *fiber.Ctx) error {
if err := service.CheckToken(c); err != nil {
return service.Response(c, fiber.StatusBadRequest, false, "Invalid Token", nil)
}
return c.Next()
}
31 changes: 20 additions & 11 deletions handler/aws.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package handler

import (
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/mstgnz/cdn/service"
)

type AwsHandler interface {
GlacierVaultList(c *fiber.Ctx) error
BucketList(c *fiber.Ctx) error
BucketExists(c *fiber.Ctx) error
}

type awsHandler struct {
Expand All @@ -18,17 +21,23 @@ func NewAwsHandler(awsService service.AwsService) AwsHandler {
return &awsHandler{awsService: awsService}
}

func (ac awsHandler) BucketList(c *fiber.Ctx) error {
buckets, _ := ac.awsService.ListBuckets()
return c.JSON(fiber.Map{
"status": true,
"result": buckets,
})
func (a awsHandler) BucketExists(c *fiber.Ctx) error {
bucketName := c.Params("bucket")
exists := a.awsService.BucketExists(bucketName)
if !exists {
return service.Response(c, fiber.StatusNotFound, false, "not found", strconv.FormatBool(exists))
}
return service.Response(c, fiber.StatusFound, true, "found", strconv.FormatBool(exists))
}

func (a awsHandler) BucketList(c *fiber.Ctx) error {
buckets, err := a.awsService.ListBuckets()
if err != nil {
return service.Response(c, fiber.StatusOK, false, err.Error(), buckets)
}
return service.Response(c, fiber.StatusOK, true, "buckets", buckets)
}

func (ac awsHandler) GlacierVaultList(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": true,
"result": ac.awsService.GlacierVaultList(),
})
func (a awsHandler) GlacierVaultList(c *fiber.Ctx) error {
return service.Response(c, fiber.StatusOK, true, "glacier vault list", a.awsService.GlacierVaultList())
}
Loading

0 comments on commit 5037762

Please sign in to comment.