Skip to content

Commit

Permalink
Added list_location endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JarcauCristian committed Dec 19, 2023
1 parent a40867b commit dcbbe8b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 9 deletions.
28 changes: 28 additions & 0 deletions helper_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ func searchTags(alias []string, tags map[string]string) (map[string][]string, er
return map[string][]string{alias[0]: findings}, nil
}

func listMinioPath(alias []string, path string) ([]string, error) {
cmdArgs := []string{"./mc", "ls", alias[1] + "/" + path, "--json"}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, err
}
scanner := bufio.NewScanner(&stdout)

var files []string
for scanner.Scan() {
line := scanner.Text()
var data map[string]interface{}
if err := json.Unmarshal([]byte(line), &data); err != nil {
return nil, err
}

if data["status"].(string) == "success" && data["type"].(string) == "file" {
files = append(files, data["key"].(string))
}
}

return files, nil
}

func searchContentType(alias []string, contentType string) (map[string][]string, error) {
cmdArgs := []string{"./mc", "find", alias[1], fmt.Sprintf("--metadata=Content-Type=%s", contentType)}

Expand Down
40 changes: 40 additions & 0 deletions instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,46 @@ func (minioInstance *MinIO) Healths() (map[string]string, error) {
return health, nil
}

func (minioInstance *MinIO) listPath(path string) ([]string, error) {
healthyInstances, err := minioInstance.Healths()
if err != nil {
return nil, err
}

var findPath []string

var wg sync.WaitGroup

wg.Add(len(healthyInstances))
for k, v := range healthyInstances {
alias := []string{k, v}

go func(alias []string, datasetPath string) {
defer wg.Done()

finding, err := search(alias, datasetPath)

if err != nil {
fmt.Println("Is not present here!")
}

if finding != "" {
findPath = []string{alias[0], alias[1]}
}
}(alias, path)
}

wg.Wait()

files, err := listMinioPath(findPath, path)

if err != nil {
return nil, err
}

return files, nil
}

func (minioInstance *MinIO) findObject(datasetPath string) (string, error) {
healthyInstances, err := minioInstance.Healths()

Expand Down
52 changes: 43 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"io"
"mime/multipart"
Expand All @@ -22,12 +21,7 @@ func main() {
r := gin.Default()
r.MaxMultipartMemory = 100 << 20

config := cors.DefaultConfig()
config.AllowOrigins = []string{"https://localhost:3000"}
config.AllowHeaders = []string{"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"}
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}

r.Use(cors.New(config))
r.Use(CORSMiddleware())

r.GET("/get_all_objects", func(c *gin.Context) {
extension, okExtension := c.GetQuery("extension")
Expand Down Expand Up @@ -302,7 +296,7 @@ func main() {

if !exists {
c.JSON(400, gin.H{
"message": "Body is incorrect!",
"message": "dataset_path parameter is required!",
})
} else {
data, err := minio.findObject(datasetPath)
Expand All @@ -320,6 +314,28 @@ func main() {
}
})

r.GET("/list_location", func(c *gin.Context) {
path, exists := c.GetQuery("path")

if !exists {
c.JSON(400, gin.H{
"message": "path parameter is required!",
})
} else {
files, err := minio.listPath(path)

if err != nil {
c.JSON(404, gin.H{
"message": "Path not found!",
})
}

c.JSON(200, gin.H{
"files": files,
})
}
})

r.PUT("/put_object", func(c *gin.Context) {
authorization := c.Request.Header["Authorization"]

Expand Down Expand Up @@ -444,7 +460,9 @@ func main() {
for k, v := range tagData {
mapTags[k] = v.(string)
}

result, err := minio.uploadFile(reader, mapTags, float64(fileSize), fileName, contentType)
fmt.Println(result)

if err != nil {
c.JSON(500, gin.H{
Expand All @@ -467,7 +485,7 @@ func main() {

func verifyToken(tokenString string) bool {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://localhost:8080/auth/realms/react-keycloak/protocol/openid-connect/userinfo", nil)
req, _ := http.NewRequest("GET", "https://62.72.21.79:8442/auth/realms/react-keycloak/protocol/openid-connect/userinfo", nil)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", tokenString))
response, err := client.Do(req)
if err != nil {
Expand All @@ -478,3 +496,19 @@ func verifyToken(tokenString string) bool {
}
return true
}

func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")

if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}

c.Next()
}
}

0 comments on commit dcbbe8b

Please sign in to comment.